-->
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.  [ 3 posts ] 
Author Message
 Post subject: CurrentSessionContext
PostPosted: Sun Aug 06, 2006 7:21 am 
Beginner
Beginner

Joined: Wed Feb 04, 2004 5:21 pm
Posts: 37
I am a bit bewildered about how to best use the new getCurrentSession functionality of SessionFactory. I currently manage the current session myself using thread local storage and it works nicely, but I would rather use Hibernate supplied functionality and rely on SessionFactory rather than my own wrapper around it.

1) You say it is best to use JTA. Yet it seems like I have to waste time learning yet another api, and relying on yet another framework for very little gain. Apparently I will also have to choose between various implementations that will tie me into that implementation. Essentially it sounds preferable to jump without a parachute.

2) Then there is the ThreadLocalSessionContext. Looking at the javadoc it seems like I can't use multiple SessionFactories in my webapp. How do I check if a transaction is open during cleanup so I can commit it without creating a new session?

3) Implement my own CurrentSessionContext. Seems like my only option, but how do I plug it in to the SessionFactory?

Henrik


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 06, 2006 8:35 am 
Beginner
Beginner

Joined: Wed Feb 04, 2004 5:21 pm
Posts: 37
Re 2:

Of course it does support multiple session factories. The usual quality of Hibernate standards shames my suspision.

To clarify:

I want to figure out a way to do something like this on the current session _possibly_ active _perhaps_ with an active transaction. I think that's a much better approach to the ServletFilter logic than always creating a session.


public void closeCurrentSession() throws HibernateException {
Session s = (Session) currentSessionHolder.get();
currentSessionHolder.set(null);
if (s != null) {
Transaction trx = s.getTransaction();
if (trx != null)
trx.commit();
s.close();
}
logger.debug("Closed current session");
}


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 06, 2006 10:28 am 
Beginner
Beginner

Joined: Wed Feb 04, 2004 5:21 pm
Posts: 37
I think I solved it myself.

Call ThreadLocalSessionContext.unbind() and use the Session it may return to check for a transaction.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.