-->
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: Correct manage Transaction
PostPosted: Wed Feb 20, 2008 7:46 am 
Newbie

Joined: Wed Feb 20, 2008 7:15 am
Posts: 3
Hi all,
In my current project I am using hibernate in the persistence layer.
I used to do these tasks with EJB 2.1. Container Managed Transaction. And I used to define the scope of transacitions in deployment descriptors.
But now, I have to do these things.

I would like to know the best way to manage the transaction with hibernate.

I suggest the next one:

Code:
try {
   session = PersistenceComponent.openSession();
   session.beginTransaction();
} catch(MyApplicationException1 e) {
   if(session != null ){
      try {
         session.rollbackTransaction();
      } catch(Exception e1) {            //         }
   }
} catch(MyApplicationException2 e) {
   if(session != null ){
       try {
         session.rollbackTransaction();
      } catch(Exception e1) {         //      }
   }
} catch(Excpetion e) {
   if(session != null ){
      try {
         session.rollbackTransaction();
      } catch(Exception e1) {         //      }
   }
} finally {
   if(session != null ){
      try {
         session.close();   
      } catch(Exception e) {         //      }
   }
}



Whould the last catch block better with throwable?

Thank you in advance.


Top
 Profile  
 
 Post subject: Re: Correct manage Transaction
PostPosted: Wed Feb 20, 2008 1:15 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
I don't think you need the exception at the end. For such abstracts you could also look at the Spring framework's code. Is this code going to be inside a container? What is your transaction management strategy?



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 20, 2008 1:32 pm 
Newbie

Joined: Wed Feb 20, 2008 7:15 am
Posts: 3
Well,
the application is almost done and I have to review the code with transactions. In order to avoid problems.

The application goes inside an j2ee application server but it does not use any of the services a j2ee application server has.

The main frame works used are JSF and Hibernate.
Transaction management is done by the programmer. As I have showed in the example code.

I have added the catch(Exception e) because I want to do rollbak if I get a RuntimeException or subclass. Because, in this case nobody will do the rollback for me.

Please, any suggestion...

Note: I can't use Spring now.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 20, 2008 2:31 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Since you are catching application exceptions here I assume there is some logic code after begin transaction too so you will need to commit the transaction at some point too. you are right with the rollback thing. As for the Spring I am suggesting that you take a look at their source code and compare your version with theirs since that's a working example.

By the way, what is the hibernate transaction settings here?



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 21, 2008 5:45 am 
Newbie

Joined: Wed Feb 20, 2008 7:15 am
Posts: 3
Hi,
the only programmatic transaction management example I saw quickly in the spring documentation is:

Code:
try {
    // execute your business logic here
} catch (MyException ex) {
    transactionManager.rollback(status);
    throw ex;
}
transactionManager.commit(status);


I think it is not enought in my case because I also define the boundary of the transaction:

Code:
try {
   .beingTransaction()
logic code
   .commitTransaction()

} catch(....) {
   doRollback....
} finally {
....
}


I think Spring is much more similar to CMT of J2EE.

About the hibernate settings in my environment:

Code:
<param id="hibernate.show_sql" value="true" />
<param id="hibernate.cache.use_second_level_cache" value="false" />
<param id="hibernate.cache.use_query_cache" value="false" />

<param id="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<param id="hibernate.max_fetch_depth" value="3"/>
<param id="hibernate.bytecode.use_reflection_optimizer" value="true"/>
<param id="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
<param id="hibernate.connection.url" value="xxxx"/>
<param id="hibernate.connection.username" value="neoflowdb2"/>
<param id="hibernate.connection.password" value="xxx" desc="pass must be established"/>


Any other suggestion?

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 21, 2008 2:42 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
This would be enough:

Code:

session = PersistenceComponent.openSession();

try {   
   session.beginTransaction();

   // logic goes here....

  session.getTransaction().commit();
} catch(Throwable t) {

            try {
                if (session.getTransaction().isActive()) {
                    session.getTransaction().rollback();
                }
            } catch (Throwable rollback_exception) {
                  // log it here whatever.
            }

throw t; // it's a bad  idea to consume a business logic exception but you see what works for your system.

} finally {
      try {
         session.close();   
      } catch(Exception e) {         //      }
   }
}


and at the same time you need to tell hibernate what transaction factory to use. In you case I would say a org.hibernate.transaction.JDBCTransactionFactory is good enough.


Farzad-


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.