Andrews Andrews - 1 year ago 73
Java Question

Hibernate retrieving/showing deleted objects even though it is commited

I was using Hibernate 3.3 and recently upgraded to version 4.3.11.
After moving to this version, I started noticing a strange problem.

  1. Objects that I delete shows up every now and then.

  2. Objects that I added newly show up only rarely.

It looks as if the current state of the db is shown only on the exact connection pool being used to delete it.

Below is the snippet of the code used to save

Transaction tx = null;

try {
Session session = hibSessFact.currentSession();
tx = session.beginTransaction();;
}catch(Exception e){
} finally {
System.out.println("session closed");

The below is how the data is retrieved

Session session = hibSessFact.currentSession();
Criteria crit = session.createCriteria(MyObject.class);
List objList = crit.list();
System.out.println("returned list");
return objList;
}catch(Exception e){

P.S.: The same happens with object retrieval when new objects are added. It shows up only intermittently, exactly every 3rd request.

Update: ThreadLocal is used to create session using HibernateSessionFactory.

Answer Source

Instead of currentSession used to get session make use of openSession() as below:

Session session = hibSessFact.openSession();

Otherwise, add the below property in hibernate configuration.

<property name="hibernate.current_session_context_class">thread</property>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download