Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Transactions

 
Sumukh Deshpande
Ranch Hand
Posts: 87
Firefox Browser IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sumukh Deshpande
Ranch Hand
Posts: 87
Firefox Browser IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. It cleared my doubt.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic