• Mybaits源码分析八之sqlSessiion的openSession方法


    mybatis的Demo案例中  ,通过sqlSessionFactory创建Session

    1  //通过sqlSessionFactory创建SqlSession
    2     public static SqlSession getSession(){
    3         return sqlSessionFactory.openSession();
    4     }
    1   @Override
    2   public SqlSession openSession(boolean autoCommit) {
    3     return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, autoCommit);
    4   }
     1   private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
     2     Transaction tx = null;
     3     try {
     4      //获取environment对象,就是mybatis配置中的environment子节点下的配置内容
     5       final Environment environment = configuration.getEnvironment();
     6      //构建事务工厂
     7       final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
     8     //创建事务实例
     9       tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
    10     //创建一个执行器
    11       final Executor executor = configuration.newExecutor(tx, execType);
    12      //返回默认的sqlSession
    13       return new DefaultSqlSession(configuration, executor, autoCommit);
    14     } catch (Exception e) {
    15       closeTransaction(tx); // may have fetched a connection so lets call close()
    16       throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
    17     } finally {
    18       ErrorContext.instance().reset();
    19     }
    20   }
     configuration.newExecutor(tx, execType);为创建一个执行器,创建执行器的过程为:
     1 public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
     2     //获取默认的执行器类型,默认为SIMPLE
     3     executorType = executorType == null ? defaultExecutorType : executorType;
     4     executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
     5     Executor executor;
     6    //执行器类型为三种BATCH 批量执行已经预编译的sql,REUSE 重复执行sql,SIMPLE只执行一次
     7     if (ExecutorType.BATCH == executorType) {
     8       executor = new BatchExecutor(this, transaction);
     9     } else if (ExecutorType.REUSE == executorType) {
    10       executor = new ReuseExecutor(this, transaction);
    11     } else {
    12       executor = new SimpleExecutor(this, transaction);
    13     }
    14     //判断是否开启缓存,默认是开启的
    15     if (cacheEnabled) {
    16       executor = new CachingExecutor(executor);
    17     }
    18     executor = (Executor) interceptorChain.pluginAll(executor);
    19     return executor;
    20   }
     
    生于忧患,死于安乐
  • 相关阅读:
    NSUserDefaults
    版本控制
    真机调试流程
    UIImageView加载图片的两种方式
    UI控件之--UIButton
    Xcode错误总结
    自动布局AutoLayout
    地理编码和反地理编码
    WordPress NOSpam PTI插件‘comment_post_ID’参数SQL注入漏洞
    WordPress RokMicroNews插件‘thumb.php’ 多个安全漏洞
  • 原文地址:https://www.cnblogs.com/songlove/p/14609968.html
Copyright © 2020-2023  润新知