my dog learned polymorphism*
The moose likes JDBC and the fly likes Transaction please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Transaction please help" Watch "Transaction please help" New topic
Author

Transaction please help

Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
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

Joined: May 10, 2001
Posts: 59
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

Joined: Apr 02, 2001
Posts: 49
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

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Mar 06, 2001
Posts: 1209
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

Joined: Mar 06, 2001
Posts: 1209
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

Joined: May 04, 2001
Posts: 103
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..


chandran..
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Transaction please help
 
Similar Threads
AutoCommit(False) and rollback bst practise
setAutoCommit(false) not working
Ejb 3.0 rollback doesnot work
Transcation management at DO level using spring jdbc framework
Auto Commit option fails to work