MyBatis源码阅读–SqlSessionFactory说明

MyBatis源码阅读-总索引

文章目录

  • 前言
  • 继承体系
  • 源码分析
    • 私有属性
    • 构造器
    • openSession()获取SqlSession

前言

SqlSessionFactory 用于根据配置文件生成的配置类Configuration创建SqlSession。

继承体系

MyBatis源码阅读–SqlSessionFactory说明_第1张图片
可以看到SqlSessionFactory接口有两个实现类DefaultSqlSessionFactory和SqlSessionManager,SqlSessionManager还实现了SqlSession接口,SqlSessionFactoryBuilder中是使用DefaultSqlSessionFactory来创建SqlSessionFactory,本文章只介绍DefaultSqlSessionFactory。

	//SqlSessionFactoryBuilder
    public SqlSessionFactory build(Configuration config) {
        return new DefaultSqlSessionFactory(config);
    }

源码分析

私有属性

只有一个私有属性Configuration对象,其由由MyBatis配置文件转化而成。

 private final Configuration configuration;

构造器

只有一个构造器,参数为Configuration类对象

 public DefaultSqlSessionFactory(Configuration configuration) {
        this.configuration = configuration;
    }

openSession()获取SqlSession

可以看到openSession主要要以下几个入参

  • autoCommit :
    true:自动提交,无需再显示调用sqlSession.commit();进行提交。
    false:不自动提交,需再显示调用sqlSession.commit();进行提交,否则更新删除插入操作无效。
  • ExecutorType
    配置默认的执行器。SIMPLE 就是普通的执行器;REUSE 执行器会重用预处理语句(prepared statements); BATCH 执行器将重用语句并执行批量更新。
  • TransactionIsolationLevel:
  • TransactionIsolationLevel事务相关的隔离等级,用于解决脏读,不可重复读等并发问题,后面会有文章进行相关介绍。
    READ_COMMITTED:读提交
    READ_UNCOMMITTED: 读未提交
    REPEATABLE_READ:可重复读
    SERIALIZABLE:可序列化

√: 可能出现 ×: 不会出现
MyBatis源码阅读–SqlSessionFactory说明_第2张图片

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

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

    public SqlSession openSession(ExecutorType execType) {
        return this.openSessionFromDataSource(execType, (TransactionIsolationLevel)null, false);
    }

    public SqlSession openSession(TransactionIsolationLevel level) {
        return this.openSessionFromDataSource(this.configuration.getDefaultExecutorType(), level, false);
    }

    public SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level) {
        return this.openSessionFromDataSource(execType, level, false);
    }

    public SqlSession openSession(ExecutorType execType, boolean autoCommit) {
        return this.openSessionFromDataSource(execType, (TransactionIsolationLevel)null, autoCommit);
    }

    public SqlSession openSession(Connection connection) {
        return this.openSessionFromConnection(this.configuration.getDefaultExecutorType(), connection);
    }

    public SqlSession openSession(ExecutorType execType, Connection connection) {
        return this.openSessionFromConnection(execType, connection);
    }

上面的openSession都是通过调用openSessionFromDataSource来创建SqlSession。
最终是根据相关配置使用DefaultSqlSession类来创建SqlSession对象。
var8 = new DefaultSqlSession(this.configuration, executor, autoCommit);


    private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
        Transaction tx = null;

        DefaultSqlSession var8;
        try {
            Environment environment = this.configuration.getEnvironment();
            TransactionFactory transactionFactory = this.getTransactionFactoryFromEnvironment(environment);
            tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
            Executor executor = this.configuration.newExecutor(tx, execType);
            var8 = new DefaultSqlSession(this.configuration, executor, autoCommit);
        } catch (Exception var12) {
            this.closeTransaction(tx);
            throw ExceptionFactory.wrapException("Error opening session.  Cause: " + var12, var12);
        } finally {
            ErrorContext.instance().reset();
        }

        return var8;
    }
		//Connection  : java.sql.Connection
    private SqlSession openSessionFromConnection(ExecutorType execType, Connection connection) {
        DefaultSqlSession var8;
        try {
            boolean autoCommit;
            try {
                autoCommit = connection.getAutoCommit();
            } catch (SQLException var13) {
                autoCommit = true;
            }

            Environment environment = this.configuration.getEnvironment();
            TransactionFactory transactionFactory = this.getTransactionFactoryFromEnvironment(environment);
            Transaction tx = transactionFactory.newTransaction(connection);
            Executor executor = this.configuration.newExecutor(tx, execType);
            var8 = new DefaultSqlSession(this.configuration, executor, autoCommit);
        } catch (Exception var14) {
            throw ExceptionFactory.wrapException("Error opening session.  Cause: " + var14, var14);
        } finally {
            ErrorContext.instance().reset();
        }

        return var8;
    }

获取TransactionFactory

    private TransactionFactory getTransactionFactoryFromEnvironment(Environment environment) {
        return (TransactionFactory)(environment != null && environment.getTransactionFactory() != null ? environment.getTransactionFactory() : new ManagedTransactionFactory());
    }

你可能感兴趣的:(Mybatis源码阅读,MyBatis)