-->
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.  [ 2 posts ] 
Author Message
 Post subject: Static vs Non-Static Sessions
PostPosted: Wed Jun 23, 2010 9:21 am 
Newbie

Joined: Fri Apr 02, 2010 11:23 am
Posts: 2
I've recently taken on the database/hibernate side of our project and am having terrible trouble understanding some fundamentals of our design regarding the use of managed sessions.

We have a util class containing a static session that is only initialised once. Retrieval of the session is used by every DAO in the system via a static method getBoundSession(). The application runs 24/7. Is this a common design?

One of the benefits which is extremely useful, is that lazy attributes/collections on domain objects can be used throughout the business logic tier since the session is always open. Another benefit is that the objects retreived will stay cached within the session.

I feel we must be using Hibernate in the wrong way, it just doesn't seem right to have a single permanently open session. Also it causes problems when separate threads are using the util class, hence sharing the session. On the flip side I can't find a way to achieve the above benefits (particularly the first) with a different design. Can anyone shed any light on this?

Thanks

James


Top
 Profile  
 
 Post subject: Re: Static vs Non-Static Sessions
PostPosted: Wed Jun 23, 2010 2:16 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
OMG! I am surprised that even works at all.

First, a session is not thread-safe and should not be used by more than one thread at once. The typical result is an exception saying something about "possible non-threadsafe access". http://docs.jboss.org/hibernate/stable/ ... ssion.html

Second, managing transactions would be really, really difficult with only one session. For example, if you need to rollback due to an exception the session must also be closed and not used again since the data that is cached in the session may be in an inconsistent state. http://docs.jboss.org/hibernate/stable/ ... exceptions

So, the question is what you should do instead. It depends on the type of application. If it is a web application a common design is the open-session-in-view pattern. http://community.jboss.org/wiki/OpenSessioninView

There is also a lot more information to find on the Hibernate wiki. http://community.jboss.org/en/hibernate

I would also recommend reading a book about Hibernate. http://www.hibernate.org/docs/books.html
Personally I found the Java Persistence with Hibernate book to be a real life-saver.


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