Skip to content

2nd cache doesn't work properly #62

@Alwayswithme

Description

@Alwayswithme

Description

I am using mybatis-spring-1.2.2, mybatis-3.2.8, spring-4.1.4.1.1.RELEASE
I have a spring bean call deviceMapper like this.

public interface DeviceMapper {
    @Select("SELECT * FROM Device")
    List<Device> all1();

    List<Device> all2();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.springapp.mvc.mapper.DeviceMapper">
    <cache readOnly="false"/>

    <select id="all2" resultType="device">
      SELECT * FROM Device
    </select>

</mapper>

And then I inject it and use it in a test method like this, and the XML query work well.

public class DeviceMapperTest {
    @Autowired
    private DeviceMapper deviceMapper;

    void test() {
        deviceMapper.all1();    // JDBC connection open
        deviceMapper.all1();    // connection open again, no cache hit

        deviceMapper.all2();    // JDBC connection open
        deviceMapper.all2();    // cache hit
    }
}

Log Output

DEBUG - Creating a new SqlSession
DEBUG - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3312a3e2] was not registered for synchronization because synchronization is not active
DEBUG - Fetching JDBC Connection from DataSource
DEBUG - JDBC Connection [ProxyConnection[PooledConnection[com.mysql.jdbc.JDBC4Connection@de62b5b]]] will not be managed by Spring
DEBUG - ==> Preparing: SELECT * FROM Device
DEBUG - ==> Parameters:
DEBUG - <== Total: 3
DEBUG - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3312a3e2]
DEBUG - Returning JDBC Connection to DataSource
DEBUG - Creating a new SqlSession
DEBUG - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@19256710] was not registered for synchronization because synchronization is not active
DEBUG - Fetching JDBC Connection from DataSource
DEBUG - JDBC Connection [ProxyConnection[PooledConnection[com.mysql.jdbc.JDBC4Connection@de62b5b]]] will not be managed by Spring
DEBUG - ==> Preparing: SELECT * FROM Device
DEBUG - ==> Parameters:
DEBUG - <== Total: 3
DEBUG - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@19256710]
DEBUG - Returning JDBC Connection to DataSource
DEBUG - Creating a new SqlSession
DEBUG - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@920693f] was not registered for synchronization because synchronization is not active
DEBUG - Cache Hit Ratio [com.springapp.mvc.mapper.DeviceMapper]: 0.0
DEBUG - Fetching JDBC Connection from DataSource
DEBUG - JDBC Connection [ProxyConnection[PooledConnection[com.mysql.jdbc.JDBC4Connection@de62b5b]]] will not be managed by Spring
DEBUG - ==> Preparing: SELECT * FROM Device
DEBUG - ==> Parameters:
DEBUG - <== Total: 3
DEBUG - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@920693f]
DEBUG - Returning JDBC Connection to DataSource
DEBUG - Creating a new SqlSession
DEBUG - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@592ba177] was not registered for synchronization because synchronization is not active
DEBUG - Cache Hit Ratio [com.springapp.mvc.mapper.DeviceMapper]: 0.5
DEBUG - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@592ba177]

If I using a @CacheNamespace in mapper interface instead of <cache /> in mapper XML, it became the annotation query work well.

    void test() {
        deviceMapper.all1();    // JDBC connection open
        deviceMapper.all1();    // cache hit

        deviceMapper.all2();    // JDBC connection open
        deviceMapper.all2();    // connection open again, no cache hit
    }

If my usage is incorrect and misunderstand the cache usage, please let me know.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions