mybatis SqlSessionFactory . openSession()方法执行过程

//false: 不自动提交
SqlSession sqlSession = factory.openSession(false);

public class DefaultSqlSessionFactory implements SqlSessionFactory {

	//DefaultSqlSessionFactory 中唯一成员变量
 	private final Configuration configuration;

	@Override
	public SqlSession openSession(boolean autoCommit) {
		return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, autoCommit);
	}

    //configuration.getDefaultExecutorType() 得到的结果就是默认的 ExecutorType.SIMPLE;
	public class Configuration {
	
		//ExecutorType.SIMPLE: 这个执行器类型不做特殊的事情。它为每个语句的执行创建一个新的预处理语句。
		//ExecutorType.REUSE: 这个执行器类型会复用预处理语句。
		//ExecutorType.BATCH: 这个执行器会批量执行所有更新语句,如果 SELECT 在它们中间执行还会标定它们是 必须的,来保证一个简单并易于理解的行为。
		
		protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE;
	}

	private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
		Transaction tx = null;
		
		//通过configuration(封装所有信息);获取到Environment信息()
		final Environment environment = configuration.getEnvironment();
		
		//从Environment中获取TransactionFactory
		final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);

		private TransactionFactory getTransactionFactoryFromEnvironment(Environment environment) {
			if (environment == null || environment.getTransactionFactory() == null) {
			  	return new ManagedTransactionFactory();
			}
			return environment.getTransactionFactory();
		}		

		//TransactionFactory是Environment类中的对象属性
		public final class Environment {
			  private final String id;
			  private final TransactionFactory transactionFactory;
			  private final DataSource dataSource;
		}
		
		//transactionFactory创建一个事务 : 传参数 -- environment.数据源, level : 事务隔离级别, 自动提交
		tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);	//555
		
		//根据execType类型, 和事务对象, 创建一个执行器
		final Executor executor = configuration.newExecutor(tx, execType);			//666
		
		//创建DefaultSqlSession: 根据配置对象configuration, 执行器 executor, 自动提交boolean值
		return new DefaultSqlSession(configuration, executor, autoCommit);			//777
		
		}
	
	}

//返回一个ManagedTransaction对象(TransactionFactory 实现类)
public class ManagedTransactionFactory implements TransactionFactory {//555

	private boolean closeConnection = true;
	
	@Override
	public Transaction newTransaction(DataSource ds, TransactionIsolationLevel level, boolean autoCommit) {
		return new ManagedTransaction(ds, level, closeConnection);
	}

}

public Executor newExecutor(Transaction transaction, ExecutorType executorType) {//666

    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) {//默认true
      executor = new CachingExecutor(executor);
    }

	//装饰模式, 将SimpleExecutor 装饰到 CachingExcutor
    executor = (Executor) interceptorChain.pluginAll(executor);
    return executor;

}

//创建一个SqlSession子类DefaultSqlSession对象返回, openSession()方法获得了SqlSession
public class DefaultSqlSession implements SqlSession { // 777

	private final Configuration configuration;
	private final Executor executor;
	private final boolean autoCommit;
	private boolean dirty;
	
	public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {
		this.configuration = configuration;
		this.executor = executor;
		this.dirty = false;
		this.autoCommit = autoCommit;	
	}

}







你可能感兴趣的:(mybatis SqlSessionFactory . openSession()方法执行过程)