Andrews
Andrews

Reputation: 985

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();
        session.save(o);
        tx.commit();
    }catch(Exception e){
        e.printStackTrace();
    } finally {
        hibSessFact.closeSession();
        System.out.println("session closed");
    }

The below is how the data is retrieved

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

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.

Upvotes: 0

Views: 347

Answers (1)

Rohit Gaikwad
Rohit Gaikwad

Reputation: 3914

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>

Upvotes: 1

Related Questions