zoukankan      html  css  js  c++  java
  • SqlSessionFactory创建SqlSession的过程

    SqlSessionFactory接口中声明了一系列opensession方法,用来返回SqlSession对象。

    而DefaultSqlSessionFactory是他的实现类,实现了其中的方法。

    如下:

      public SqlSession openSession() {
        return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
      }

    其中openSessionFromDataSource方法如下:

      private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
        Transaction tx = null;
        try {
          final Environment environment = configuration.getEnvironment();
          final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
          tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
          final Executor executor = configuration.newExecutor(tx, execType, autoCommit);
          return new DefaultSqlSession(configuration, executor);
        } catch (Exception e) {
          closeTransaction(tx); // may have fetched a connection so lets call close()
          throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
        } finally {
          ErrorContext.instance().reset();
        }
      }

    传入的第一个参数为configuration中执行器类型(有三种,SimpleExecutor,BatchExecutor,ReuseExecutor),第二个参数为事物管理等级,第三个是是否自动提交事物。

    在方法中的操作:获取environment对象(全局xml文件中配置)

    通过TransactionFactory工厂创建事物对象

    创建Executor对象,需要传入事物对象、environment对象,以及autoCommit。

    最后创建并返回SqlSession的实现类的对象,并将其需要的参数传入。

  • 相关阅读:
    获取bootstrap table数据并封装 为json
    不自动切换eclipse视图
    over 分析函数之 lag() lead()
    oracle日期的处理
    表空间的创建
    分析函数 over用法 之row_number() runk_number
    oracle 序列
    Laravel 5
    使用hexo+github搭建免费个人博客详细教程
    windows7设置定时任务运行ThinkPHP框架程序
  • 原文地址:https://www.cnblogs.com/neu-student/p/7517292.html
Copyright © 2011-2022 走看看