Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Confused while using currentsession method  RSS feed

Akshay Ajmani
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Current Session method is used in along with
<property name="current_session_context_class">thread</property>

Suppose in thread t1
1)I create a session using currentsession() and add an object using that session and then close that session.
2)In the same thread if i try to create a session then will i get sessionException Session is already Closed.

I tried the following code snippet and it worked fine
public static void main(String[] a) throws Exception
Session session=HibernateUtil.currentSession();
SmHostBean bean=new SmHostBean();
bean=new SmHostBean();


Note:HibernateUtil is my custom class here.HibernateUtil.currentsession() in turn calls sessionFactory.getCurrentSession().

The reasion I am asking this question is because in my web application i m facing this problem of Session is Closed

what i am doing in my application is

1)From the User Interface I click on a button which starts a process in which i call three functions one after the other.
In all the three functions i am opening and closing the session.(the first two functions are called in 2 new threads i.e i spwan 2 threads after clicking on the button on the UI)
But in the third function, if i try to open a session (using currentsession()), i get an exception saying Session is already closed.

Is my code snippet above depict the same thing that i am trying to do in my web application.

Kindly help
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!