-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate cache refresh problem
PostPosted: Sat Aug 02, 2008 5:47 am 
Newbie

Joined: Sat Aug 02, 2008 5:06 am
Posts: 1
Dear all, I have a problem when using hibernate to connect to mysql.
My application can retrieve data correctly if all changes are made thru' hibernate api inside my application. However, it cannot refresh data correctly if someone changed the DB data manually or using third party tools. Worstly, there is no way to refresh the hibernate cache unless I have restarted my application. I have read thru' many articles and tried many methods, but the problem still cannot be solved. Please help .... thanks.
Here are my code and hibernate config.

hibernate.cfg.xml,

<?xml version="1.0" encoding="UTF-8"?>

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://192.168.1.1:3306/dietasia?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF-8</property>
<property name="hibernate.connection.username">guest</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.pool_size">20</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>

<!-- Note 1, Disable the second-level cache -->
<property name="hibernate.cache.use_query_cache">false</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<mapping class="myapp.entity.Login" />
<mapping class="myapp.entity.Members" />
<mapping class="myapp.entity.Saletransaction" />
</session-factory>
</hibernate-configuration>



Program code,

public class MemberBean implements MemberRemote, MemberLocal {
public List findList(final String searchField, final String condition) {
Session session = null;
List members = null;

try {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

// Note 2
sessionFactory.evictQueries() ;

session = sessionFactory.openSession();

// Note 3
session.clear();
session.setCacheMode(CacheMode.REFRESH);

Criteria crit = session.createCriteria(Members.class);

// Note 4
crit.setCacheMode(CacheMode.REFRESH)
// Note 5
crit.setCacheable(false);

crit.add(Restrictions.like(searchField, "%"+condition+"%"));

crit.setFirstResult(0);
crit.setMaxResults(10);
members = crit.list();
}catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}finally{
if(session!=null) {
session.close();
}
}
return members;
}
}

Remark,
Note 1, 2, 3, 4, 5 are the methods I have applied to try stopping or refresh the cache. But it's not working.....


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 03, 2008 5:12 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
This seems really strange. I have no real answer but I remember that a similar thing happened to me once. I spent a few hours before I found out that I was doing the manual changes in a different database than what Hibernate was using. In my case, a restart of the Hibernate application of course didn't help so maybe you have hit something different.

Another tip is to enable logging (debug-level) to see what is going on inside Hibernate. Be prepared for a huge output though.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.