sf logo

sf.qof.session
Class BaseSessionRunner<T>

java.lang.Object
  extended by sf.qof.session.BaseSessionRunner<T>
Type Parameters:
T - the type of the result of the run method.
All Implemented Interfaces:
SessionRunner<T>
Direct Known Subclasses:
DefaultSessionRunner, RetrySessionRunner

public abstract class BaseSessionRunner<T>
extends Object
implements SessionRunner<T>

Abstract base class for implementing a SessionRunner.

Implements the execute method that has the following behaviour:
Start a new session, start a new transaction and call run. If no exception occured and the transaction is not in rollback only state then commit the transaction, stop the session and return the result of run. Otherwise rollback the transaction, stop the session and re-throw the exception wrapped in a SystemException.

BaseSessionRunner is used by default implementations of SessionRunner that use a TransactionRunnable to execute queries but it can also be used directly:

 String result = new BaseSessionRunner<String>("MY_CONTEXT_NAME") {
   protected String run(Connection connection, Object... arguments) throws SQLException {
     PreparedStatement ps = connection.prepareStatement("select name from person where id = ?");
     String result = null;
     try {
       ps.setInt(1, (Integer)arguments[0]);
       ResultSet rs = ps.executeQuery();
       if (rs.next()) {
         result = rs.getString(1);
       }
       rs.close();
     } finally {
       ps.close();
     }
     return result;
   }
 }.execute(55);
 


Field Summary
protected  SessionContext sessionContext
          Holds the current session context.
 
Constructor Summary
BaseSessionRunner()
          Creates a BaseSessionRunner that creates a session from the default session context.
BaseSessionRunner(String contextName)
          Creates a BaseSessionRunner that creates a session from the session context with the given name.
 
Method Summary
 T execute(Object... arguments)
          A call to execute starts a new session and executes some code in a transactional context.
protected abstract  T run(Connection connection, Object... arguments)
          This method is called once during the call to execute.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sessionContext

protected SessionContext sessionContext
Holds the current session context.

Constructor Detail

BaseSessionRunner

public BaseSessionRunner()
Creates a BaseSessionRunner that creates a session from the default session context.


BaseSessionRunner

public BaseSessionRunner(String contextName)
Creates a BaseSessionRunner that creates a session from the session context with the given name.

Parameters:
contextName - the context name
Method Detail

execute

public T execute(Object... arguments)
          throws SystemException
Description copied from interface: SessionRunner
A call to execute starts a new session and executes some code in a transactional context. If an exception is thrown by the executed code the transaction is rolled back otherwise it is commited.

Specified by:
execute in interface SessionRunner<T>
Parameters:
arguments - arguments that are passed to the executed code
Returns:
the result of the executed code
Throws:
SystemException - thrown if an unexpected error occured
See Also:
SessionRunner.execute(Object[])

run

protected abstract T run(Connection connection,
                         Object... arguments)
                  throws SQLException
This method is called once during the call to execute. It must be overridden and should contain the code that needs to be run in a transaction context.

Parameters:
connection - the thread's current database connection
arguments - arguments passed to the execute method
Returns:
a result
Throws:
SQLException - throw this if an error occured. This will cause a rollback of the current transaction.

sf logo

Copyright © 2007 brunella ltd. All Rights Reserved.