Skip to content

Unexpected cache hit ratio is reported for a SELECT with flushCache enabled #1960

Open
@mengxiandong

Description

@mengxiandong

My email: [email protected]

Issue version: mybatis 3.5.1

Phenomenon description:

    declare an SQL, configure flachcache = true, and then use two sessions to query and submit; query and submit, session 2 hits the cache and queries the database(Console print log:Cache Hit Ratio [org.coderead.mybatis.UserMapper]: 0.5). I think hit cache and database query should be mutually exclusive.
   The execution process of mybatis is as follows:
           Session 1: clear the temporary storage area - > Put in the second level cache
           Session 2: clear the temporary storage area - > get the old value from the cache area - > query the database to get the new value, put in the second level cache - > return the new value

==============Here are the main source code and console logs================

==============The Test Class================

package org.coderead.mybatis.cache;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.coderead.mybatis.StatementHandlerTest;
import org.coderead.mybatis.UserMapper;

/**
 * @since 2020年6月13日20:28:39
 */
public class MySecondCacheTest {

    public static void main(String[] args) {
        SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = factoryBuilder.build(StatementHandlerTest.class.getResourceAsStream("/mybatis-config.xml"));
        for(int i = 0; i < 2; i++){
            SqlSession session = factory.openSession();
            session.getMapper(UserMapper.class).selectByid3(10);
            session.commit();
        }
    }

}

==============The Mapper================

package org.coderead.mybatis;

import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.coderead.mybatis.bean.User;

@CacheNamespace
public interface UserMapper {

    @Select({"select * from users where id=#{1}"})
    @Options(flushCache = Options.FlushCachePolicy.TRUE)
    User selectByid3(Integer id);
}

==============The console logs================

22:02:30,443 DEBUG org.coderead.mybatis.UserMapper:54 - Cache Hit Ratio [org.coderead.mybatis.UserMapper]: 0.0
22:02:30,447 DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction:54 - Opening JDBC Connection
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
22:02:30,979 DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource:54 - Created connection 1742920067.
22:02:30,980 DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction:54 - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@67e2d983]
22:02:31,034 DEBUG org.coderead.mybatis.UserMapper.selectByid3:54 - ==>  Preparing: select * from users where id=? 
22:02:31,060 DEBUG org.coderead.mybatis.UserMapper.selectByid3:54 - ==> Parameters: 10(Integer)
22:02:31,115 DEBUG org.coderead.mybatis.UserMapper.selectByid3:54 - <==      Total: 1
22:02:31,123 DEBUG org.coderead.mybatis.UserMapper:54 - Cache Hit Ratio [org.coderead.mybatis.UserMapper]: 0.5
22:02:31,123 DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction:54 - Opening JDBC Connection
22:02:31,439 DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource:54 - Created connection 1891502635.
22:02:31,439 DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction:54 - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@70be0a2b]
22:02:31,477 DEBUG org.coderead.mybatis.UserMapper.selectByid3:54 - ==>  Preparing: select * from users where id=? 
22:02:31,477 DEBUG org.coderead.mybatis.UserMapper.selectByid3:54 - ==> Parameters: 10(Integer)
22:02:31,519 DEBUG org.coderead.mybatis.UserMapper.selectByid3:54 - <==      Total: 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions