Interface TransactionalExecutor
Connection, in the context of a transaction.
Note that participation in an existing transaction i.e. requiresNew is merely a hint to the host
application and not binding. Best effort will be attempted.
The following example will query an application's table using JDBC:
final TransactionalExecutor transactionalExecutor = transactionalExecutorFactory
.createExecutor()
.readOnly()
.newTransaction();
final String summary = transactionalExecutor.execute(new ConnectionCallback<String>()
{
public String execute(final Connection connection)
{
try
{
final String schemaPrefix = transactionalExecutor.getSchemaName().map(s -> s + '.').getOrElse("");
final PreparedStatement preparedStatement = connection.prepareStatement(""
+ "select summary\n"
+ "from " + schemaPrefix + "jiraissue\n"
+ "order by id desc");
final ResultSet resultSet = preparedStatement.executeQuery();
return resultSet.next() ? resultSet.getString("summary") : "no issues";
}
catch (SQLException e)
{
throw new RuntimeException("oh noes!", e);
}
}
});
- Since:
- 3.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescription<A> Aexecute(ConnectionCallback<A> callback) Execute a callback which is supplied aConnection, within a transaction.Alter this executor so that the connection executes within an existing transaction or creates a new one if one is not presentio.atlassian.fugue.Option<String>Returns the configured schema name (if any), for connections provided duringexecute(ConnectionCallback).Alter this executor so that it executes in a new transaction, regardless of any existingreadOnly()Alter this executor so that the connection is read-onlyAlter this executor so that the connection is read-write
-
Method Details
-
execute
Execute a callback which is supplied aConnection, within a transaction.After successful execution, the transaction will be committed. If the transaction is within the scope of a larger transaction i.e.
requiresNewhas been set, it will be scheduled for commit, pending successful completion of the target transaction.If any exception is thrown by
callback, the transaction will be immediately rolled back.Do not attempt to retain or reuse the Connection outside of the scope of
callbackor within a different thread. Connection is usually not considered thread safe.Exceptions thrown from the callback are passed through without wrapping.
SQLExceptionoccuring in the Executor will be wrapped inRdbmsException.A
UnsupportedOperationExceptionwill be thrown on invoking any of the following:Connection.setAutoCommit(boolean)Connection.commit()Connection.close()Connection.rollback()Connection.setReadOnly(boolean)Connection.setReadOnly(boolean)Connection.abort(java.util.concurrent.Executor)Connection.setCatalog(String)Connection.setSchema(String)Connection.setTransactionIsolation(int)Connection.setNetworkTimeout(java.util.concurrent.Executor, int)
- Parameters:
callback- mandatory, may returnVoid- Returns:
- return value of
callback
-
getSchemaName
Returns the configured schema name (if any), for connections provided duringexecute(ConnectionCallback).- Returns:
- schema name, if there is one
-
readOnly
Alter this executor so that the connection is read-only- Returns:
- this executor
-
readWrite
Alter this executor so that the connection is read-write- Returns:
- this executor
-
newTransaction
Alter this executor so that it executes in a new transaction, regardless of any existing- Returns:
- this executor
-
existingTransaction
Alter this executor so that the connection executes within an existing transaction or creates a new one if one is not present- Returns:
- this executor
-