-->
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.  [ 6 posts ] 
Author Message
 Post subject: Session Management
PostPosted: Mon Sep 22, 2008 6:50 pm 
Newbie

Joined: Mon Jan 28, 2008 1:55 pm
Posts: 19
Hibernate version:
3.0

Name and version of the database you are using:
Microsoft SQL Server 2005

My software has a Flex front ended and a Java back end which uses Hibernate 3 to manipulate my Microsoft SQL Server 2005 database. Everything is deployed within the Tomcat 6.0 server. The software has extremely poor use of sessions/transactions and I'd like to know the best implementation.

Currently I am thinking of binding sessions to the thread using:

Code:
      <property name="hibernate.current_session_context_class">thread</property>
      <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>


Then each DAO method would have the following format:

Code:
Session session = HibernateUtil.getCurrentSession();
session.getTransaction().beginTransaction();
//DO SOME DATABASE WORK
session.getTransaction().commit();


Is this implementation good? Is this approach thread safe? Can it be made more efficient? I'm avoiding using the openSession call because I figure getCurrentSession is more efficient?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 22, 2008 10:35 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
That code is transactional, but not thread-safe.
Because sessions themselves are not thread-safe, unless you wrap that code in something that makes them so.
And yes, creating a transaction in that way, every time a DAO operation occurs, is somewhat of an overkill, because it doesn't take advantage of larger inclusive transactions that might be already available in the database conversation.

My advice would be, work with some managed environment, so that you don't have to deal with session creation or transactions programatically.
I use Spring, which, since Hibernate 3, makes use of contextual sessions, allows for cleaner code, and lets you do a lot via configuration as opposed to code.

Spring will circumscribe the thread-safety issue, but not eliminate it. You would still have to do some manual threading code (or work as shortly and statelessly as possible) in those places of your application that risk access by multiple threads simultaneously.

I plan to write a tutorial on this, but didn't have the time yet. Post me this question at hibernar.org if you want, and I will answer you in more detail in a couple of days.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2008 10:07 am 
Newbie

Joined: Mon Jan 28, 2008 1:55 pm
Posts: 19
I'm certainly interested in hearing what you have to say about using Spring and Hibernate together. My question seems to be absolutely fundamental to using Hibernate and because of that I would assume that you can solve this problem without introducing another framework? Does anybody out there have such a solution or does it simply wind up being easier/cleaner to use Spring?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2008 2:00 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
The short answer is no.
I mean, Hibernate ramarkably washed its hands about how you manage your sessions and keep them alive in an enterprise application (that is, unless you also use JBoss and pay them the big bucks).

The utility class they give at page 7 (1.2.5 Startup and helpers) reads from the configuration file and creates a SesionFactory every time, so it is not very useful, except for the most basic client code.

If you have DAOs, etc already in your application, you can create some sort of utility class, containing a ThreadLocal instance, which all DAOs would extend.

However, that utility class would end up doing pretty much what you originally described (obtaining /opening a session from the ThreadLocal, opening/closing a transaction) for each database operation.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2008 2:05 pm 
Newbie

Joined: Mon Jan 28, 2008 1:55 pm
Posts: 19
Okay Spring it is! I posted my question on your form. For now I'll research this on my own but I'm looking forward to your tutorial a great deal. I'd really appreciate any information you can provide on the subject. Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2008 6:14 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
OK, it took me a couple of days, but the Spring+Hibernate "tutorial" is ready here.

http://hibernar.org/articulos_en/spring ... xample.php

It is simply a bare-bones Spring+Hibernate application, with all the necessary files deconstructed by me step-by-step.

_________________
Gonzalo Díaz


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