MyBatis Executor Overview and Source Parsing

Label (Space Separation): Mybatis

Actuator Overview

  • Like tomcat, Mybatis has an executor, but not as complex as tomcat.
  • The executor provides query, modification, transaction submission, transaction rollback, first-level cache cleaning, FlashStatement interface.
  • An executor corresponds to a SqlSession, which is created by Configuration.
  • Mybatis's executor is designed in delegation mode, and BaseExecutor, SimpleExecutor, ReuseExecutor, CachingExecutor are its implementation classes. CachingExecutor holds a delegate object, which implements the core functions of the executor.
  • Caching Executor implements caching based on BaseExecutor.
  • Simply put, Executor implements the interaction with the database.

Source code parsing

  • Let's take a look at Mybatis's process of creating Executor

Execution process: OpenSession FromDataSource is a submethod within openSession(). When Mybatis created SqlSession, a new Executor was put into Default SqlSession. We can also understand that a SqlSession holds an Executor. Executors and database connection pools are not associated here.

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();
    }
  }

The code of the new Executor method in openSessionFromDataSource is as follows:

  public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
    executorType = executorType == null ? defaultExecutorType : executorType;
    executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
    Executor executor;
    if (ExecutorType.BATCH == executorType) {
      executor = new BatchExecutor(this, transaction);
    } else if (ExecutorType.REUSE == executorType) {
      executor = new ReuseExecutor(this, transaction);
    } else {
      executor = new SimpleExecutor(this, transaction);
    }
    if (cacheEnabled) {
      executor = new CachingExecutor(executor);
    }
    executor = (Executor) interceptorChain.pluginAll(executor);
    return executor;
  }

In Configuration, the new Executor method determines whether to generate BatchExecutor or ReuseExecutor or SimpleExecutord according to the type of executor Type.
cacheEnabled indicates whether Phase II caching is enabled. There is a delegate delegate delegate delegate object in CachingExecutor. There is a real executor object. Intereptor Chain is the responsibility chain of interceptor. Executor transforms in pluginAll method. The final executor is a proxy object.
PS: Actuator, interceptor and cache are the three most important parts of Mybatis.

Below is the Mybatis executor class diagram, from which you can see that the executor uses the delegation mode.

Keywords: PHP Mybatis Tomcat Database Session

Added by pooker on Tue, 23 Jul 2019 05:52:06 +0300