SqlSession对应一次数据库会话,每次访问数据库都需要创建SqlSession,一旦关闭sqlSession就需要在重写创建。在SqlSession中定义了常用的数据库的操作以及事务的操作。sql语句在执行的时候流程如下图:
后续将介绍其他几个类。
DefaultSqlSession
该类实现了SqlSession接口,也是单独使用Mybatis进行开发时最常使用的SqlSession。其将所有数据库相关的操作全部封装到Executor接口实现中,并通过executor字段选择不同的Executor实现。数据库的查找最后通过Executor.query实现。
public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
try {
MappedStatement ms = configuration.getMappedStatement(statement);
return executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error querying database. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}
DefaultSqlSessionFactory
是一个工厂类,实现了SqlSessionFactory接口,主要提供了两种创建DefaultSqlSession。一种是通过数据源获取数据库连接,并创建Executor对象以及DefaultSqlSession对象。
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
Transaction tx = null;
try {
//获取配置environment对象
final Environment environment = configuration.getEnvironment();
//获取配置的TransactionFactory对象
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
//创建Executor对象
final Executor executor = configuration.newExecutor(tx, execType);
//创建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会使用该数据库连接对象创建Executor和DefaultSqlSession对象。
private SqlSession openSessionFromConnection(ExecutorType execType, Connection connection) {
try {
boolean autoCommit;
try {
autoCommit = connection.getAutoCommit();
} catch (SQLException e) {
// Failover to true, as most poor drivers
// or databases won't support transactions
autoCommit = true;
}
final Environment environment = configuration.getEnvironment();
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
final Transaction tx = transactionFactory.newTransaction(connection);
//创建executor
final Executor executor = configuration.newExecutor(tx, execType);
//创建sqlSession
return new DefaultSqlSession(configuration, executor, autoCommit);
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}
SqlSessionManager
同时实现了SqlSession和SqlSessionFactory接口。同时提供了SqlSessionFactory创建SqlSession和管理数据库操作的功能。SqlSessionManager只有一个私有构造方法,创建对象需要调用newInstance方法,在实现SqlSession接口的方法时,都直接调用sqlSessionProxy的sqlSession代理对象实现相应的方法。创建代理对象使用的是SqlSessionInteerceptor对象,其创建sqlSession有两种方式。第一种与DefaultSqlSessionFactory的行为相同,同一线程通过sqlSession访问数据库的时候,都会创建新的SqlSession。另一种是通过ThreadLocal记录当前线程绑定的sqlSession,供当前线程使用,避免多次创建。
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//获取当前线程绑定的SqlSession
final SqlSession sqlSession = SqlSessionManager.this.localSqlSession.get();
if (sqlSession != null) {
try {
//第二种模式
return method.invoke(sqlSession, args);
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
} else {
//没有绑定sqlSession,则新建一个SqlSession
try (SqlSession autoSqlSession = openSession()) {
try {
final Object result = method.invoke(autoSqlSession, args);
autoSqlSession.commit();
return result;
} catch (Throwable t) {
autoSqlSession.rollback();
throw ExceptionUtil.unwrapThrowable(t);
}
}
}
}