-->
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.  [ 13 posts ] 
Author Message
 Post subject: Question about Transaction Semantics
PostPosted: Mon Oct 23, 2006 3:28 am 
Newbie

Joined: Sat Oct 21, 2006 10:51 pm
Posts: 7
I'm new to hibernate, just arrived from J2EE/CMT-EntityEJBs. So far I've liked hibernate, but I'm a bit confused about transaction semantics. Apollogies in advace if this is a silly question.

I have a web app, and thus using the session-per-request pattern. In this app I have to deal with non-trivial bussiness rules (validations, calculations, etc) with something like:
Code:
public String action_handler() {
  // this is a Java Studio Creator App
  MyBussinessClass.bussinessMethod1();
  MyBussinessClass.bussinessMethod2();
  ....
}



In this case, each of the bussines methods do a getCurrentSession(), then beginTransaction() and end in commit()/rollback(). Now, is this correct? I mean, is it possible in the same session do a beginTransaction/commit several times? Will this be done in two sepparated transactions? How can I work with more than one transaction in one session? Is the last question related to the session-per-conversation pattern?

Thank you in advace.
Antonio.


Top
 Profile  
 
 Post subject: Nested Transactions?
PostPosted: Mon Oct 23, 2006 4:13 am 
Newbie

Joined: Sat Oct 21, 2006 10:51 pm
Posts: 7
Also, I've seen that in session-per-request nested transactions can't be used (like in CMT-EntityEJB - transaction-requires-new).

