• Post Reply Bookmark Topic Watch Topic
  • New Topic

AutoCommit(False) and rollback bst practise  RSS feed

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Commit it in the last line of the try block, you want to commit it only if there has not been an exception...
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 56819
173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch vidhya Narayanan

Please avoid coloured text and only use "quote" for what other people have said.
 
Bartender
Posts: 2633
17
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Lorand Komaromi
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!