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

What kind of pit will be encountered in the MyBatis first-level cache?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail what kind of pit will be encountered in MyBatis level 1 cache. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

First-level cache concept

When we use Mybatis for database operations, a SqlSession is created for a database session, and the SqlSession object is closed at the end of the session. Then the life cycle of an SqlSession corresponds to a session of the Mybatis. In a session of Mybatis, we will most likely query the exact same sql statement multiple times, and query the database once for each query if no action is taken. However, the time of a session is generally very short, and the query results of the same Sql are very likely to be exactly the same. As the cost of querying the database is relatively high, it will lead to a waste of resources in the system.

To solve this problem, Mybatis adds caching operations to each session. The scope of this cache is in one session. Caching occurs with the creation of a session (SqlSession) and is released as the session ends. For the query operation of a session, always check whether the query result exists in the cache, if so, directly fetch the result in the cache, and query the database if it does not exist. In this way, the exact same query in a session will be queried only once, saving system resources.

Introduction

MyBatis level 1 caching (MyBaits calls it Local Cache) cannot be turned off, but there are two levels to choose from:

Package org.apache.ibatis.session;/** * @ author Eduardo Macarron * / public enum LocalCacheScope {SESSION, / / session-level cache STATEMENT / / statement-level cache}

1) session-level caching

Within the same sqlSession, the database will no longer be queried for the same query, directly from the cache.

Verification code:

[

Public static void main (String [] args) throws IOException {InputStream inputStream = new ClassPathResource ("mybatis.xml"). GetInputStream (); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder (). Build (inputStream); SqlSession sqlSession = sqlSessionFactory.openSession (); UserDao mapper = sqlSession.getMapper (UserDao.class); System.out.println (mapper.get (1L)); System.out.println ("-"); System.out.println (mapper.get (1L));}

Output:

As you can see from the log output, the first query is queried through the database, but the second query is not, and is read directly through the cache.

Pit: there is a pit in this caching strategy, which can cause problems when serving the cluster.

Suppose you now have a service cluster with two nodes.

First of all, both nodes make the same query, and both nodes have their own first-level cache. After the same query, the two nodes will no longer query the database.

If node 1 executes the update statement at this time, the first-level cache of node 1 will be flushed, while the first-level cache of node 2 will not be changed.

2) statement-level caching

Pit avoidance: to avoid this problem, you can set the level of the first-level cache to the statement level, so that the first-level cache will be cleared every time the query ends. The MyBatis source code is as follows:

In the configuration file for MyBatis, add the following configuration:

The verification code remains the same as above.

Output:

As you can see, even for the same query, each query reads the database directly.

Avoid the pit. Over.

Caching is impossible without caching, at this point, you need to use caching middleware, which manages caching.

This is the end of this article on "what kind of pit will be encountered in the first-level cache of MyBatis". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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