-->
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: Exception handling idiom
PostPosted: Thu Sep 23, 2004 8:39 am 
Newbie

Joined: Thu Sep 23, 2004 8:34 am
Posts: 2
Hibernate version: 2.1

The hibernate documentation proposes following Exception handling idiom:

----------------------------

Session sess = factory.openSession();
Transaction tx = null;
try {
tx = sess.beginTransaction();
// do some work
...
tx.commit();
}
catch (Exception e) {
if (tx!=null) tx.rollback();
throw e;
}
finally {
sess.close();
}

-----------------------------

However this isn't possible, cause tx.rollback() throws a checked HibernateException, so I have to catch the HibernateException in the catch-clause too.

Is this a mistake of the documentation?
It would be nice, to throw only an unchecked Exception from rollback().

Greetings,
Maarten Donders


Top
 Profile  
 
 Post subject: Re: Exception handling idiom
PostPosted: Thu Sep 23, 2004 10:21 am 
Newbie

Joined: Mon Sep 20, 2004 10:27 am
Posts: 7
Hi Maarten:


Code:
Session sess = factory.openSession();
Transaction tx = null;
try {
       tx = sess.beginTransaction();
       // do some work
       ...
       tx.commit();
}
catch (Exception e) {
      if (tx!=null) {
           try{
               tx.rollback();
           }
           catch(Exception e){
                 log.warn("Exception Occured when rolling back" ,e);
           }
}
finally {
          sess.close();
}




It could be done this way too. I think it really depends on how you wanted your layer to behave.

What I feel about the documentation is , it is meant to give you an idea how to implement.

Anyways Hibernate guys correct me if I am wrong

_________________
Rgds,

--Siva Jagadeesan
Application Developer
ThoughtWorks, Inc


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 23, 2004 11:13 am 
Newbie

Joined: Thu Sep 23, 2004 8:34 am
Posts: 2
Hi Siva,

I just noticed in the samples that it seems to be common to let every method which uses hibernate throw a HibernateException. Therefore it isn't necessary to catch the HibernateException in the catch-clause.

I don't like that style, cluttering all my methods with HibernateExceptions, but I'll find my way around that.

Thank you for answering,
Maarten


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.