• 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   }
     
    生于忧患,死于安乐
  • 相关阅读:
    rabiitmq 消息丢失处理
    后端返回文件流和json格式的方式进行文件下载导出
    elasticsearch 安装
    docker-compose 命令
    艾孜尔江的部分影视作品
    游戏作品:万里同风
    Unity设置计时器
    Unity项目中VS Code部分功能出现问题解决思路
    Unity接收FFmpeg的UDP推流
    SVN Basics
  • 原文地址:https://www.cnblogs.com/songlove/p/14609968.html
Copyright © 2020-2023  润新知