本文共 3771 字,大约阅读时间需要 12 分钟。
SqlSession对象是数据库操作的核心单元,获取过程涉及多个关键步骤。以下是具体实现方法:
通过调用sqlSessionFactory.openSession()方法获取SqlSession实例:
SqlSession session = sqlSessionFactory.openSession();
根据全局配置获取默认的Executor类型,创建事务上下文:
public SqlSession openSession() { return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);} 获取环境信息,创建事务工厂并初始化默认事务:
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) { Transaction tx = null; try { // 获取环境信息 final Environment environment = configuration.getEnvironment(); // 创建事务 tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit); // 创建执行器 final Executor executor = configuration.newExecutor(tx, execType); // 创建SqlSession return new DefaultSqlSession(configuration, executor, autoCommit); } catch (Exception e) { closeTransaction(tx); throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e); } finally { ErrorContext.instance().reset(); }} 根据ExecutorType创建相应的执行器实例:
public Executor newExecutor(Transaction transaction, ExecutorType executorType) { executorType = executorType == null ? defaultExecutorType : executorType; executorType = executorType == null ? ExecutorType.SIMPLE : executorType; Executor executor; if (ExecutorType.BATCH == executorType) { executor = new BatchExecutor(this, transaction); } else if (ExecutorType.REUSE == executorType) { executor = new ReuseExecutor(this, transaction); } else { executor = new SimpleExecutor(this, transaction); } // 二级缓存配置 if (cacheEnabled) { executor = new CachingExecutor(executor); } // 拦截器链 executor = (Executor) interceptorChain.pluginAll(executor); return executor;} CachingExecutor类的构造方法:
public CachingExecutor(Executor delegate) { this.delegate = delegate; delegate.setExecutorWrapper(this);} pluginAll方法实现:
public Object pluginAll(Object target) { for (Interceptor interceptor : interceptors) { target = interceptor.plugin(target); } return target;} DefaultSqlSession类的构造:
public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) { this.configuration = configuration; this.executor = executor; this.autoCommit = autoCommit;} SqlSession session = sqlSessionFactory.openSession();
根据默认ExecutorType创建事务上下文:
@Overridepublic SqlSession openSession() { return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);} 获取环境信息,创建事务工厂并初始化默认事务:
final Environment environment = configuration.getEnvironment();final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
根据ExecutorType创建相应的执行器实例:
final Executor executor = configuration.newExecutor(tx, execType);
对二级缓存的具体操作:
public CachingExecutor(Executor delegate) { this.delegate = delegate; delegate.setExecutorWrapper(this);} pluginAll方法实现:
public Object pluginAll(Object target) { for (Interceptor interceptor : interceptors) { target = interceptor.plugin(target); } return target;} DefaultSqlSession类的构造:
public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) { this.configuration = configuration; this.executor = executor; this.autoCommit = autoCommit;} 通过以上步骤,可以清晰地了解SqlSession对象的获取与创建过程,包括事务管理、执行器创建以及二级缓存配置等关键环节。
转载地址:http://mpwc.baihongyu.com/