-->
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: Hibernate Pattern
PostPosted: Fri Oct 17, 2003 12:42 pm 
Regular
Regular

Joined: Tue Oct 14, 2003 11:11 pm
Posts: 62
Location: Brasil/Curitiba
I'm using Struts and the Thread Local Session pattern...

In the begin of the request I open a Transaction, and after in the DAO classes I retrieve the session,
but I never flush or close the session in the DAO...

Then, in the end of request I commit or rollback the Transaction and close the Session.

Is This the right way ? Can I save/update many tables in the same request with the same Hibernate Session ?

Struts Action
Code:
Session session = null;
Transaction trans = null;
try {
   ActionForward forward = null;

   session = Hibernate2ThreadLocalSession.currentSession();
   // Open Transaction
   trans = session.beginTransaction();
   
   // method in the subclass
   forward = performAction(mapping, form, request, response);
      
   // COMMIT
   trans.commit();

   return forward;
}
   catch (Exception e) {
      if (trans != null) {
         trans.rollback();
      }
      throw e;
   }
   finally {
      try {
         // CLOSE SESSION
         Hibernate2ThreadLocalSession.closeSession();
      }
      catch (HibernateException e1) {
         e1.printStackTrace();
      }
   }


DAO:
Code:
void test()
{
   session = openSession();
   // do something

   // don't flush or close the Session here...
}

_________________
Ricardo Lecheta


Top
 Profile  
 
 Post subject: fine
PostPosted: Fri Oct 17, 2003 2:19 pm 
Regular
Regular

Joined: Tue Sep 16, 2003 11:35 am
Posts: 93
Location: San Francisco, CA
Yeah that should work fine. Transactions can cover updates to many tables. The only thing I'd warn you about it that your Struts action logic might need some way of rolling back the transaction itself and in your current code there is no way for the action to rollback the transaction, other than to throw an exception. After originally having a set up very similar to your using a Filter, I eventually decided I had to have each Action create and manage its own transactions. Of course this is just advice from my own projects so your current code might be fine for your situation.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 17, 2003 3:28 pm 
Regular
Regular

Joined: Tue Oct 14, 2003 11:11 pm
Posts: 62
Location: Brasil/Curitiba
thanks !

_________________
Ricardo Lecheta


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.