Four cases of Mybatis first-level cache failure (actually two)

When we learn Mybatis, we inevitably learn the cache module. Among them, the first-level cache invalidation online videos or articles agree that there are four situations:

1. Different SqlSession objects correspond to different first-level caches, even if the same data is queried, the database must be revisited;

2. The same SqlSession object, but the query conditions are different;

3. The same SqlSession object performs any "addition, deletion and modification" operations during two queries, regardless of whether these "additions, deletions and modifications" operations affect the cached data;

4. The cache is manually emptied during two queries of the same SqlSession object (the clearCache() method of the SqlSession object is called).

I know that the first and second cases cannot be called "failure". You must know that "failure" means that the cache does not exist, but the cache still exists in the first and second cases. We can verify it through the code. First look at the first case:

    @Test
    public void getEmpbyEid3() throws Exception {
    
    
        SqlSession sqlSession = SqlSessionUtils.getSqlSession(true);
        SqlSession sqlSession2 = SqlSessionUtils.getSqlSession(true);

        CacheMapper mapper = sqlSession.getMapper(CacheMapper.class);
        CacheMapper mapper2 = sqlSession2.getMapper(CacheMapper.class);

        Emp empByCche = mapper.getEmpByCche(3);
        Emp empByCche2 = mapper2.getEmpByCche(3);
        Emp empByCche3 = mapper.getEmpByCche(3);

        System.out.println(empByCche);
        System.out.println(empByCche2);
        System.out.println(empByCche3);
    }
}

operation result:

DEBUG 12-24 13:51:02,501 ==>  Preparing: select * from t_emp_s where eid = ? (BaseJdbcLogger.java:137) 
DEBUG 12-24 13:51:02,527 ==> Parameters: 3(Integer) (BaseJdbcLogger.java:137) 
DEBUG 12-24 13:51:02,543 <==      Total: 1 (BaseJdbcLogger.java:137) 
DEBUG 12-24 13:51:02,551 ==>  Preparing: select * from t_emp_s where eid = ? (BaseJdbcLogger.java:137) 
DEBUG 12-24 13:51:02,551 ==> Parameters: 3(Integer) (BaseJdbcLogger.java:137) 
DEBUG 12-24 13:51:02,553 <==      Total: 1 (BaseJdbcLogger.java:137) 
Emp{
    
    eid=3, empName='王五', sex='男', email='123@qq.com', age=23, dept=null}
Emp{
    
    eid=3, empName='王五', sex='男', email='123@qq.com', age=23, dept=null}
Emp{
    
    eid=3, empName='王五', sex='男', email='123@qq.com', age=23, dept=null}

Process finished with exit code 0

First look at the first case: we access the same method through different sqlsessions, and because of different sqlsessions, mybatis queries the database twice. But when the third print statement is executed, the database is not queried again, so we can know that the cache of the first sqlsession has not expired.

Second case:

    @Test
    public void getEmpbyEid2() throws Exception {
    
    
        SqlSession sqlSession = SqlSessionUtils.getSqlSession(true);

        CacheMapper mapper = sqlSession.getMapper(CacheMapper.class);

        Emp empByCche = mapper.getEmpByCche(3);
        Emp empByCche2 = mapper.getEmpByCche(1);
        Emp empByCche3 = mapper.getEmpByCche(3);

        System.out.println(empByCche);
        System.out.println(empByCche2);
        System.out.println(empByCche3);
    }

operation result:

DEBUG 12-24 13:59:56,400 ==>  Preparing: select * from t_emp_s where eid = ? (BaseJdbcLogger.java:137) 
DEBUG 12-24 13:59:56,420 ==> Parameters: 3(Integer) (BaseJdbcLogger.java:137) 
DEBUG 12-24 13:59:56,442 <==      Total: 1 (BaseJdbcLogger.java:137) 
DEBUG 12-24 13:59:56,442 ==>  Preparing: select * from t_emp_s where eid = ? (BaseJdbcLogger.java:137) 
DEBUG 12-24 13:59:56,443 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:137) 
DEBUG 12-24 13:59:56,444 <==      Total: 1 (BaseJdbcLogger.java:137) 
Emp{
    
    eid=3, empName='王五', sex='男', email='123@qq.com', age=23, dept=null}
Emp{
    
    eid=1, empName='张三', sex='男', email='123@qq.com', age=23, dept=null}
Emp{
    
    eid=3, empName='王五', sex='男', email='123@qq.com', age=23, dept=null}

Process finished with exit code 0

We can also check and execute different query conditions, mybatis will query the database again, but it will not make the cache of the same sqlsessin, the same method, and the same query condition "invalid".

The above is my personal opinion, if there is any mistake, please correct me.

Guess you like

Origin blog.csdn.net/qq_46051303/article/details/128428013