-->
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: Why transaction don't rollback when Exception hap
PostPosted: Sun Dec 19, 2004 3:42 pm 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
Hi everyone:

Now I use this pattern to write Hibernate code , I want to know if it is not safe or not correctly.

Code:
      Session session=null;
      Transaction tx=null;
try{
        session=HibernateUtil.currentSession();
        tx=session.beginTransaction();
        ..................................
        .............................
    }catch(Exception e){
          try{
                   tx.rollback();
               }catch(Exception e){
                    e.printStackTrace();
               }
   }finally{
           try{
                   HibernateUtil.closeSession();
               }catch(Exception e){
                    e.printStackTrace();
               }

   }
....................................................................

But if some Exception happened when webapp is running,JBoss is outofmemory frequently.
The image is in the following:
Image

Why? I want to know if the session is not closed when Exception happened. Or the transaction don't rollback after Exception happened ?
The HibernateUtil is the right Hibernate docs's helper class.

Could someone give me any idea? THks!

_________________
You are not alone...


Top
 Profile  
 
 Post subject: The image
PostPosted: Sun Dec 19, 2004 3:43 pm 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
The Image is the Following picture:

http://forum.javaeye.com/download.php?id=854[/list]

_________________
You are not alone...


Top
 Profile  
 
 Post subject: :(
PostPosted: Sun Dec 19, 2004 3:45 pm 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
Sorry,the code should be this:
Code:

Session session=null;
      Transaction tx=null;
try{
        session=HibernateUtil.currentSession();
        tx=session.beginTransaction();
        ..................................
        .............................
        tx.commit();
    }catch(Exception e){
          try{
                   tx.rollback();
               }catch(Exception e){
                    e.printStackTrace();
               }
   }finally{
           try{
                   HibernateUtil.closeSession();
               }catch(Exception e){
                    e.printStackTrace();
               }

   }
....................................................................

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 19, 2004 4:27 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
OutOfMemoryError is an Error and not an Exception.

Use catch(Throwable) to catch them all.

(but note that with an outofmemoryerror you are not guaranteed much - their might not be enough memory to perform the rollback - and then it is up to the db not to commit the transaction..

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: :(
PostPosted: Sun Dec 19, 2004 5:16 pm 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
Thank you Max.

You mean that I should enlarge my Linux Server's memory or Oracle10g's Memory? (The webapp running at Linux redhad and Oracle DB) .

What does "Use catch(Throwable) to catch them all" mean?
I already catch all the possible exception in my Code. How to catch them all? Could you give me some idea? Thanks!

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 19, 2004 5:31 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Look at the inheritance hierachy of Throwable - Exception and Error extends from it. So, catch(Throwable) catches both Exception's and Errors.

Please read up on exception and error handling in java - nothing to do with hibernate ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 19, 2004 5:33 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
and that you java vm runs out of memory means you should get more memory in your java vm....(or check if you have a memory leak ,)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 19, 2004 6:08 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Quote:
session=HibernateUtil.currentSession();
tx=session.beginTransaction();
..................................
.............................
tx.commit();


and what about showing what is under
................
................
?

are you doing massive insert update?

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject: :)
PostPosted: Wed Dec 22, 2004 2:53 am 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
max wrote:
and that you java vm runs out of memory means you should get more memory in your java vm....(or check if you have a memory leak ,)


Thks max. I will change my code to following:

Code:
Session session=null;
      Transaction tx=null;
try{
        session=HibernateUtil.currentSession();
        tx=session.beginTransaction();
        ..................................
        .............................
        tx.commit();
    }catch(Throwable e){
          try{
                   tx.rollback();
               }catch(Exception e){
                    e.printStackTrace();
               }
   }finally{
           try{
                   HibernateUtil.closeSession();
               }catch(Exception e){
                    e.printStackTrace();
               }

   }


But the hibernate's docs all catch HibernateException only. For example,HibernateUtil helper class. Why hibernate's example all catch HibernateException instead of Throwable ?

I will try catch Throwable instead of Exception in the next Application test.
Our hibernate web application is accessed by 20 clients.

Quote:
get more memory in your java vm


What java vm paremeter should I set , I don't know whether 256 is enough. Expecting your suggestion. :)

_________________
You are not alone...


Top
 Profile  
 
 Post subject: :)
PostPosted: Wed Dec 22, 2004 3:03 am 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
anthony wrote:
Quote:
session=HibernateUtil.currentSession();
tx=session.beginTransaction();
..................................
.............................
tx.commit();


and what about showing what is under
................
................
?

are you doing massive insert update?


Hi anthony:


There are a insert and a update opperation between my commit block.

Should I do little database insert or update in one transaction? And do another database opperation in a new transaction?

_________________
You are not alone...


Top
 Profile  
 
 Post subject: :(
PostPosted: Wed Dec 22, 2004 3:09 am 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
anthony wrote:
Quote:
session=HibernateUtil.currentSession();
tx=session.beginTransaction();
..................................
.............................
tx.commit();


and what about showing what is under
................
................
?

are you doing massive insert update?


Another problem:

Should I close hibernate session after commit transaction per time? It is slow if I have many transaction opperation. I think it should be better if I do it the following way:

Code:
Session session=null;
      Transaction tx=null;
try{
        session=HibernateUtil.currentSession();
        tx=session.beginTransaction();
        ..................................
        .............................
        tx.commit();
    }catch(Throwable e){
          try{
                   tx.rollback();
               }catch(Exception e){
                    e.printStackTrace();
               }
   }


And in Servlet Filter ,I close the hibernate session finally.

I don't know whether it is the right way.

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 22, 2004 4:35 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
i'll suggest you go read the chapters about transaction & session handling in applications in Hibernate In Action - they explain all this stuff

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 22, 2004 6:01 am 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
max wrote:
i'll suggest you go read the chapters about transaction & session handling in applications in Hibernate In Action - they explain all this stuff


Yes,I will read it . ;-)
Thks !

_________________
You are not alone...


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.