Dashboard > RIFE > ... > Database > Chainable transactions
RIFE Log In | Sign Up   View a printable version of the current page.
Chainable transactions


Added by Geert Bevin, last edited by Geert Bevin on May 22, 2004  (view change)
Labels: 
(None)

The DbQueryManager class has an inTransaction(DbTransactionUser) method. It ensures that all the instructions in the provided DbTransactionUser instance are executed inside a transaction and committed afterwards. This doesn't mean that a new transaction will always be created. If a transaction is already active, it will simply be re-used. The commit will also only take place if a new transaction has actually been started by the active inTransaction invocation, otherwise it's the responsibility of the enclosing code to execute the commit. If a runtime exception occurs during the execution and a new transaction has been started beforehand, it will be automatically rolled back.

If you need to explicitly roll back an active transaction, use the rollback method of the DbTransactionUser class. If you use a regular rollback method, it's possible that you're inside a nested transaction and that after the rollback other logic continues to be executed outside the transaction. Using the correct rollback method, stops the execution of the active DbTransactionUser and breaks out of any number of them nesting.

Since you sometimes use a transaction without being interested in returning a result, you can use the DbTransactionUserWithoutResult class which doesn't require you to write a meaningless 'return null;' statement at the end of the implemented method.

It's recommended to always use transactions through the inTransaction method since it ensures that transactional code can be re-used and enclosed in other transactional code. Correctly using the regular transaction-related methods requires great care and planning and often results in error-prone and not reusable code.

For example:

final Insert insert = new Insert(mDatasource).into("valuelist").field("value", 232);
final DbQueryManager manager = new DbQueryManager(datasource);
manager.inTransaction(new DbTransactionUserWithoutResult() {
        public void useTransactionWithoutResult()
        throws InnerClassException
        {
            manager.executeUpdate(insert);
            manager.executeUpdate(insert);
        }
    });

You can throw regular exceptions inside the transaction with the DbTransactionUser's throwException(Throwable) method. The transaction will be rolled back if the active inTransaction method started a new transaction. An instance of InnerClassException will be thrown and can be caught outside the inTransaction method. The original exception you wanted to throw is then available through the InnerClassException's getCause() method.



Are you enjoying Confluence? Please consider purchasing it today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.1a Build:#515 May 19, 2006) - Bug/feature request - Contact Administrators