Mybatis source code parsing (b): SqlSession

Mybatis process:

Here Insert Picture Description

2. Get The SqlSession

  • Remove Exceutor (actuator) from the configuration file, call openSessionFromDataSource ()
    Here Insert Picture Description
  • New types of actuators default: SIMPLE
    Here Insert Picture Description
  • There are three types of actuators:
    Here Insert Picture Description
  • note:
    Here Insert Picture Description
	executor = (Executor) interceptorChain.pluginAll(executor);

Executor to intercept enhanced design patterns used here was the decorator pattern

  • Then return: DefaultSqlSession (configuratioin, Executor, affairs issues)
    Executor execution of CRUD configuratioinHere Insert Picture Description

Summary: The process of obtaining the SqlSession

  • First SqlSessionFactory call openSession ()
    SqlSession session = sessionFactory.openSession();
  • Then openSession () call openSessionFromDataSource ()
    public SqlSession openSession() {
      return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
  • Return DefaultSqlSession objects , note DefaultSqlSession include: (the Configuration, Executor, affairs)
    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 {
Published 47 original articles · won praise 34 · views 8873

Guess you like