Dear all,
I have a very difficult problem to track down.
I have automatically generated Java classes by using MyEclipse’s Hibernate reverse engineering options. I have a JSP front-end, which allows the user to update and view data from this table.
I'm committing all my transactions etc. as appropriate.
The problem is that whilst the updates work ok, under Tomcat it appears that the data is being cached when I get data using a simple query (see below). If I refresh the web page, I can get different data back from each refresh, some of which is wrong (i.e. old data that is no longer in the database). Running the isame data access code on the command line, only the correct (new) version of the data is returned.
My suspicion is obviously with cached session data, and something about the way this is handled in the webserver environment. Not that I'm not using connection pools or datasources for this - the hibernate.cfg.xml is as follow:
Code:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">xx</property>
<property name="connection.url">
jdbc:sqlserver://localhost\sqlexpress:1433;databaseName=xx
</property>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="hibernate.max_fetch_depth">1</property>
<property name="myeclipse.connection.profile">SQLServer</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.cache.use_query_cache">false</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="hibernate.connection.aggressive_release">false</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!--
thread is the short name for
org.hibernate.context.ThreadLocalSessionContext
and let Hibernate bind the session automatically to the thread
-->
<property name="current_session_context_class">thread</property>
<mapping resource=......
</session-factory>
</hibernate-configuration>
I have tried to do all kinds of things whilst saving my object:
Code:
public void save(Object transientInstance, org.apache.commons.logging.Log log) {
org.hibernate.Session s = getSession();
org.hibernate.Transaction t = s.beginTransaction();
try {
s.save(transientInstance);
s.flush();
t.commit();
s.refresh(transientInstance);
s.clear();
s.close();
……
But nothing works. When accessing the data via the following method, I often get old data:
Code:
private Query getX() {
Session s = getSession();
Query q = null;
try {
q = s.createQuery("from X as e "
+ "order by e.y.name, e.ordernum, e.name");
} catch (HibernateException e) {
e.printStackTrace();
s.close();
}
return q;
}
FYI, I am obtaining the session via the automatically generated HibernateSessionFactory:
Code:
public static Session getSession() throws HibernateException {
[b]Session session = (Session) threadLocal.get();[/b]
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
Finally, the mapping for my Class X is as follows:
Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="enjoy.hibernate.X" table="X " schema="dbo" catalog="xx">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
…
<many-to-one name="Y" class="enjoy.hibernate.Y" fetch="select">
<column name="primary_area_id" not-null="true" />
</many-to-one>
<set name="Z" inverse="true">
<key>
<column name="secondary_area_id" not-null="true" />
</key>
<one-to-many class="enjoy.hibernate.Z" />
</set>
…
</hibernate-mapping>
I am using SQL Server, but I have no reason to believe it would be any different using a different database.
I’ve looked at many forums, e.g. the posting at
http://forum.hibernate.org/viewtopic.php?t=968417&highlight=cache, but I cannot see an answer to my problems.
Has anybody got any suggestions?
Cheers
Jonny