Class TransactionUtil

  extended by com.onewebsql.util.transaction.TransactionUtil

public class TransactionUtil
extends Object

Transaction util helps to run your operation within transaction. You don't have to manage transaction scope and to handle exceptions manually. Transaction util itself begins transaction and commits it or rollbacks. Example of transaction util usage is shown below:

 // create and configure your data source (e.g. PostgreSQL data source)
 PGSimpleDataSource ds = new PGSimpleDataSource();

 // create our transactional data source wrapper
 TransactionalDataSource transactionalDs = new TransactionalDataSource(ds);
 // create transaction util
 TransactionUtil transactionUtil = new TransactionUtil(transactionalDs);
 // wrap your operation into TransactionOp and run within transaction
 transactionUtil.withTransaction(new TransactionOp() {
     public String invoke(TransactionalDataSource transactionalDataSource) {
         // create DAOs
         BookDAO bookDAO = new BookDAOImpl(transactionalDataSource, new PostgresDBAdapter());
         // do operation on database using DAOs
         Book book = bookDAO.getByPK(...);
         return null;
 When your operation don't throw exception transaction will be commited. When your operation
 throws exception or you mark transaction for rollback (TransactionalDataSource.markTransactionForRollback())
 transaction will be rollback.
 More information about OneWebSQL and database transactions you will find in "OneWebSQL Transaction Guide".

Constructor Summary
TransactionUtil(TransactionalDataSource transactionalDataSource)
          Creates transaction util.
Method Summary
<T> T
withTransaction(TransactionOp<T> operation)
          Runs given operation within transaction.
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail


public TransactionUtil(TransactionalDataSource transactionalDataSource)
Creates transaction util.

transactionalDataSource - transactional data source that transaction management
Method Detail


public <T> T withTransaction(TransactionOp<T> operation)
Runs given operation within transaction. It means that TransactionalDataSource.beginTransaction() is invoked before operation and TransactionalDataSource.commitTransaction() is invoked after operation. If operation throws exception during its execution or transaction is marked for rollback TransactionalDataSource.rollbackTransaction() is invoked instead of TransactionalDataSource.commitTransaction().

Type Parameters:
T - type of object returned by operation
operation - the operation to run within transaction
result returned by operation
RuntimeException - if operation throws exception during its execution
RuntimeTransactionException - if exception occurs during transaction commit or rollback
com.onewebsql.util.exceptions.RuntimeDoubleException - if operation throws exception during its execution and then rollback also failed

Copyright © 2011-2013 e-point SA. All Rights Reserved.