com.onewebsql.util.transaction
Class TransactionUtil

java.lang.Object
  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();
 ds.setServerName("localhost");
 ds.setDatabaseName("transactiondemo");
 ds.setUser("transactiondemo");
 ds.setPassword("transactiondemo");

 // 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(...);
         book.setPrice(12.45);
         bookDAO.update(book);  
         ...
                         
         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

TransactionUtil

public TransactionUtil(TransactionalDataSource transactionalDataSource)
Creates transaction util.

Parameters:
transactionalDataSource - transactional data source that transaction management
Method Detail

withTransaction

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
Parameters:
operation - the operation to run within transaction
Returns:
result returned by operation
Throws:
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.