|
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectsf.qof.session.BaseSessionRunner<T>
T - the type of the result of the run method.public abstract class BaseSessionRunner<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 |
|---|
protected SessionContext sessionContext
| Constructor Detail |
|---|
public BaseSessionRunner()
BaseSessionRunner that creates a session
from the default session context.
public BaseSessionRunner(String contextName)
BaseSessionRunner that creates a session
from the session context with the given name.
contextName - the context name| Method Detail |
|---|
public T execute(Object... arguments)
throws SystemException
SessionRunnerexecute 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.
execute in interface SessionRunner<T>arguments - arguments that are passed to the executed code
SystemException - thrown if an unexpected error occuredSessionRunner.execute(Object[])
protected abstract T run(Connection connection,
Object... arguments)
throws SQLException
execute.
It must be overridden and should contain the code that needs to be
run in a transaction context.
connection - the thread's current database connectionarguments - arguments passed to the execute method
SQLException - throw this if an error occured. This will cause a rollback
of the current transaction.
|
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||