0

I am implementing WebService with Hibernate to write/read data into database (MySQL). One big issue I have was when I insert data (e.g., USER table) via one JVM (example: JUNit test or directly from DBUI suite) successfully, my WebService's Hibernate running on separate JVM cannot find this new data. They all point to the same DB server. It is only if I had destroyed the WebService's Hibernate SessionFactory and recreate it, then the WebService's Hibernate layer can read the new inserted data. In contrast, the same JUnit test or a direct query from DBUI suite can find the inserted data.

Any assistance is appreciated.

Panini Luncher
  • 639
  • 8
  • 10
  • could it be possible that you have enable 2nd level caching and your second app reading stale data ? – Sajith Silva Dec 31 '13 at 04:37
  • @Sajith, thanks. But I do not think I enable the 2nd level caching. Also, in this scenario I believe I have two separate SessionFactory instances running: One is by the JUNIT JVM where it inserts data; another is by the WebService server where it reads data per method call. – Panini Luncher Dec 31 '13 at 05:20

1 Answers1

0

This issue is resolved today with the following:

  1. I changed our Hibernate config file (hibernate.cfg.xml) to have Isolation Level to at least "2" (READ COMMITTED). This immediately resolved the issue above. To understand further about this isolation level setting, please refer to these:

  2. I ensured I did not use 2nd level caching by setting CacheMode to IGNORE for each of my Session object:

    Session session = getSessionFactory().openSession();
    session.setCacheMode(CacheMode.IGNORE);
    
  3. Reference only: Some folks did the following in hibernate.cfg.xml to disable their 2nd level caching in their apps (BUT I didn't need to):

    <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
    <property name="hibernate.cache.use_second_level_cache">false</property>
    <property name="hibernate.cache.use_query_cache">false</property>
    
Community
  • 1
  • 1
Panini Luncher
  • 639
  • 8
  • 10