sqlg2.db
Class TransactionRunnable<R>

java.lang.Object
  extended by sqlg2.db.TransactionRunnable<R>
Type Parameters:
R - type of a value computed by the transactional block
All Implemented Interfaces:
java.rmi.Remote, ISimpleTransaction

public abstract class TransactionRunnable<R>
extends java.lang.Object
implements ISimpleTransaction

Utility class to run ITransactions safely.


Constructor Summary
TransactionRunnable()
           
 
Method Summary
protected  void commit()
          Commits transaction
<T extends IDBCommon>
T
getInterface(java.lang.Class<T> cls)
          Returns data access interface generated by preprocessor running in the transaction.
protected  void rollback()
          Rolls back transaction
protected abstract  R run()
          Implement this method to define which block to run in transaction.
static
<T> T
runInTransaction(IDBInterface db, TransactionRunnable<T> runnable)
          Run block in transaction atomically: block is either committed or rolled back.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TransactionRunnable

public TransactionRunnable()
Method Detail

commit

protected final void commit()
                     throws java.sql.SQLException,
                            java.rmi.RemoteException
Commits transaction

Throws:
java.sql.SQLException
java.rmi.RemoteException

rollback

protected final void rollback()
                       throws java.sql.SQLException,
                              java.rmi.RemoteException
Rolls back transaction

Throws:
java.sql.SQLException
java.rmi.RemoteException

getInterface

public final <T extends IDBCommon> T getInterface(java.lang.Class<T> cls)
                                       throws java.rmi.RemoteException
Returns data access interface generated by preprocessor running in the transaction.

Specified by:
getInterface in interface ISimpleTransaction
Parameters:
cls - interface class
Returns:
data access interface implementation
Throws:
java.rmi.RemoteException

run

protected abstract R run()
                  throws java.sql.SQLException,
                         java.rmi.RemoteException
Implement this method to define which block to run in transaction.

Returns:
computed value
Throws:
java.sql.SQLException
java.rmi.RemoteException

runInTransaction

public static <T> T runInTransaction(IDBInterface db,
                                     TransactionRunnable<T> runnable)
                          throws java.sql.SQLException,
                                 java.rmi.RemoteException
Run block in transaction atomically: block is either committed or rolled back. It is more safe to use than ITransaction manually since you can forget to finish transaction. Anyway you should call either commit() or rollback() at the end of block, or else transaction will be rolled back automatically.

Example:

 TransactionRunnable.runInTransaction(db, new TransactionRunnable<Void>() {
     public Void run() {
         ITest testIface = getInterface(ITest.class);
         // work with testIFace in the transaction
         commit();
         return null;
     }
 });
 

Throws:
java.sql.SQLException
java.rmi.RemoteException