What is the correct code pattern for completing transactions in Java (exception rollback and successful commit)?

I am looking for a common code pattern to correctly handle transactions that may be abnormal I assume that there is a common code pattern, no matter what kind of transaction we deal with

I have a method to do something in a transaction and want to re throw the exception that may occur inside the transaction code block The following are examples of such methods:

protected void doIt() {
  // for JDBC connection transaction may be started automatically
  // but assume we start it here
  Tran tran = session.beginTran();
  try {
    // here comes code that does some processing
    // modifies some data in transaction
    // etc.

    // success - so commit
    tran.commit();

  } catch (Exception ex) { // many different exceptions may be thrown
                           // subclass of RuntimeException,sqlException etc.
     // error - so rollback
     tran.rollback();

     // Now rethrow ex
     throw ex;              // this line causes trouble,see description below
  }      
}

Now - there is a compilation error in the doit method It must declare throws exception, but this is unacceptable because the method doit is used in many places and adding throws exception leads to subsequent modifications where doit is used directly and indirectly This is because of known Java language design problems and declared exceptions

The question now is how to change the exception and transaction code to achieve my goal - correctly handle the transaction completion (commit or rollback based on success conditions) and re throw the exactly same exception that may be caught in the transaction code block

I know I can do something like throwing a new runtimeException (Ex), but it will throw exceptions of other classes. I want to avoid such a solution

Solution

I would choose such a thing

protected void doIt() {
  // for JDBC connection transaction may be started automatically
  // but assume we start it here
  Tran tran = session.beginTran();
  bool success = false;
  try {
    // here comes code that does some processing
    // modifies some data in transaction
    // etc.

    // success - so commit
    tran.commit();
    success = true;
  } finally { 
     // error - so rollback
     if (! success)
       tran.rollback();
  }      
}

.. Or if tran has a method to query status (tran. Isfinished ()) or something else, you don't need bool Any thrown exception (i.e. runtime exception or error, if there is no checked exception) will be executed directly, and the finally block will be executed on the stack

If rollback throws an exception, you will need to catch those and log em or other things (failed rollback is a very serious case) Remember not to throw anything in this case, because the exception currently unfolding the stack will be lost

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>