My confusion raised when I tryied to re-use bussiness method within another bussiness methods. Eg.
Code:
void doSomeProcess(Long id) {
  Session session = HibernateUtil.getCurrentSession();
  try {
     session.beginTransaction();
     // Load persistent object
     MyDTO dto = (MyDTO)session.get(MyDTO.class, id);
     // calculate some value for some property
     SomeValue someValue = calculateSomeValue();
     dto.setSomeValue(someValue);
     // do more things
     ...
     session.getTransaction().commit();
  catch (....) {
       // handle exceptions
   }

     


If calculateSomeValue() has to be called from the UI Layer individually, it has to call getCurrentSession() and to commit/rollback as needed; but when called within doSomeProcess() can no be placed between the transaction demarcation boundaries because when doing the final comit, it finds the session closed. It works fine when placed outside the Tx boundaries.

In JTA with CMT, the session bean's remote method was part of a global transaction, and when needed context.setRollbackOnly() can be called to force rollback. Didn't have to worry about transaction boundaries. :s

Any hint or pointer to documentation will be very appreciated.
Thank you in advace.
Antonio


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 23, 2006 7:48 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
You can use CMT with Hibernate too, see docs for details.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 23, 2006 3:11 pm 
Newbie

Joined: Sat Oct 21, 2006 10:51 pm
Posts: 7
baliukas wrote:
You can use CMT with Hibernate too, see docs for details.


I'm moving from a web app with bussines logic coded in EJB Session Beans to end up with a layered web application. At the end, won't be any more EJB Session Beans/EntityBeans so I can get rid of the EJB Container. As far as I remember CMT si available only to EJB Session Beans. I've seen the docs, but I need to look for a more detailed example where UserTransaction is used.

Been able to run outside a J2EE/JavaEE application server will be a plus to my app.

Thank you.
Antonio.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 24, 2006 2:59 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
I prefer to demarcate transactions in abstract "command" or "action" :


Code:
public abstract class Action {

   protected abstract void execute();


   public final void run(){
       
     //BEGIN TRANSACTION
           
        execute();
       
     //END TRANSACTION, CLOSE SESSION     


   }


}


Code:

public final class MyAction extends Action{


  protected void execute(){

  // this is a Java Studio Creator App
  MyBussinessClass.bussinessMethod1();
  MyBussinessClass.bussinessMethod2();

  }

}


Code:

public final class MyAction2 extends Action{


private Long id;

  public MyAction2(Long id){
    this.id = id;
   }

  protected void execute(){

  Session session = HibernateUtil.getCurrentSession();

   
     // Load persistent object
     MyDTO dto = (MyDTO)session.get(MyDTO.class, id);
     // calculate some value for some property
     SomeValue someValue = calculateSomeValue();
     dto.setSomeValue(someValue);
     // do more things
     ...


  }

}


some static utility is fine too:

Code:
static void excuteAction( Action action){

    //BEGIN TRANSACTION
           
        action.execute();
       
     //END TRANSACTION, CLOSE SESSION     




}


it helps, if you need compound commands:


Code:

public final class CompoundAction extends Action{


   public CompoundAction(Action actions[]){
        ....
   }

  protected void execute(){

    for(Action action: actions ){
        action.run();// no transaction demarcation in base class,"excuteAction" demarcates transactions
    }

  }

}



This is classic "Command" design pattern.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 25, 2006 12:47 am 
Newbie

Joined: Sat Oct 21, 2006 10:51 pm
Posts: 7
Baliukas,

Thank you for your answer, it was useful information. I'm reading about this, and I think other question has been answered. It is nothing wrong with doing a
Code:
  Session session = HibernateUtil.getCurrentSession();
  session.beginTransaction();
  // ...do things
  session.getTransaction().commit();

  session.beginTransaction();
  // ...do things
  session.getTransaction().commit();


wich is fine taking care of avoid nesting beginTransaction()/commit() calls.

Thanks again.
Best regards
Antonio.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 25, 2006 1:48 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
It is a long story, read "11.1. Session and transaction scopes". It depends, but I prefer to close session after commit to avoid concurrency issues (see "AutoCloseSessionEnabled" configuration parameter too).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 27, 2006 7:16 pm 
Newbie

Joined: Sat Oct 21, 2006 10:51 pm
Posts: 7
baliukas wrote:
It is a long story, read "11.1. Session and transaction scopes". It depends, but I prefer to close session after commit to avoid concurrency issues (see "AutoCloseSessionEnabled" configuration parameter too).


I've read about the session-per-request pattern that the session object must be discarded after the commit() operation, because of the chance of having stale objects. Even the pattern can be extended to use 2 transactions, but in a session that spans 2 user requests.

As I mentioned earlier, the application is being developed in JavaStudio Creator, who in turn gives the JavaServer Faces framework. Other frameworks include a kind of plugin or module to handle the open-session-in-view pattern, and I think this must be the correct way to have it done in the JSF framework.

With that being said, if there is a chance of having several transactions in a session (loading info, doing bussiness, rendering) bounded to a thread, it will simplify a lot my migration path. Can you be so kind of elaborate a bit the "it depends..." part of your answer?

Thank you in advance.
Antonio.


Top
 Profile  
 
 Post subject: could not locate TransactionManager when i am using Containe
PostPosted: Sat Oct 28, 2006 12:29 pm 
Newbie

Joined: Sat Oct 28, 2006 9:36 am
Posts: 7
Location: Hyderabad
baliukas wrote:
You can use CMT with Hibernate too, see docs for details.
Code:
when i am using container managed transactions i am getting Hibernate Exception could not locate TransactionManager and NameNot foundException[/url][/list]

_________________
hazi pasha.mohammed


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 30, 2006 1:18 pm 
Newbie

Joined: Sat Oct 28, 2006 9:36 am
Posts: 7
Location: Hyderabad
baliukas wrote:
You can use CMT with Hibernate too, see docs for details.
[/url][/list][/list][/code][/quote][/u][/i][/b]

_________________
hazi pasha.mohammed


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 30, 2006 7:07 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
I tend to use CMT with SLSB (Local or remote as use cases require). Most of the time it is in the form of a the command pattern.
Transaction manager is controlled by the container. Do see the documentation for appropriate configuration.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 31, 2006 1:09 pm 
Newbie

Joined: Sat Oct 28, 2006 9:36 am
Posts: 7
Location: Hyderabad
david wrote:
I tend to use CMT with SLSB (Local or remote as use cases require). Most of the time it is in the form of a the command pattern.
Transaction manager is controlled by the container. Do see the documentation for appropriate configuration.
documenation means Hibernate or Orion App Server.because i am using Orion App Server in OracleJDeveloper please give me code of hibernate.cfg.xml and SessionFactory.getCurrentSession() and closeSession();

_________________
hazi pasha.mohammed


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 31, 2006 1:15 pm 
Newbie

Joined: Sat Oct 28, 2006 9:36 am
Posts: 7
Location: Hyderabad
david wrote:
I tend to use CMT with SLSB (Local or remote as use cases require). Most of the time it is in the form of a the command pattern.
Transaction manager is controlled by the container. Do see the documentation for appropriate configuration.
documenation means Hibernate or Orion App Server.because i am using Orion App Server in OracleJDeveloper please give me code of hibernate.cfg.xml and SessionFactory.getCurrentSession() and closeSession();

_________________
hazi pasha.mohammed


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