Click here to Skip to main content
15,899,937 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.

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'.

HTML
<bean id="sessionFactory" scope="singleton"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="configLocation" value="classpath:hibernate.cfg.xml" />
	</bean>



Problems are as following.

1. I run my project.

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

http://localhost:8080/myproj

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
Updated 12-Mar-13 22:18pm
v2

1 solution

Hello,

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.
XML
<!-- Enable the second-level cache  -->
<property name="hibernate.cache.provider_class">
    net.sf.ehcache.hibernate.EhCacheProvider
</property>
<property name="hibernate.cache.region.factory_class">
    net.sf.ehcache.hibernate.EhCacheRegionFactory
</property>
<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>

Regards,
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900