0

I user Session session=SessionFactory.getCurrentSssion() method in DAO as class level variable in my Struts2 application. I maintain Transaction in struts interceptor class. In may DAO class, when i try to get Data from DataBase sometime i am getting "session closed" exception. In my code for saveOrUpdate() method, i handle Hibernate Exception. In catch block i roll back the Transaction.

please help me, what happens when rollback the transaction if i use getCurrentSession() method.

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
swamy
  • 67
  • 1
  • 4

2 Answers2

2

You are using SessionFactory.getCurrentSession() of Hibernate which is used to create new session & it is managed by Hibernate automatically. It means when you call getCurrentSession(), Hibernate bind this session with local thread which will be accessed anywhere if you set hibernate.current_session_context_class property in your hibernate.cfg.xml file. This property bind your current session to a local thread.

Since, you are using getCurrentSession() method instead of openSession(), your session 
will closed automatically by Hibernate as soon you perform any operation on database &
commit the transaction.

e.g.,

Session session = sessionFactory.getCurrentSession();
Transaction transaction = session.beginTransaction();
Student student = new Student();
...
session.save(student);
transaction.commit();

To recover from this error, you should create session as & when needed using sessionFactory.openSession() method. By doing this you can have full control over session object.

e.g.,

Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
try {
    // do something
    transaction.commit();
} catch (Exception ex) {
    // on error, revert changes made on DB
    if (transaction != null) {
        transaction.rollback();
    }
} finally {
    session.close();
}

More Information :

  1. You can have a good idea Hibernate openSession() vs getCurrentSession()
  2. You can create managed session in Hibernate. Look at this ManagedSessionContext (Hibernate JavaDocs)

What happens when rollback the transaction ?

When you call rollback() method of Transaction it will revert all current changes done
on database. It doesn't have any concern with closing Hibernate session.
Community
  • 1
  • 1
OO7
  • 2,785
  • 1
  • 21
  • 33
  • +1, this is the right answer. I won't delete mine because he is performing the operation from within an Interceptor, and if he won't do it the thread-safe way, the new sessions will be shared. – Andrea Ligios Oct 09 '14 at 21:10
  • @swamy Your welcome. You should accept my answer, bcoz it solves ur problem. – OO7 Oct 27 '14 at 05:20
0

I maintain Transaction in struts interceptor class. [...] sometime i am getting "session closed" exception

Interceptors are NOT Thread-Safe. You are probably doing something wrong in your Interceptor (impossible to be sure until you'll post some code).

Ensure you are not using any object at Class level, because they're thread-unsafe.

Refer to this question for a practical example.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243