前言
上一篇介绍了Mapper接口代理的实现原理,今天继续剖析SqlSession代理。希望看完大家能搞懂下面问题:
- Spring是如何管理Mapper的Bean,实现线程安全
-
Mybatis自身的sqlSession是否线程安全
源码分析
二. SqlSession代理
-
1、创建SqlSessionTemplate
在Spring扫描Mapper创建bean时,可以留意到不仅设置了className,beanClass,还设置了sqlSessionFactory属性。
public class ClassPathMapperScanner extends ClassPathBeanDefinitionScanner {
private MapperFactoryBean> mapperFactoryBean = new MapperFactoryBean
MapperFactoryBean继承于SqlSessionDaoSupport,当设置sqlSessionFactory属性时会触发setSqlSessionFactory()
方法。
public abstract class SqlSessionDaoSupport extends DaoSupport {
private SqlSessionTemplate sqlSessionTemplate;
/**
* Set MyBatis SqlSessionFactory to be used by this DAO. Will automatically create SqlSessionTemplate for the given
* SqlSessionFactory.
*
* @param sqlSessionFactory
* a factory of SqlSession
*/
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
if (this.sqlSessionTemplate == null || sqlSessionFactory != this.sqlSessionTemplate.getSqlSessionFactory()) {
this.sqlSessionTemplate = createSqlSessionTemplate(sqlSessionFactory);
}
protected SqlSessionTemplate createSqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
最终会为每个Mapper Bean创建一个SqlSessionTemplate对象,因此SqlSessionTemplate在Mapper Bean之间是不共享的。
- 2、创建动态代理
再看看SqlSessionTemplate的构造函数
public class SqlSessionTemplate implements SqlSession, DisposableBean {
public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType,PersistenceExceptionTranslator exceptionTranslator) {
...
this.sqlSessionFactory = sqlSessionFactory;
this.executorType = executorType;
this.exceptionTranslator = exceptionTranslator;
//创建sqlSession代理
this.sqlSessionProxy = (SqlSession) newProxyInstance(SqlSessionFactory.class.getClassLoader(),
new Class[] { SqlSession.class }, new SqlSessionInterceptor());
}
SqlSessionTemplate是实现了SqlSession接口的类。回顾上一篇,Mapper Bean实际调用的代理类MapperProxy,invoke()
方法里用到的sqlSession就是SqlSessionTemplate。然而SqlSessionTemplate自己也不亲自操作,而是使用动态代理替自己完成sqlSession操作
public class SqlSessionTemplate implements SqlSession, DisposableBean {
private final SqlSession sqlSessionProxy;
...
@Override
public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) {
this.sqlSessionProxy.select(statement, parameter, rowBounds, handler);
}
@Override
public int insert(String statement) {
return this.sqlSessionProxy.insert(statement);
}
...
}
顺而言之,关注点来到处理类SqlSessionInterceptor
。它是SqlSessionTemplate私有内部类。
private class SqlSessionInterceptor implements InvocationHandler {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//获取当前线程事务的SqlSession
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)) {
sqlSession.commit(true);
}
return result;
} catch (Throwable t) {
...
} finally {
if (sqlSession != null) {
closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory);
}
}
}
}
- 3、sqlSession线程管理
SqlSessionInterceptor
的invoke方法获取Sqlsession的方式并不简单。奥妙在于getSqlSession()
方法。它是Spring专门管理sqlSession的工具类SqlSessionUtils
里的静态方法。
public final class SqlSessionUtils {
public static SqlSession getSqlSession(SqlSessionFactory sessionFactory, ExecutorType executorType,PersistenceExceptionTranslator exceptionTranslator) {
...
//从当前线程获取SqlSessionHolder
SqlSessionHolder holder = (SqlSessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
//从SqlSessionHolder获取SqlSession,引用次数加1
SqlSession session = sessionHolder(executorType, holder);
if (session != null) {
return session;
}
LOGGER.debug(() -> "Creating a new SqlSession");
//如果当前线程没有sqlSession,创建一个
session = sessionFactory.openSession(executorType);
//将新的sqlSession注册当前线程
registerSessionHolder(sessionFactory, executorType, exceptionTranslator, session);
return session;
}
}
可以看出sqlSession是由SqlSessionHolder管理的。只要搞懂sqlSession如何创建与注册,获取的事就再简单不过了。重心放在最后两行代码sessionFactory.openSession(executorType)
和registerSessionHolder(sessionFactory, executorType, exceptionTranslator, session);
。
-
4、openSession()
实现了SqlSessionFactory的接口有两个:DefaultSqlSessionFactory和SqlSessionManager。
SqlSessionManager还实现了SqlSession类,是供用户单独用Mybatis时使用的。MyBatis为了将SqlSession管理权交给Spring。将SqlSession由SqlSessionTemplate来实现,SqlSessionFactory使用DefaultSqlSessionFactory。
public class DefaultSqlSessionFactory implements SqlSessionFactory {
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
Transaction tx = null;
try {
final Environment environment = configuration.getEnvironment();
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
// 创建事务
tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
final Executor executor = configuration.newExecutor(tx, execType);
//新建一个DefaultSqlSession对象作为SqlSession
return new DefaultSqlSession(configuration, executor, autoCommit);
} catch (Exception e) {
closeTransaction(tx); // may have fetched a connection so lets call close()
throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}
}
DefaultSqlSessionFactory不仅创建sqlSession还创建了事务。
- 5、registerSessionHolder
private static void registerSessionHolder(SqlSessionFactory sessionFactory, ExecutorType executorType,PersistenceExceptionTranslator exceptionTranslator, SqlSession session) {
SqlSessionHolder holder;
...
Environment environment = sessionFactory.getConfiguration().getEnvironment();
...
//将新的sqlSession与sqlSessionHolder绑定
holder = new SqlSessionHolder(session, executorType, exceptionTranslator);
//将新的sqlSessionHolder与线程绑定
TransactionSynchronizationManager.bindResource(sessionFactory, holder);
TransactionSynchronizationManager
.registerSynchronization(new SqlSessionSynchronization(holder, sessionFactory));
holder.setSynchronizedWithTransaction(true);
//引用次数加一
holder.requested();
...
}
registerSessionHolder
是SqlSessionUtils的私有静态方法,会创建新的SqlSessionHolder与当前线程绑定。
public abstract class TransactionSynchronizationManager {
private static final ThreadLocal
bindResource()
实现原理也很简单。将SessionFactory作为key,SqlSessionHolder为value存入一个map里,然后再将这个map存入ThreadLocal。因此,Spring调用Mybatis每个线程调用的是不同的SqlSessionHolder即不同的SqlSession,Spring就是这样管理sqlSession,实现线程安全的。
总结
经过两篇文章对mybatis源码的层层剖析,可知。Mapper Bean是通过MapperFactoryBean创建MapperProxy代理获取实现类。MapperProxy用到的SqlSession是SqlSessionTemplate,实际执行的是代理处理类SqlSessionInterceptor
。执行时会从当前线程获取SqlSessionHolder中的SqlSession,实现线程安全。