How can I stop Java or Hibernate Caching

18,450

Solution 1

The Hibernate 1st level cache can not be disabled (see How to disable hibernate caching). You need to understand Hibernate's session cache if you want to force Hibernate querying to the database.

Lokesh Gupta has a good tutorial on http://howtodoinjava.com/2013/07/01/understanding-hibernate-first-level-cache-with-example/

  1. First level cache is associated with “session” object and other session objects in application can not see it.
  2. The scope of cache objects is of session. Once session is closed, cached objects are gone forever.
  3. First level cache is enabled by default and you can not disable it.
  4. When we query an entity first time, it is retrieved from database and stored in first level cache associated with hibernate session.
  5. If we query same object again with same session object, it will be loaded from cache and no sql query will be executed.
  6. The loaded entity can be removed from session using evict() method. The next loading of this entity will again make a database call if it has been removed using evict() method.
  7. The whole session cache can be removed using clear() method. It will remove all the entities stored in cache.

You should therefor either use the evict() or clear() method to force a query to the database.

In order to verify this, you can turn on SQL output using the hibernate.show_sql configuration property (see https://docs.jboss.org/hibernate/orm/5.0/manual/en-US/html/ch03.html#configuration-optional).

Solution 2

Have you tried disabling the cache in the database itself?

I believe that Hibernate first and second level caches are Hibernate specific, but the database will still cache under the hood.

MySQL - force not to use cache for testing speed of query

Share:
18,450
Neil
Author by

Neil

• 3+ years of IT Experience in Analysis, Design, Development, Testing, Deployment and Implementation of Multi-Tier distributed applications using Java, J2EE Technologies. • 1 year Erlang Development, analysis and testing • Good experience on net setting and Cisco routers setting • Good experience on DevOps tools, like Jenkins, Ansible. etc • Good understanding of design patterns and n-tier architecture. • Excellent Java development skills using J2EE, Servlets, JSON, JMS, JDBC, and Java Beans. • Strong front-end UI development skills using scripting languages like JSTL, HTML5, JavaScript, JQuery and CSS. • Experience on XML and parsing methodologies like DOM and SAX. • Experience in Service Oriented Architecture (SOA) and developing Web services using SOAP, REST, XML, WSDL, JAXP XML Beans. • Experience on security design and development using Asymmetric cryptograph, Kerberos Protocol, Spring Security • Extensive experience in design, development and implementation of Model-View-Controller frame works using Sprint boot, Struts and Spring MVC. • Extensive experience in development and implementation of ORM framework Hibernate/ Hibernate with Spring Data Access. • Strong database skills in DB2 and MySQL. • Expertise in programming with SQL, PL/SQL and Stored Procedures. • Experienced with Java Multithreaded programming to develop multithreaded modules and applications. • Expertise in designing applications using various J2EE design patterns like Singleton, Value Object, Data Access Object, Factory, Session Façade and Business Delegate Service Locator etc. • Expertise in using and configuring various web & application servers like GlassFish, Apache Tomcat, Jetty and JBoss. • Proficiency in programming with different Java IDE's like IntellJ, Eclipse and Netbeans. • Extensive experience in Linux, UNIX, Shell scripting. • Used log4J for application logging and notification tracing mechanisms. • Worked with various Version Control Tools including GitHub, Subversion, Tortoise SVN and. • Experience on NoSQL DataBase including MangoDB, ElasticSearch

Updated on June 04, 2022

Comments

  • Neil
    Neil almost 2 years

    I have an app to retrieve data from Database, and I monitor the time my app takes to retrieve data.

    But I have an issue when I use the same data input set to retrieve data with my app, the second time retrieving will take much less time.

    I assume Java or Hibernate has some cache or temp file to save the data, so second time run will be fast, but I don't want it happen. I need monitor the time it actually takes, not the time retrieve from cache or temp file.

    I tried to forbid the cache and temp file generate in Java control Panel, I tried to disable the hibernate cache(first level or second level). But these are still not solve my problem. The second time run still takes less time than it should take.

    Any idea the reason caused the second time run faster? it just a simple app to retrieve data from DB

  • Neil
    Neil over 8 years
    Not I haven't, I will try now, Thank you
  • FrankGod
    FrankGod almost 2 years
    If we query same object again with same session object, it will be loaded from cache and no sql query will be executed., in this case the data in the cache could be different in the db?
  • boskoop
    boskoop almost 2 years
    Yes, this is the case. As for almost all caches, the data in the cache can differ from the actual value. But also if you were to read again from the database, the data could change in your db between your read an the commit on your transaction (depends also on your transaction isolation level). So you will have to find anyway a strategy to prevent the "lost update" problem in your data (e.g. by using pessimistic or optimistic locking).