设计模式_代理模式_在SqlSessionTemplate(Spring)中的应用

时间:2022-04-16 03:16:10

1.SqlSessionTemplate的构造函数,根据传入的SqlSessionFactory和ExecutorType创建一个Spring管理的SqlSession,并生成SqlSession的动态代理对象

 public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType,
PersistenceExceptionTranslator exceptionTranslator) { notNull(sqlSessionFactory, "Property 'sqlSessionFactory' is required");
notNull(executorType, "Property 'executorType' is required"); this.sqlSessionFactory = sqlSessionFactory;
this.executorType = executorType;
this.exceptionTranslator = exceptionTranslator;
//这里使用了jdk的动态代理,为SqlSession创建了代理对象
this.sqlSessionProxy = (SqlSession) newProxyInstance(
SqlSessionFactory.class.getClassLoader(),
new Class[] { SqlSession.class },
new SqlSessionInterceptor());
}

2.创建一个名为SqlSessionInterceptor的InvocationHandler实现类,当调用SqlSession的代理对象的方法时,会调用这个 InvocationHandler实现类的invokd方法,这里,SqlSessionInterceptor中实现了对SqlSession的释放

 private class SqlSessionInterceptor implements InvocationHandler {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
SqlSession sqlSession = getSqlSession(
SqlSessionTemplate.this.sqlSessionFactory,
SqlSessionTemplate.this.executorType,
SqlSessionTemplate.this.exceptionTranslator);
try {
Object result = method.invoke(sqlSession, args);
if (!isSqlSessionTransactional(sqlSession, SqlSessionTemplate.this.sqlSessionFactory)) {
// force commit even on non-dirty sessions because some databases require
// a commit/rollback before calling close()
sqlSession.commit(true);
}
return result;
} catch (Throwable t) {
Throwable unwrapped = unwrapThrowable(t);
if (SqlSessionTemplate.this.exceptionTranslator != null && unwrapped instanceof PersistenceException) {
// release the connection to avoid a deadlock if the translator is no loaded. See issue #22
  //如果遇到异常,释放SqlSession
closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory);
sqlSession = null;
Throwable translated = SqlSessionTemplate.this.exceptionTranslator.translateExceptionIfPossible((PersistenceException) unwrapped);
if (translated != null) {
unwrapped = translated;
}
}
throw unwrapped;
} finally {
//最后,如果SqlSession不为空,释放SqlSesssion
if (sqlSession != null) {
closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory);
}
}
}
}

3.通过SqlSession的代理对象来执行相关的增删改查方法

 @Override
public int insert(String statement) {
//通过SqlSessionTemplate构造函数中生成的SqlSession的动态代理对象来执行 insert 操作
return this.sqlSessionProxy.insert(statement);
} @Override
public int delete(String statement) {
//通过SqlSessionTemplate构造函数中生成的SqlSession的动态代理对象来执行 delete 操作
return this.sqlSessionProxy.delete(statement);
} @Override
public int update(String statement) {
//通过SqlSessionTemplate构造函数中生成的SqlSession的动态代理对象来执行 update 操作
return this.sqlSessionProxy.update(statement);
} @Override
public <T> T selectOne(String statement, Object parameter) {
//通过SqlSessionTemplate构造函数中生成的SqlSession的动态代理对象来执行 selectOne 操作
return this.sqlSessionProxy.<T> selectOne(statement, parameter);
}

4.由于SqlSessionTemplate通过代理的方式帮我们维护了SqlSession的生命周期(close,rollback,comit这些),所以我们不能显示的调用close方法

 @Override
public void close() {
//不可以对Spring管理的SqlSession执行close方法
throw new UnsupportedOperationException("Manual close is not allowed over a Spring managed SqlSession");
}

5.在SpringBoot项目中实测SqlSessionTemplate的方法执行,执行完方法后,会释放相应的SqlSession资源

设计模式_代理模式_在SqlSessionTemplate(Spring)中的应用

设计模式_代理模式_在SqlSessionTemplate(Spring)中的应用

设计模式_代理模式_在SqlSessionTemplate(Spring)中的应用