This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

AutoCommit(False) and rollback bst practise

 
Rahul Shivsharan
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
 
Lorand Komaromi
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...
 
Amarnath Ramasamy
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.

 
vidhya Narayanan
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?
 
Campbell Ritchie
Sheriff
Posts: 48454
56
  • 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.
 
Jan Cumps
Bartender
Posts: 2584
11
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic