MyBatis源碼分析(三)-SqlSession理解

SqlSession理解
1、建立SqlSession
1.建立事務
a.事務工廠根據DataSource建立一個事務對象(Connection對象,事務級別,是否自動提交)
b.事務工廠根據Connection建立一個事務對象(事務級別和是否自動提交已經被設置在Connection對象中)
c.系統默認的事務工廠是ManagedTransactionFactory,默認是關閉鏈接的 private boolean closeConnection = true;

2.建立Executor(statement)對象
a.根據配置文件中的ExecutorType決定建立Executor的類型(SIMPLE【SimpleExecutor】, REUSE【ReuseExecutor】, BATCH【BatchExecutor】)
b.若是配置cacheEnabled=true,則建立executor = new CachingExecutor(executor);
3.返回SqlSession,默認返回的是DefaultSqlSession對象(該對象維護Configuration對象、Executor對象、是否自動提交)
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);
      return new DefaultSqlSession(configuration, executor, autoCommit);
    } 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();
    }
  }
  private SqlSession openSessionFromConnection(ExecutorType execType, Connection connection) {
    try {
      boolean autoCommit;
      try {
        autoCommit = connection.getAutoCommit();
      } catch (SQLException e) {
        // Failover to true, as most poor drivers
        // or databases won't support transactions
        autoCommit = true;
      }     
      final Environment environment = configuration.getEnvironment();
      final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
      final Transaction tx = transactionFactory.newTransaction(connection);
      final Executor executor = configuration.newExecutor(tx, execType);
      return new DefaultSqlSession(configuration, executor, autoCommit);
    } catch (Exception e) {
      throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
    } finally {
      ErrorContext.instance().reset();
    }
二.DefaultSqlSession對象,SqlSession對象的使用
(一)在使用DefaultSqlSession以前要現要求Mapper.xml的加載過程
對mapper節點的解析,XMLMapperBuilder對象進行xml文件解析和MapperBuilderAssistant對象將解析後的數據組裝成對象交付給Configuration對象

1.傳遞mapper.xml文件路徑、inputstream、Configuration對象
2.XMLMapperBuilder對象的configurationElement(parser.evalNode("/mapper"));解析mpper.xml文件
3.XMLMapperBuilder對象以buildStatementFromContext(context.evalNodes("select|insert|update|delete"));爲例進行分析
4.XMLMapperBuilder對象的buildStatementFromContext(List<XNode> list, String requiredDatabaseId),主要是XMLStatementBuilder對象在幹活
5.final XMLStatementBuilder statementParser = new XMLStatementBuilder(configuration, builderAssistant, context, requiredDatabaseId);
6.XMLStatementBuilder對象開始幹活:statementParser.parseStatementNode();最終將全部的配置信息放到MapperBuilderAssistant對象中,而後由MapperBuilderAssistant把真實的MappedStatement放到Configuration對象中
(二)使用SqlSession的重要一步就是首先要根據ID獲取MappedStatement
1.SqlSession session = sqlSessionFactory.openSession();
2.BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);//真實是從Configuration對象中進行查找的
3.Blog b = mapper.selectSomething(1);
4.session.close();sql

相關文章
相關標籤/搜索