These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Confused while using currentsession method
PostPosted: Fri Jun 01, 2007 3:54 am 
Beginner
Beginner

Joined: Sun Nov 19, 2006 6:18 am
Posts: 28
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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 04, 2007 9:10 am 
Beginner
Beginner

Joined: Sun Nov 19, 2006 6:18 am
Posts: 28
No knows the ans to this prob?? ..Kindly help


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 05, 2007 1:10 am 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
Well the code snippet you gave is different because everything is in the same thread.

So in your post you keep mentioning that in your application you open and close the session. Actually with the currentSession() approach you wouldn't have to explicitly close the session.

I'd be curious to see the code in the three functions and how the threads are spawned.

_________________
Don't forget to rate the reply if it helps..:)

Budyanto


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 05, 2007 4:18 am 
Beginner
Beginner

Joined: Sun Nov 19, 2006 6:18 am
Posts: 28
The code i had posted was to indicate my understanding of open and current session method. In my application code i do session.getTransaction().commit(),so it closes my session.
The following code depicts what i m doing in my application

class SchedulerAction()
{

public void callSync()
{

SmDbreplication manager=new SmDbreplication ();
manager.replicate();

//spawn new thr
DialPlan d=new DialPlan();
d.start();

now open session here// this is where i get exception.In the previous threads i do not get session close exception
}

}

class SmDbreplication ()
{

// spwan n no of threads here
void replicate()
{
//in a loop
InsertionWorkThread thr=new InsertionWorkThread ();
thr.start();

//wait for all threads to return
}
}


class InsertionWorkThread ()
{

public void run()
{
//open session
//store data

//commit session
}

}

class DialPlan()
{

public void run()
{
//open session
//store data

//commit session
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 11, 2007 1:54 pm 
Beginner
Beginner

Joined: Sun Nov 19, 2006 6:18 am
Posts: 28
Hi,
I understood the difference by going through the hibernate source code and it helped me solve my problem also.

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.