Mybatis initialization process analysis is completed, followed by the second step of SqlSession creation.
Overview of the creation process
The SqlSession creation process is as follows:
Mybatis provides two implementations of SqlSessionFactory: SqlSessionManager and DefaultSqlSessionFactory are returned by DefaultSqlSessionFactory by default.
DefaultSqlSessionFactory overloads multiple methods for creating a SqlSession object, specifying automatic commit, transaction isolation level, and specified executor type in addition to the default no-argument method. The actuator type is an enumeration that corresponds to the three actuator types provided by Mybatis: SIMPLE, REUSE, and BATCH.
Starting with the no-argument method, you can see that you end up passing three default parameters: the default actuator type (which defaults to SIMPLE in Configuration), the default isolation level, and non-auto-commit. The process of creating a SqlSession does three things:
Mybatis -config = mybatis-config = mybatis-config = mybatis
2. Create transaction objects based on data source, transaction isolation level, and whether to commit automatically in the transaction factory root configuration file;
Create an executor object based on the transaction object and executor type.
Create the DefaultSqlSession object and set the Configuration and executor objects to this object.
This process creates three objects: the transaction, the executor, and DefaultSqlSession.
Transaction creation process
Transaction creation source code process as shown below:
The JDBC type alias corresponds to the JdbcTransactionFactory class in the configuration file, so we can directly look at the JdbcTransactionFactory class. The method for creating a Transaction is newTransaction, which calls the JdbcTransaction constructor directly, except that there are no extra operations to set the data source, Transaction isolation level, and automatic commit to the object.
JdbcTransaction is an implementation of Transaction. It already implements the getConnection, COMMIT, rollback, close, and getTimeout methods for Transaction.
Mybatis DataSource has two implementations: PooledDataSource and UnpooledDataSource. It is determined by the type value of the DataSource node in the configuration file. In production, we use connection pooling like DBCP or DruID.
Actuator creation
The source code for actuator initialization is shown below:
Executor objects are created using the Configuration object. The newExecutor method determines the type of Executor to be created and then creates an object based on that type. If caching is enabled, a CachingExecutor object is created using the new Executor object as a parameter.
CachingExecutor holds other Executor objects that perform operations on them. CachingExecutor only cache that block. CachingExecutor is more like a proxy for other executors.
Configuration and Transaction are stored in BaseExecutor, and SimpleExecutor implements methods to manipulate the database, which we’ll see later.
The pluginAll method on the interceptorChain is used to wrap the executor through all configured interceptors.
conclusion
The constructor for DefaultSqlSession has nothing to say, so I won’t expand it here. SqlSession has global configuration and executor. Executor has global configuration and Transaction. SqlSession has global configuration and Transaction. A Transaction has a DataSource obtained from the Configuration, which contains the connection information of the database.
The SqlSession then executes the SQL statement and continues.
Java programmer daily study notes, such as understanding the wrong welcome to exchange discussion!