• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused while using currentsession method

 
Akshay Ajmani
Greenhorn
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();
session.beginTransaction();
SmHostBean bean=new SmHostBean();
bean.setId(111L);
bean.setIpaddress("1");
bean.setPort("11");
session.save(bean);
session.getTransaction().commit();
bean=new SmHostBean();
bean.setId(112L);
bean.setIpaddress("1");
bean.setPort("11");
session=HibernateUtil.currentSession();
session.beginTransaction();
session.save(bean);
session.getTransaction().commit();

}

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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic