The scope and life cycle of MyBatis core components

We have mastered the creation of MyBatis components and their basic applications, but this is far from enough, we also need to discuss its life cycle.

Lifecycle is an important issue for components, especially in multi-threaded environments, such as Internet applications, Socket requests, etc. MyBatis is also commonly used in multi-threaded environments. Misuse will cause serious multi-threaded concurrency problems. In order to write correctly For MyBatis applications, we need to master the life cycle of MyBatis components.

The so-called life cycle is the time that each object should survive. For example, some objects will be closed after they are used up once, so that they will be destroyed by the  Java  Virtual Machine (JVM) to avoid continuing to occupy resources, so we will go according to the role of each component. Determine its life cycle.

SqlSessionFactoryBuilder

The function of SqlSessionFactoryBuilder is to create SqlSessionFactory. After the creation is successful, SqlSessionFactoryBuilder loses its function, so it can only exist in the method of creating SqlSessionFactory, and do not let it exist for a long time. So the best scope for an instance of SqlSessionFactoryBuilder is method scope (that is, local method variables).

SqlSessionFactory

SqlSessionFactory can be considered as a database connection pool, its role is to create SqlSession interface objects. Because the essence of MyBatis is the operation of Java on the database, the life cycle of SqlSessionFactory exists in the entire MyBatis application, so once the SqlSessionFactory is created, it must be saved for a long time until the MyBatis application is no longer used, so the life of SqlSessionFactory can be considered The cycle is equivalent to the application cycle of MyBatis.

Since SqlSessionFactory is a connection pool to the database, it occupies the connection resources of the database. If you create multiple SqlSessionFactories, then there will be multiple database connection pools, which is not conducive to the control of database resources, and will also lead to the consumption of database connection resources, system downtime, etc., so try to avoid such situations.

Therefore, in general applications, we often want SqlSessionFactory as a singleton, so that it can be shared in the application. So the best scope for SqlSessionFactory is application scope.

SqlSession

If SqlSessionFactory is equivalent to a database connection pool, then SqlSession is equivalent to a database connection (Connection object). You can execute multiple SQLs in a transaction, and then submit or rollback the transaction through its commit, rollback and other methods.

So it should survive in a business request. After processing the entire request, it should close the connection and return it to SqlSessionFactory, otherwise the database resources will be exhausted quickly and the system will be paralyzed, so use try...catch ...finally... statement to ensure that it closes properly.

So the best scope for SqlSession is request or method scope.

Mapper

Mapper is an interface, which is created by SqlSession, so its maximum life cycle is at most consistent with SqlSession. Although it is very useful, its database connection resources will also disappear due to the closure of SqlSession, so its life cycle should be Less than or equal to the life cycle of SqlSession. Mapper represents the business processing in a request, so it should be in a request, and once the related business is processed, it should be discarded.

Above, we discussed the life cycle of MyBatis components, as shown in Figure 1.

Life cycle of MyBatis components


Figure 1 Life cycle of MyBatis components

Guess you like

Origin blog.csdn.net/unbelievevc/article/details/132268560