MyBatis actuator with an overview of the source code parsing

Tags (separated by spaces): Mybatis


Actuator overview

  • Wanted, like tomcat, Mybatis actuators also exist, but no less complex actuator tomcat.
  • Actuator provides query, modify, commit the transaction, roll back the transaction, clearing cache, FlashStatement interface.
  • An actuator corresponds to a SqlSession, created by Configuration.
  • Mybatis actuators using entrusting mode design, BaseExecutor, SimpleExecutor, ReuseExecutor, CachingExecutor its implementation class. CachingExecutor hold a delegate object is achieved by the implementation of its core functions.
  • CachingExecutor in BaseExecutor basis to achieve cache function.
  • In simple terms Executor to achieve the interaction with the database.

Source resolve

  • Let's look at the process of creation Executor of Mybatis

Mybatis creation process

Implementation process: openSessionFromDataSource is openSession () method inside of a child, Mybatis in the creation of SqlSession, new an Executor and put it in the DefaultSqlSession, we can also interpret it this way a SqlSession hold a Executor. And performing database connection pool where there is no association.

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

newExecutor openSessionFromDataSource method code which 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;
  }

newExecutor in the Configuration Method, according to the type of determination is to generate executorType BatchExecutor or ReuseExecutor, SimpleExecutord
, cacheEnabled indicates whether to enable two cache. There is CachingExecutor a delegate delegate object, there real implementation object, interceptorChain interceptor chain of responsibility, Executor done in pluginAll conversion method, the final executor is a proxy object.
PS: actuators, interceptor, the cache is the most important of the three parts Mybatis.

The following is a class diagram Mybatis actuator, it can be seen from the actuator's delegated mode.
FIG Mybatis performing class

Guess you like

Origin www.cnblogs.com/tamguo/p/11230222.html