Click here to Skip to main content
13,087,566 members (93,331 online)
Rate this:
Please Sign up or sign in to vote.
See more:

My project is a web project based on Struts2 + Spring + Hibernate.
And I'm using MySQL.
I'm defining beans and applying DI( Dependency Injection ).
All beans have 'request' scope, but only org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean instance is 'singleton'.

<bean id="sessionFactory" scope="singleton"
		<property name="configLocation" value="classpath:hibernate.cfg.xml" />

Problems are as following.

1. I run my project.

2. I connected to my project on the web browser.


3. Here display the table rows in my database.

4. Then, I changed a row in the database on PHPMyAdmin.

5. I refresh my page, but the page is showing the unchanged data.

What's the problem?
Please help me.
Posted 12-Mar-13 22:15pm
Updated 12-Mar-13 22:18pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1


Hibernate has two possible places where data can get cached, in the first level cache (the Session) or the second level cache (some third party cache implementation).
The first level cache is always used when you open a Session and load an object into it. You can update this object's properties all you like, its only when the session is flushed or closed (by calling those methods on the session) that the object's properties are persisted to the database.

The second level cache is only used if you explicitly add caching declarations to your mapping files. Otherwise it is not used.

Given this I suspect that you are not properly closing the session and that's why you are able to see the changes. If you have second level cache (EHCache) then use following xml to disable it. Disabling first level cache is not recommended and there is not suitable way to di this. You can use session.evict(YOUR OBJECT) to remove your object from this cache though.
<!-- Enable the second-level cache  -->
<property name="hibernate.cache.provider_class">
<property name="hibernate.cache.region.factory_class">
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_structured_entries">true</property>     
<property name="hibernate.cache.generate_statistics">true</property>


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 24 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100