• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Transaction please help

 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
I had a doubt:
try{
startTransaction();
....insert 1;
....AAAAAAAAAAA......
....insert 2;
}catch(SQLException e){
rollback()
}
if insert 1 fails then rollback w'd work.
Suppose there's a NullpointerException at 'AAAAAAA' then
what w'd happen? the catch w'd'nt "catch" this exception.
Yes one option
in the end catch(Exception e)
then rollback.
But am not convinced.
I'm confused, will my stuff rollback w/o catching
java.lang.Exception?
thanks a lot,
karthik.
 
kichu kichu
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai karthik,
If an exception occurs,then your rollback will not occur.
Now if u want your rollback to occur regardless of whether an exception occurs or not,then put rollback inside finally{}.
Try it and get back to me

regards
kichu
 
Raja Shekhar
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi karthik Guru

M also facing the same problem...

if u got the solution
Plz Let Me know...
bye,bye
Raj
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One technique is

You can catch and rethrow RuntimeException with impunity; since it's unchecked, this won't clutter up your method's throws clause. In addition to this, you need a catch block for every checked Exception (such as SQLException), i.e. for every exception in your method's throws clause.
If you have a large number of checked Exceptions in your throws clause, you could catch (Exception), rollback, wrap the exception inside a MyGenericDataAccessException and throw that (this is called a nested exception -- see e.g. RemoteException for examples of this technique). That cleans up your throws clause as well.
Note that if you use EJBs, the EJB container already provides similar services for you -- if your bean throws a RuntimeException the container will roll back the transaction for you.
- Peter

[This message has been edited by Peter den Haan (edited May 17, 2001).]
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter den Haan:
[B]One technique is

yes at the moment this is what i'm doing. yeah infact we have our DatabaseManager which wrap exceptions into a single exception and throw it back.
..karthik
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by kichu kichu:
Hai karthik,
If an exception occurs,then your rollback will not occur.
Now if u want your rollback to occur regardless of whether an exception occurs or not,then put rollback inside finally{}.
Try it and get back to me

regards
kichu

I can't do that right since i'm not sure if an exception occured/ everythign went through fine bcos finally will be executed in any case.
..karthik.
 
Raajesh Chandran
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!!
1.while inserting U got the exception .what does it mean.
The values are not inserted.ok
2.what do u mean by rollback.just Undo the part which done lastly after commit.ok.

3.U R getting the exception while inserting means not inserted then how U rollback that one.
Hope this is going there.
chandran..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic