Mybatis principle analysis (two) the creation process of SqlSession

In the previous article, we took an in-depth look at what SqlSessionFactory did during the creation process, so here we will continue to talk about the next step of SqlSession creation. Click here for those who did not read the previous article:

Mybatis principle analysis (1) parse the global xml configuration file to generate SqlSessionFactory

First of all, we know that in the process of creating SqlSessionFactory, in fact, most of the bottom layer is to initialize our Configuration object, and create a DefaultSqlSession object to the upper layer through this constructed Configuration object as a parameter. So now we have a DefaultSqlSession object that contains a Configuration object.

So what we are going to study now is SqlSession session = sqlSessionFactory.openSession(true) what the bottom layer of the process of creating SqlSession has done.

Well, let’s not say much, let’s debug.

Continue to dive into the openSessionFromDataSource method

 It is obvious here that we can see that what is finally returned to us is a DefaultSqlSession object, and this object requires an Executor. What is an Executor and what is it for? Here we take a look at the newExecutor method of creating Executor in depth.

 

Let’s look at this method in three parts

part1

According to the passed defaultExecutorType to determine what type of Executor to create, if the type is simple, create a SimpleExecutor, if the type is reuse, create a ReuseExecutor, and if the type is batch, create a BatchExecutor.

part2

After creating a specific Executor object, then check whether the second level cache is turned on. The setting of the second level cache is set in the global configuration file, so if it is turned on, the cacheEnabled property of our Configuration object is true , Otherwise it is false. If it is true, that is, if the secondary cache is turned on, then we will pass the created Executor object as a parameter to a constructor called CacheExecutor. So we can dive into CacheExecutor:

You can find that the incoming Executor object is assigned to the Executor variable inside. Look at its other methods, such as:

It can be found that the actual execution of the operation to interact with the database is actually the Executor object we passed in, indicating that CacheExecutor is just another layer of our Executor encapsulated, and then some more caching methods are added to it. In fact, this is also Java. The static proxy of the design pattern is implemented. CacheExecutor is equivalent to a proxy class of Executor, which adds the function of caching. 

part3 

The sentence executor = (Executor) interceptorChain.pluginAll(executor) is very important. We will encounter this code later. What is it? This is actually related to our plug-in mechanism. In Mybatis, we can customize the plug-in. We will talk about this later.

After going through these 3 parts, you get an Executor object that may be encapsulated by CacheExecutor and plug-ins. Finally return to the upper level

Finally, what we get is a DefaultSqlSession object that implements the SqlSession interface, and the object contains the Configuration object and the Executor object that may be encapsulated.

Guess you like

Origin blog.csdn.net/weixin_37689658/article/details/99121800