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&useUnicode=true&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.....
|