Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why must SqlSession in mybatis be turned off?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Why must close the SqlSession in mybatis, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Mybatis's SqlSession must be shut down.

A strange problem arises when querying data using mybatis today. For the same sql statement, the query is fast and sometimes slow, and has a certain regularity. About one out of every 10 queries will be particularly slow, the fast one only needs 1ms, and the slow one needs 20000ms SQL code and screenshots of fast and slow time:

Select fknr from jq_fkqk where jjxh =? A fast situation

Slow situation

When printing mybatis query information from the log, I observed that it was particularly slow, not because it was slow to query, but because it had to wait a long time to start:

Preparing: select fknr from jq_fkqk where jjxh =?

It means that it will be here for a long time, the waiting time.

Why do you have to wait so long? I naturally looked at the last query function, found that there is no session.close () in the function, add this sentence, the problem is solved, the speed is very fast.

I wanted to look at the official documents and see why, but I couldn't find it, so I had to analyze it based on my own understanding. SqlSession is constructed through SqlSessionFactory, which is equivalent to maintaining a connection pool. When we keep querying, the number of connections to the database reaches an upper limit because the connection is not closed (maybe the connection pool has the maximum number of connections, but we have found documents).

After reaching the upper limit, when you request the query again, Factory says that there is no connection. Please wait a moment, it will first determine which SqlSession is no longer used (similar to the garbage collection mechanism), and then call the corresponding process to automatically close the useless session connection. Note that the calling process is queued and takes time.

After shutting down the useless session, Factory informs you that there is a free session, so start preparing your query, so you will wait a long time to appear:

Preparing: select fknr from jq_fkqk where jjxh =?

This is purely my own understanding, but the point is expressed, that is, the session in Mybatis must manually close it, session.close (), otherwise it will occupy resources and lead to performance degradation!

Usage of SqlSession in mybatis I. scope of use of SqlSession

What is stored in SqlSession is the compiled sql statements, which are generated by reading the mapper.xml file from the mybatis configuration file, and the sql statements are stored in SqlSessionFactory and SqlSession. Encapsulate the operation of the database, such as: query, insert, update, delete and so on.

SqlSession is created through SqlSessionFactory, while SqlSessionFactory is created by loading configuration files through SqlSessionFactoryBuilder. In development, SqlSession is turned off every time it is used, and created when it is used, that is, multiple cases, thread-safe. SqlSessionFactory has only one object in the whole class, that is, singleton, singleton thread is not safe.

II. SqlSessionFactoryBuilder

SqlSessionFactoryBuilder is used to create SqlSessionFacoty,SqlSessionFacoty. Once created, SqlSessionFactoryBuilder is not needed, because SqlSession is produced through SqlSessionFactory, so you can use SqlSessionFactoryBuilder as a utility class. The best scope of use is the method scope, that is, the local variables in the method body.

III. SqlSessionFactory

SqlSessionFactory is an interface, and different overloading methods of openSession are defined in the interface. The best scope of use of SqlSessionFactory is throughout the running of the application. Once created, it can be reused. SqlSessionFactory is usually managed in singleton mode.

IV. SqlSession

SqlSession is a user-oriented interface, database operations are defined in sqlSession, and DefaultSqlSession implementation classes are used by default.

The execution process is as follows:

1. Load data sources and other configuration information

Environment environment = configuration.getEnvironment ()

2. Create a database link

3. Create a transaction object

4. All operations to create an Executor,SqlSession are done through Executor. The mybatis source code is as follows:

If (ExecutorType.BATCH = = executorType) {executor = newBatchExecutor (this, transaction);} elseif (ExecutorType.REUSE = = executorType) {executor = new ReuseExecutor (this, transaction);} else {executor = new SimpleExecutor (this, transaction);} if (cacheEnabled) {executor = new CachingExecutor (executor, autoCommit);}

The implementation class of SqlSession is DefaultSqlSession, and Executor is essentially used in this object to manipulate the database

Each thread should have its own instance of SqlSession. Instances of SqlSession cannot be shared, and it is not thread-safe. So the best scope is the request or method scope. Never place a reference to an SqlSession instance in a static field or an instance field of a class.

Open a SqlSession; and close it when you have finished using it. This close operation is usually placed in the finally block to ensure that the close can be performed every time. As follows:

SqlSession session = sqlSessionFactory.openSession (); try {/ / do work} finally {session.close ();} is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report