zoukankan      html  css  js  c++  java
  • 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   }
     
    生于忧患,死于安乐
  • 相关阅读:
    车载OS盘点及特点分析一:车载OS几大系统介绍
    CTF常用软件/工具
    汽车软件产业研究报告(2020年)
    高级加密标准(AES)分析
    工具 | CTP、SimNow、NSight、快期
    CTF之图片隐写术解题思路
    V2X和车路协同研究:5G V2X将成为数字座舱标配
    腾讯安全正式发布《IoT安全能力图谱》
    Microsoft Remote Desktop Beta 下载地址
    密码学初探|加密模式
  • 原文地址:https://www.cnblogs.com/songlove/p/14609968.html
Copyright © 2011-2022 走看看