Meaningless Drivel is fun!*
The moose likes JDBC and the fly likes Transactions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Transactions" Watch "Transactions" New topic
Author

Transactions

Sumukh Deshpande
Ranch Hand

Joined: Feb 17, 2008
Posts: 87

Hello,

I have a question related to Java Transaction.

I am aware of how do I rollback transaction when any of the SQL statement in set of statements fail.

But, I would like to do something like this:

Suppose I have 2 INSERT statements and 1 UPDATE statement.
If first INSERT fails then I want to rollback but if second INSERT fails I still want to commit.

Can anybody please tell me how do I do it?

Thanks in advance.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Transactions are not really designed to handle this sort of conditional logic. The idea behind them is you wrap up atomic pieces of work that are either successful and commit, or is unsuccessful and rolled back in their entirty. I suppose you could put some conditional logic in your catch statement that checks the SQLException's error message to see if it is from the second insert but it sounds a little flaky. You may just have to treat the three statements as seperate transactions an "rollback" in your code manually (i.e. issue a delete & update statements to undo what you've done).


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6657
    
    5

You may just have to treat the three satatements as seperate transacitons an "rollback" in your code manually (i.e. issue a delete & update statements to undo what you've done).


++ on that.

The requirement you are asking for goes against the nature of atomicity of a transaction ACID test.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Sumukh Deshpande
Ranch Hand

Joined: Feb 17, 2008
Posts: 87

Thanks. It cleared my doubt.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Transactions
 
Similar Threads
SQL syntax error
Commit & Rollback using java?
embedding a update & get in a ejb transaction
executeBatch
How to rollback multiple prepared statements