Hibernate query returns stale data

I have a sleeping request (hibernate 3) that only reads data from a database. The database is updated by a separate application, and the query result does not reflect changes in the database.

With a bit of research, I think this might have something to do with the Hibernate L2 cache (I don't think this is the L1 cache, since I always open a new session and close it after it ends).

Session session = sessionFactoryWrapper.getSession();
List<FlowCount> result = session.createSQLQuery(flowCountSQL).list();
session.close();

I tried disabling the second level cache in the hibernate configuration file, but it does not work:

<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="hibernate.cache.use_query_cache">false</property>
<propertyname="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

I also added session.setCacheMode(CacheMode.Refresh);after Session session = sessionFactoryWrapper.getSession();to force update L1 cache, but still not working ...

Is there any other way to get the changes to the database? Am I doing something wrong how to disable the cache? Thank.

Update: , :

  • . . .
  • . . MySql Workbench. , .
  • . .

, , , - ...

+4
5

- L1, refresh (Object).

Hibernate Docs

.   ,   -.   .

  • SQL (, )
  • Blob Clob

, , session.setCacheMode(CacheMode.Refresh), L1. , CacheMode L1. Hibernate

CacheMode .

+1

hibernate .

, Hibernate, DEBUG org.hibernate ( TRACE org.hibernate.type, ).

0

? , , .

SessionFactoryWrapper, , ? - , ? , , . , Hibernate.

.

0

, ?

:

https://hibernate.atlassian.net/browse/HHH-9367

https://jira.grails.org/browse/GRAILS-11645

:

http://howtodoinjava.com/2013/07/01/understanding-hibernate-first-level-cache-with-example/

http://www.dineshonjava.com/p/cacheing-in-hibernate-first-level-and.html#.VhZ7o3VElhE

, 1- Hibernates

, - :

userByEmail('foo@bar.com').email != 'foo@bar.com'

@Issue('https://jira.grails.org/browse/GRAILS-11645')
class FirstLevelCacheSpec extends IntegrationSpec {
    def sessionFactory

    def setup() {
        User.withNewSession {
            User user = new User(email: 'test@test.org', password: 'test-password')
            user.save(flush: true, failOnError: true)
        }

    }

    private void updateObjectInNewSession(){
        User.withNewSession {
            def u = User.findByEmail('test@test.org', [cache: false])
            u.email = 'foo@bar.com'
            u.save(flush: true, failOnError: true)
        }
    }

    private User userByEmail(String email){
        User.findByEmail(email, [cache: false])
    }

    def "test first update"() {
        when: 'changing the object in another session'
        updateObjectInNewSession()

        then: 'retrieving the object by changed identifier (no 2nd level cache)'
        userByEmail('foo@bar.com').email == 'foo@bar.com'
    }

    def "test stale object in 1st level"() {
        when: 'changing the object after pulling objects to cache by finder'
        userByEmail('test@test.org')
        updateObjectInNewSession()

        then: 'retrieving the object by changed identifier (no 2nd level cache)'
        userByEmail('foo@bar.com').email != 'foo@bar.com'
    }
}
0

Hibernate.

, c3p0.

, , c3p0.

-1

Source: https://habr.com/ru/post/1589033/


All Articles