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.  [ 4 posts ] 
Author Message
 Post subject: Implementing hibernate on legacy systems
PostPosted: Thu Sep 04, 2008 8:24 am 
Newbie

Joined: Fri Jul 18, 2008 10:14 am
Posts: 5
[b]Hibernate version:[3.1.3]

We are planning to start using Hibernate in our browser based Enterprise Application. We are trying to implement it on a small part of our core structures to see how feasible it would be for us to use on the legacy code and database. Few queries regarding the same..

Intitially we wanted to keep one transaction open for all the read operations and keep on opening and closing other transactions for write operations (Since it needs a commit). But, it seems we cant keep two transactions open on the same session. So, we tried opening a new session for write operations, but again we cannot share the dettached object read from session 1 (which is still open) and save it through session 2.

I could read a obj from session 1, detach it by committing the transaction on session 1 and then reuse the object on session 2 by updating it. But, this would mean lot of sessions/transactions opening and closing.

Any suggestions on the basic design or path way to implement hibernate on the existing code?

Would hibernate 3.3 be of any help?

_________________
Somebody tell me..!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2008 8:59 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
There is nothing wrong with having lots of sessions and transactions. If you worry about performance there are "tools" such as second-level cache, connection pooling, etc. to help. Keeping a session alive for too long will lead to filling up the memory with first-level cache entries since everything loaded in a session is kept until the session is closed or explicitly cleared. For webapps the simplest pattern is a session-per-request pattern. This is easy to understand and rarely leads to surprises. There is a good document about this on the Hibernate wiki: http://www.hibernate.org/42.html


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2008 9:24 am 
Newbie

Joined: Fri Jul 18, 2008 10:14 am
Posts: 5
Thnx nordberg.

I went through that document.

So, using a getCurrentSession() and begining and committing a transaction upon it is safe? Are there any issues related to this in multi threaded architecture?

_________________
Somebody tell me..!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2008 1:55 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
No, if you keep it to one session per request there will only be one thread accessing that session. And that reminds me about another problem with your original idea. Sessions are not thread-safe and a single session can only be used by a single thread at a time. If you try to use a single session for reads this means that you have to synchronize all access to it. This is not a good idea in a web environment because there are usually lots of threads executing at the same time. It is also problematic if you use proxies and lazy collections since you may not really know exactly when they are going to be initialized.

I recommend reading as much on the web as you can and maybe also the "Java Persistence with Hibernate" book. The original version of this book (Hibernate in action) saved or project from a complete collapse. After reading it we produced a document with "rules" for our project. We didn't always agree with the book but I think we had good reason for not to. It may or may not be of any interest but here is the link: http://base.thep.lu.se/chrome/site/late ... layer.html


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