Forum Stats

  • 3,770,686 Users
  • 2,253,154 Discussions
  • 7,875,540 Comments

Discussions

why do we need to rollback ?

Murray9654
Murray9654 Member Posts: 486

Hi,

    i am using plain jdbc and tomcats connection pool. I mean i am using datasource. In a try block i am setting the auto commit to false and after executing the update statement i am committing the transaction by Connection.commit() but i am not writing any roll back in catch block. My intention is if an exception occurs it should roll back because it is not commited. So why should we declare rollback in catch block? Is it mandatory to do it? Is there any problem if we don't do it?

Tagged:
Murray9654

Best Answer

  • Unknown
    edited Oct 27, 2015 10:14AM Accepted Answer
    My intention is if an exception occurs it should roll back because it is not commited.
    

    Then you should put a ROLLBACK so that your 'intention' becomes reality at the EXACT POINT it should happen.

     So why should we declare rollback in catch block?
    

    Because that is the EXACT POINT in time when you know for sure that a ROLLBACK is the appropriate thing to do.

    Your code should EXPLICITLY perform any actions needed to implement your intent. You should NEVER let Oracle (any DB) or Java perform implicit operations.

    Is it mandatory to do it?
    

    No - it is NOT mandatory.

    Is there any problem if we don't do it?
    

    Yes - there WILL BE problems - if someone else needs to maintain, fix or enhance your code later.

    With no explicit ROLLBACK the rollback will ONLY happen when the connection object is closed/destroyed. That may not happen until your application actually finishes and exits Java.

    Meanwhile your uncommitted operation may have LOCKED data in the database preventing other users from working with that data. You are also still user other database resources (e.g. temp space, memory) that you really don't need anymore.

    BEST PRACTICES is to ALWAYS release resources when you no longer need them. That includes using a ROLLBACK as soon as you know that is what is needed.

    Murray9654

Answers

  • Unknown
    edited Oct 27, 2015 10:14AM Accepted Answer
    My intention is if an exception occurs it should roll back because it is not commited.
    

    Then you should put a ROLLBACK so that your 'intention' becomes reality at the EXACT POINT it should happen.

     So why should we declare rollback in catch block?
    

    Because that is the EXACT POINT in time when you know for sure that a ROLLBACK is the appropriate thing to do.

    Your code should EXPLICITLY perform any actions needed to implement your intent. You should NEVER let Oracle (any DB) or Java perform implicit operations.

    Is it mandatory to do it?
    

    No - it is NOT mandatory.

    Is there any problem if we don't do it?
    

    Yes - there WILL BE problems - if someone else needs to maintain, fix or enhance your code later.

    With no explicit ROLLBACK the rollback will ONLY happen when the connection object is closed/destroyed. That may not happen until your application actually finishes and exits Java.

    Meanwhile your uncommitted operation may have LOCKED data in the database preventing other users from working with that data. You are also still user other database resources (e.g. temp space, memory) that you really don't need anymore.

    BEST PRACTICES is to ALWAYS release resources when you no longer need them. That includes using a ROLLBACK as soon as you know that is what is needed.

    Murray9654
  • Murray9654
    Murray9654 Member Posts: 486
    edited Oct 27, 2015 10:18AM

    Thank you so much for your quick response.

This discussion has been closed.