Self-test the first level cache and second level cache of mybatis

First let's talk about the definition:

The first level cache is the cache on the SqlSession;

The second level cache is the cache on the SQLSessionFactory.

In the absence of any configuration, mybatis opens the first level cache by default;

public static void main(String[] args) {
		Logger log = Logger.getLogger(Test.class);
		SqlSession sqlSession=null;
		try {
			sqlSession = CreatFactoryUtils.openSqlSession();
			UserDao mapper=sqlSession.getMapper(UserDao.class);
			List<User> list = mapper.findUser();
			log.info("================"+list);
			log.info("Call this query again to see if sql is still executed");
			List<User> listNew = mapper.findUser();
			log.info("================"+listNew);
		} catch (Exception e) {
		}finally {
			if (sqlSession!=null) {
				sqlSession.close();
			}
		}

The print result is as follows:

DEBUG 2018-04-30 01:36:36,529 org.apache.ibatis.datasource.pooled.PooledDataSource:Created connection 1445157774.
DEBUG 2018-04-30 01:36:36,533 dao.UserDao.findUser:==>  Preparing: select id,username,userpwd from t_users
DEBUG 2018-04-30 01:36:36,574 dao.UserDao.findUser:==> Parameters:
DEBUG 2018-04-30 01:36:36,592 dao.UserDao.findUser:<==      Total: 1
April 30, 2018 1:36:36 am [mybatis.Test] UNKNOWN METHOD
Information: ===============[User [id=1, username=dddd, userpwd=123]]
April 30, 2018 1:36:36 am [mybatis.Test] UNKNOWN METHOD
INFO: Call this query again to see if sql is still executed
April 30, 2018 1:36:36 am [mybatis.Test] UNKNOWN METHOD
Information: ===============[User [id=1, username=dddd, userpwd=123]]
DEBUG 2018-04-30 01:36:36,602 org.apache.ibatis.transaction.jdbc.JdbcTransaction:Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@56235b8e]
DEBUG 2018-04-30 01:36:36,602 org.apache.ibatis.datasource.pooled.PooledDataSource:Returned connection 1445157774 to pool.

It can be seen that in the two query processes, only one sql statement is executed; it means that mybatis has already opened the first-level cache by default. Some people will say, why is this not the cache of SQLSessionFactory? Then let's look at:

	public static void main(String[] args) {
		Logger log = Logger.getLogger(Test.class);
		SqlSession sqlSession=null;
		SqlSession sqlSessionNew=null;
		try {
			sqlSession = CreatFactoryUtils.openSqlSession();
			UserDao mapper=sqlSession.getMapper(UserDao.class);
			List<User> list = mapper.findUser();
			sqlSession.commit();
			log.info("================"+list);
			log.info("Call this query again to see if sql is still executed");
			List<User> listNew = mapper.findUser();
			log.info("================"+listNew);
		} catch (Exception e) {
		}finally {
			if (sqlSession!=null) {
				sqlSession.close();
			}
			if(sqlSessionNew!=null){
				sqlSessionNew.close();
			}
		}
	}

The print result at this time:

DEBUG 2018-04-30 02:05:38,594 org.apache.ibatis.datasource.pooled.PooledDataSource:Created connection 1445157774.
DEBUG 2018-04-30 02:05:38,595 dao.UserDao.findUser:==>  Preparing: select id,username,userpwd from t_users 
DEBUG 2018-04-30 02:05:38,631 dao.UserDao.findUser:==> Parameters: 
DEBUG 2018-04-30 02:05:38,648 dao.UserDao.findUser:<==      Total: 1
April 30, 2018 2:05:38 am [mybatis.Test] UNKNOWN METHOD
Information: ===============[User [id=1, username=dddd, userpwd=123]]
April 30, 2018 2:05:38 am [mybatis.Test] UNKNOWN METHOD
INFO: Call this query again to see if sql is still executed
DEBUG 2018-04-30 02:05:38,661 dao.UserDao.findUser:==>  Preparing: select id,username,userpwd from t_users 
DEBUG 2018-04-30 02:05:38,661 dao.UserDao.findUser:==> Parameters: 
DEBUG 2018-04-30 02:05:38,662 dao.UserDao.findUser:<==      Total: 1
April 30, 2018 2:05:38 am [mybatis.Test] UNKNOWN METHOD
Information: ===============[User [id=1, username=dddd, userpwd=123]]
DEBUG 2018-04-30 02:05:38,663 org.apache.ibatis.transaction.jdbc.JdbcTransaction:Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@56235b8e]
DEBUG 2018-04-30 02:05:38,663 org.apache.ibatis.datasource.pooled.PooledDataSource:Returned connection 1445157774 to pool.

Obviously, two sql queries are executed; it needs to be explained here that after the commit is submitted, the first-level cache will be invalid. This is to prevent dirty reads, so that when you query again, the sql is executed from the database.

This means that the first level cache is on the SQLSession

Now let's take a look at the second-level cache. The above test code remains unchanged, but the second-level cache first entity class must implement the Serializable interface, otherwise an error will be reported;

public class User implements Serializable{

	private String id;
	private String username;
	private String userpwd;

Then add <cache></cache> to the mapping file

<resultMap id="user" type="pojo.User" >  
    <id column="id" property="id"  />  
    <result column="username" property="username" />  
    <result column="userpwd" property="userpwd"  />
  </resultMap>
  <cache></cache>

Now that the preparations are complete, we also use the test files submitted by the two query commits just now. No changes are required. Now the first-level cache is invalid, so we open the second-level cache to see if we need to perform two queries:

DEBUG 2018-04-30 02:07:27,603 org.apache.ibatis.datasource.pooled.PooledDataSource:Created connection 1864350231.
DEBUG 2018-04-30 02:07:27,606 dao.UserDao.findUser:==>  Preparing: select id,username,userpwd from t_users
DEBUG 2018-04-30 02:07:27,643 dao.UserDao.findUser:==> Parameters:
DEBUG 2018-04-30 02:07:27,662 dao.UserDao.findUser:<==      Total: 1
April 30, 2018 2:07:27 am [mybatis.Test] UNKNOWN METHOD
Information: ===============[User [id=1, username=dddd, userpwd=123]]
April 30, 2018 2:07:27 am [mybatis.Test] UNKNOWN METHOD
INFO: Call this query again to see if sql is still executed
DEBUG 2018-04-30 02:07:27,742 dao.UserDao:Cache Hit Ratio [dao.UserDao]: 0.5
April 30, 2018 2:07:27 am [mybatis.Test] UNKNOWN METHOD
Information: ===============[User [id=1, username=dddd, userpwd=123]]
DEBUG 2018-04-30 02:07:27,742 org.apache.ibatis.transaction.jdbc.JdbcTransaction:Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@6f1fba17]
DEBUG 2018-04-30 02:07:27,743 org.apache.ibatis.datasource.pooled.PooledDataSource:Returned connection 1864350231 to pool.

The result is that only one query is executed, the second-level cache has taken effect, and the first-level and second-level cache should be understood here;



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325592274&siteId=291194637