aspose file tools*
The moose likes JDBC and the fly likes AutoCommit(False) and rollback bst practise Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "AutoCommit(False) and rollback bst practise" Watch "AutoCommit(False) and rollback bst practise" New topic
Author

AutoCommit(False) and rollback bst practise

Rahul Shivsharan
Ranch Hand

Joined: Jun 17, 2009
Posts: 83
If i have two tables TABLE-A and TABLE-B

both of them has to be inserted or updated in transaction , if insert both insert , and if any error in insert of on table rollback from both the tables.

So my question is which is the best practise


Should i commit the connection in last line of try block or should i commit the connection in the finally block before closing the connection.

Please suggest me
Lorand Komaromi
Ranch Hand

Joined: Oct 08, 2009
Posts: 276
Commit it in the last line of the try block, you want to commit it only if there has not been an exception...


OCJP 6 (93%)
Amarnath Ramasamy
Greenhorn

Joined: Feb 05, 2010
Posts: 7
You should commit only at end of try block. As everyone knows, finally block will always be invoked regardless of exception throw or not. So obviuosly you need not commit after rollback during exception.

vidhya Narayanan
Greenhorn

Joined: Apr 08, 2010
Posts: 6
I too have the same problem.I have a set of tables where i will perform n number of updates and deletes. If i use this rollback() , will i get the previous values that were present?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39813
    
  28
Welcome to the Ranch vidhya Narayanan

Please avoid coloured text and only use "quote" for what other people have said.
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2510
    
  10

vidhya Narayanan wrote:I too have the same problem.I have a set of tables where i will perform n number of updates and deletes. If i use this rollback() , will i get the previous values that were present?
Yes. Rollback undoes all updates and deletes from when you started your transaction.

OCUP UML fundamental and ITIL foundation
youtube channel
Lorand Komaromi
Ranch Hand

Joined: Oct 08, 2009
Posts: 276
Jan Cumps wrote:Yes. Rollback undoes all updates and deletes from when you started your transaction.


Assuming that you're using a DB that supports transactions...
 
 
subject: AutoCommit(False) and rollback bst practise