question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

2nd cache doesn't work properly

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
harawatacommented, Jul 14, 2016

@xueyumusic 's comment seems to be correct. Let me know if you think otherwise. And thank you for the detailed report!

0reactions
harawatacommented, Jun 7, 2018

@wqbill , No problem at all. 😃 The hardest part is to reproduce the problem on my end, so please upload the project you used to test to your GitHub repo. Then I will look into it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hibernate Second level cache doesn't work - Stack Overflow
I didn't cached embedded objects. And It looked like query was working twice. But second time it didn't really loaded VersionData, but queried ......
Read more >
Hibernate Second-Level Cache - Baeldung
A guide to the Hibernate second-level cache and how to use it in practice.
Read more >
Pitfalls of the Hibernate Second-Level / Query Caches - DZone
This post will go through how to setup the Hibernate Second-Level and Query caches, how they work and what are their most common...
Read more >
How Do I Fix My Caching Problems Or Clear Web Browser's ...
Open your browser and click on the settings button (...) in the top-right corner. · Click on the three horizontal dots again &...
Read more >
A Tip For Properly Utilizing Hibernate 2nd Level Cache
The solution is to use @Cache(..) (a hibernate-specific annotation) on all the collections. What about lazy collections? Laziness serves a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found