sf logo

sf.qof.session
Class RetrySessionRunner<T>

java.lang.Object
  extended by sf.qof.session.BaseSessionRunner<T>
      extended by sf.qof.session.RetrySessionRunner<T>
Type Parameters:
T - the type of the result of the TransactionRunnable. If no result is returned this type should be Void
All Implemented Interfaces:
SessionRunner<T>

public class RetrySessionRunner<T>
extends BaseSessionRunner<T>

RetrySessionRunner is an implementation of SessionRunner that runs a TransactionRunnable in a session context and retries if an error occured. Calling execute starts a new session call the run method of the TransactionRunnable and commits or rolls back the transaction on success or failure. If an error occured it retries up to numberOfRetries times. delayInMilliSeconds can be used to wait a specified amount of time between the retries.

Typical usage is like this:

 List<Person> personList = ...
 PersonUpdaterRunnable runnable = new PersonUpdaterRunnable();
 Integer numberOfUpdates = 
   new RetrySessionRunner<Integer>(runnable, "MY_CONTEXT_NAME").execute(personList, 3);
 

See Also:
SessionRunner

Field Summary
 
Fields inherited from class sf.qof.session.BaseSessionRunner
sessionContext
 
Constructor Summary
RetrySessionRunner(TransactionRunnable<T> runnable, int numberOfRetries)
          Creates a RetrySessionRunner that creates a session from the default session context.
RetrySessionRunner(TransactionRunnable<T> runnable, int numberOfRetries, long delayInMilliSeconds)
          Creates a RetrySessionRunner that creates a session from the default session context.
RetrySessionRunner(TransactionRunnable<T> runnable, String contextName, int numberOfRetries)
          Creates a RetrySessionRunner that creates a session from the session context with the given name.
RetrySessionRunner(TransactionRunnable<T> runnable, String contextName, int numberOfRetries, long delayInMilliSeconds)
          Creates a RetrySessionRunner 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  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
 

Constructor Detail

RetrySessionRunner

public RetrySessionRunner(TransactionRunnable<T> runnable,
                          int numberOfRetries)
Creates a RetrySessionRunner that creates a session from the default session context.

Parameters:
runnable - a TransactionRunnable
numberOfRetries - max number of retries

RetrySessionRunner

public RetrySessionRunner(TransactionRunnable<T> runnable,
                          int numberOfRetries,
                          long delayInMilliSeconds)
Creates a RetrySessionRunner that creates a session from the default session context.

Parameters:
runnable - a TransactionRunnable
numberOfRetries - max number of retries
delayInMilliSeconds - delay inbetween retries in milli seconds

RetrySessionRunner

public RetrySessionRunner(TransactionRunnable<T> runnable,
                          String contextName,
                          int numberOfRetries)
Creates a RetrySessionRunner that creates a session from the session context with the given name.

Parameters:
runnable - a TransactionRunnable
contextName - the context name
numberOfRetries - max number of retries

RetrySessionRunner

public RetrySessionRunner(TransactionRunnable<T> runnable,
                          String contextName,
                          int numberOfRetries,
                          long delayInMilliSeconds)
Creates a RetrySessionRunner that creates a session from the session context with the given name.

Parameters:
runnable - a TransactionRunnable
contextName - the context name
numberOfRetries - max number of retries
delayInMilliSeconds - delay inbetween retries in milli seconds
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>
Overrides:
execute in class BaseSessionRunner<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 T run(Connection connection,
                Object... arguments)
         throws SQLException
Description copied from class: BaseSessionRunner
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.

Specified by:
run in class BaseSessionRunner<T>
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.