T - The type of objects that start and end transactions.public interface TxManager<T>
JDBCDataSource and provides the means for
implicit hierarchical transactions and sharing connections. Each connection
is bound to an object. If this object requests another connection, the very
same connection (and thus the same transaction) will be used. This allows for
spanning a transaction over several method calls without the need for the
connection being a method parameter. This TxManager manages
committing and rolling back appropriately, that is, a hierarchical connection
will not be committed before the topmost starter commits.
When starting a transaction, the caller can specify the desired isolation
level and whether the transaction is read-only. If this is not specified, the
default value set in the constructor or the default value from the underlying
database will be used. If subsequent starters of the same transaction (using
the same object) provide a more restrictive isolation level or want to get a
writable transaction that is already read-only, an
InvalidOptionsException will be thrown immediately. This also
applies to hierarchical transactions. If subsequent starters want a
hierarchical transaction but the top-level transaction has been started as
non-hierarchical, an InvalidOptionsException will be thrown
immediately. This does not apply for subsequent non-hierarchical based and a
hierarchical top-level transaction.
Use the following usage pattern (or something similar) for appropriate clean-up of the resources:
T ta;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try
{
con = txManager.startTransaction(ta);
try
{
stmt = con.createStatement();
rs = stmt.executeQuery(<query>);
// read from the result set
rs = txManager.close(rs);
stmt = txManager.close(stmt);
txManager.finishTransaction(ta);
// These 3 lines may also be 1 line: txManager.finishTransaction(rs, stmt, ta);
// Now all three variables are null and the transaction will be finished
// and cannot be used again. This will be handled appropriately in the finally-block.
}
finally
{
// This will only do something if the transaction has not been finished.
txManager.closeTransaction(ta, stmt, rs);
}
}
catch (SQLException ex) // the catch part is optional, depending on which exceptions your method may throw
{
throw new DataSourceException(ex);
}
The minimal usage pattern is as follows:
T ta;
Connection con = null;
try
{
con = txManager.startTransaction(ta);
try
{
// do anything with the connection
txManager.finishTransaction(ta);
}
finally
{
txManager.closeTransaction(ta);
}
}
catch (SQLException ex) // the catch part is optional, depending on which exceptions your method may throw
{
throw new DataSourceException(ex);
}
It is crucial for the hierarchical transaction handling that the transaction
is finished and closed. If a transaction is closed without being
finished, the transaction will be rolled back!
Transactions for remote iterators are potentially long-running and are therefore treated separately. Since they are used for retrieval of a lot of data, they are read-only and READ_COMMITTED. Each transaction will have its own connection. As soon as the remote iterator is created, it will handle closing the involved result set, statement and connection. However, before the remote iterator is created, the caller needs to handle clean-up. The usage pattern is similar:
T ta;
ManagedExtendedConnection con = null;
ManagedStatement stmt = null;
ManagedResultSet rs = null;
try
{
con = txManager.startIteratorTransaction(ta);
try
{
stmt = con.createStatement();
rs = stmt.executeQuery(<query>);
// create the remote iterator
// do not close result set or the statement and do not commit or rollback
// the transaction
// If you do not create a remote iterator, for instance, the result set is
// empty, you will need to close explicitly.
if (!creationSuccessful)
{
rs = txManager.close(rs);
stmt = txManager.close(stmt);
con = txManager.finishIteratorTransaction(con);
}
}
finally
{
// This will only do something if the transaction has not been finished.
// Otherwise the result set, statement and connection will be closed
// when dropping all created remote iterator.
txManager.closeIteratorTransaction(ta, rs, stmt);
}
}
catch (SQLException ex) // the catch part is optional, depending on which exceptions your method may throw
{
throw new DataSourceException(ex);
}
Note that the iterator transaction can be used for arbitrary read-only access
before the remote iterator is created.| Modifier and Type | Method and Description |
|---|---|
<R extends java.sql.ResultSet> |
close(R rs)
Closes the designated result set and returns
null. |
<S extends java.sql.Statement> |
close(S stmt)
Closes the designated statement and returns
null. |
void |
closeIteratorTransaction(java.sql.ResultSet rs,
java.sql.Statement stmt,
T o)
Closes the designated result set, the designated statement and the iterator
transaction started for the designated object and rolls it back if no
remote iterator exists.
|
void |
closeIteratorTransaction(java.sql.Statement stmt,
T o)
Closes the designated statement and the iterator transaction started for
the designated object and rolls it back if no remote iterator exists.
|
void |
closeIteratorTransaction(T o)
Closes the iterator transaction started for the designated object and rolls
it back if no remote iterator exists.
|
void |
closeQuietly(java.sql.ResultSet rs)
Closes the designated result set and ignores any exception.
|
void |
closeQuietly(java.sql.ResultSet rs,
java.sql.Statement stmt)
Closes the designated result set and the statement and ignores any
exception.
|
void |
closeQuietly(java.sql.Statement stmt)
Closes the designated statement and ignores any exception.
|
void |
closeTransaction(java.sql.ResultSet rs,
java.sql.Statement stmt,
T o)
Closes the designated result set, the designated statement and the
transaction started for the designated object on the current level and
rolls it back if not explicitly finished.
|
void |
closeTransaction(java.sql.Statement stmt,
T o)
Closes the designated statement the transaction started for the designated
object on the current level and rolls it back if not explicitly
finished.
|
void |
closeTransaction(T o)
Closes the transaction started for the designated object on the current
level and rolls it back if not explicitly
finished.
|
ExtendedConnection |
commitTransaction(T o)
Commits the transaction started for the designated object.
|
<M extends ManagedExtendedConnection> |
finishIteratorTransaction(M con)
Finishes the iterator transaction of the designated connection.
|
void |
finishTransaction(java.sql.ResultSet rs,
java.sql.Statement stmt,
T o)
Closes the designated result set, the designated statement and finishes the
transaction started for the designated object on the current level.
|
void |
finishTransaction(java.sql.Statement stmt,
T o)
Closes the designated statement and finishes the transaction started for
the designated object on the current level.
|
void |
finishTransaction(T o)
Finishes the transaction started for the designated object on the current
level.
|
void |
reserveConnection(T o,
boolean readOnly)
Reserves a read-only or writable connection for the designated object.
|
void |
reserveConnection(T o,
boolean readOnly,
boolean hierarchical)
Reserves a read-only or writable connection and possibly for a hierarchical
transaction for the designated object.
|
void |
reserveConnection(T o,
boolean readOnly,
boolean hierarchical,
int isolation)
Reserves a read-only or writable connection and possibly for a hierarchical
transaction for the designated object having the designated isolation
level.
|
void |
reserveConnection(T o,
boolean readOnly,
int isolation)
Reserves a read-only or writable connection for the designated object
having the designated isolation level.
|
ExtendedConnection |
rollbackTransaction(T o)
Rolls back the transaction started for the designated object.
|
ManagedExtendedConnection |
startIteratorTransaction(T o)
Starts a new iterator transaction for the designated object and returns the
corresponding connection.
|
ExtendedConnection |
startTransaction(T o,
boolean readOnly)
Starts a read-only or writable transaction for the designated object and
returns the corresponding connection.
|
ExtendedConnection |
startTransaction(T o,
boolean readOnly,
boolean hierarchical)
Starts a read-only or writable transaction and possibly a hierarchical
transaction for the designated object and returns the corresponding
connection.
|
ExtendedConnection |
startTransaction(T o,
boolean readOnly,
boolean hierarchical,
int isolation)
Starts a read-only or writable transaction and possibly a hierarchical
transaction for the designated object having the designated isolation
level.
|
ExtendedConnection |
startTransaction(T o,
boolean readOnly,
int isolation)
Starts a read-only or writable transaction for the designated object having
the designated isolation level.
|
void |
unreserveConnection(T o)
Unreserves a connection for the designated object.
|
void reserveConnection(T o, boolean readOnly) throws java.sql.SQLException
Make sure to unreserve the connection!
o - The object for which to reserve a connection.readOnly - A hint on whether the connection is read-only.java.sql.SQLException - If there are problems creating a connection, an
SQLException will be thrown.InvalidOptionsException - If the previously created
transaction/reserved connection is read-only but now a writable
connection is requested, an InvalidOptionsException
will be thrown.void reserveConnection(T o, boolean readOnly, boolean hierarchical) throws java.sql.SQLException
Make sure to unreserve the connection!
o - The object for which to start a transaction.readOnly - A hint on whether the connection is read-only.hierarchical - Whether subtransactions should be isolated from higher
level transactions by using savepoints.java.sql.SQLException - If there are problems creating a connection, an
SQLException will be thrown.InvalidOptionsException - If the previously created
transaction/reserved connection is read-only but now a writable
connection is requested or the previously created instance is
sharing but now a connection for hierarchical transactions is
requested, an InvalidOptionsException will be
thrown.void reserveConnection(T o, boolean readOnly, int isolation) throws java.sql.SQLException
Make sure to unreserve the connection!
o - The object for which to start a transaction.readOnly - A hint on whether the connection is read-only.isolation - A hint on the desired isolation level.java.sql.SQLException - If there are problems creating a connection, an
SQLException will be thrown.InvalidOptionsException - If the previously created
transaction/reserved connection is read-only but now a writable
connection is requested or the requested isolation level is more
restrictive than the one already set previously for the
connection, an InvalidOptionsException will be
thrown.void reserveConnection(T o, boolean readOnly, boolean hierarchical, int isolation) throws java.sql.SQLException
Make sure to unreserve the connection!
o - The object for which to reserve a connection.readOnly - A hint on whether the connection is read-only.hierarchical - A hint on whether subtransactions should be isolated
from higher level transactions by using savepoints.isolation - A hint on the desired isolation level.java.sql.SQLException - If there are problems creating a connection, an
SQLException will be thrown.InvalidOptionsException - If the previously created
transaction/reserved connection is read-only but now a writable
connection is requested or the requested isolation level is more
restrictive than the one already set previously for the
connection or the previously created instance is sharing but now
a connection for hierarchical transactions is requested, an
InvalidOptionsException will be thrown.void unreserveConnection(T o) throws java.sql.SQLException
o - The object for which to unreserve a connection.java.sql.SQLException - If there are problems closing the connection, an
SQLException will be thrown.ExtendedConnection startTransaction(T o, boolean readOnly) throws java.sql.SQLException
o - The object for which to start a transaction.readOnly - A hint on whether the transaction is read-only.java.sql.SQLException - If there are problems creating a connection, an
SQLException will be thrown.InvalidOptionsException - If the previously created transaction is
read-only but now a writable transaction is requested, an
InvalidOptionsException will be thrown.ExtendedConnection startTransaction(T o, boolean readOnly, boolean hierarchical) throws java.sql.SQLException
o - The object for which to start a transaction.readOnly - A hint on whether the transaction is read-only.hierarchical - Whether subtransactions should be isolated from higher
level transactions by using savepoints.java.sql.SQLException - If there are problems creating a connection, an
SQLException will be thrown.InvalidOptionsException - If the previously created transaction is
read-only but now a writable transaction is requested or the
previously created instance is sharing but now a hierarchical
transaction is requested, an InvalidOptionsException
will be thrown.ExtendedConnection startTransaction(T o, boolean readOnly, int isolation) throws java.sql.SQLException
o - The object for which to start a transaction.readOnly - A hint on whether the transaction is read-only.isolation - A hint on the desired isolation level.java.sql.SQLException - If there are problems creating a connection, an
SQLException will be thrown.InvalidOptionsException - If the previously created transaction is
read-only but now a writable transaction is requested or the
requested isolation level is more restrictive than the one
already set previously for the transaction, an
InvalidOptionsException will be thrown.ExtendedConnection startTransaction(T o, boolean readOnly, boolean hierarchical, int isolation) throws java.sql.SQLException
o - The object for which to start a transaction.readOnly - A hint on whether the transaction is read-only.hierarchical - A hint on whether subtransactions should be isolated
from higher level transactions by using savepoints.isolation - A hint on the desired isolation level.java.sql.SQLException - If there are problems creating a connection, an
SQLException will be thrown.InvalidOptionsException - If the previously created transaction is
read-only but now a writable transaction is requested or the
requested isolation level is more restrictive than the one
already set previously for the transaction or the previously
created instance is sharing but now a hierarchical transaction is
requested, an InvalidOptionsException will be
thrown.<S extends java.sql.Statement> S close(S stmt)
throws java.sql.SQLException
null. This allows
for the intended usage pattern (see class description).stmt - The statement to be closed.null if closing was successful, the designated
statement otherwise.java.sql.SQLException - If there are problems closing the designated
statement, an SQLException will be thrown.<R extends java.sql.ResultSet> R close(R rs)
throws java.sql.SQLException
null. This allows
for the intended usage pattern (see class description). This does not need
to be called for a result set that is part of a remote iterator.rs - The result set to be closed.null if closing was successful, the designated result
set otherwise.java.sql.SQLException - If there are problems closing the designated result
set, an SQLException will be thrown.ExtendedConnection commitTransaction(T o) throws java.sql.SQLException
o - The object for which to commit a transaction.java.sql.SQLException - If there are problems committing the transaction (or
releasing the savepoint), an SQLException will be
thrown.ExtendedConnection rollbackTransaction(T o) throws java.sql.SQLException
o - The object for which to rollback a transaction.java.sql.SQLException - If there are problems rolling back the transaction (or
the savepoint), an SQLException will be thrown.void finishTransaction(T o) throws java.sql.SQLException
o - The object for which to finish and commit a transaction.InvalidServiceUsageException - If
no transaction has been started for the designated object, an
InvalidServiceUsageException will be thrown.java.sql.SQLException - If there are problems finishing and committing the
transaction (or releasing the savepoint), an
SQLException will be thrown.void finishTransaction(java.sql.Statement stmt,
T o)
throws java.sql.SQLException
stmt - The statement to be closed.o - The object for which to finish and commit a transaction.InvalidServiceUsageException - If
no transaction has been started for the designated object, an
InvalidServiceUsageException will be thrown.java.sql.SQLException - If there are problems closing the statement or
finishing and committing the transaction (or releasing the
savepoint), an SQLException will be thrown.void finishTransaction(java.sql.ResultSet rs,
java.sql.Statement stmt,
T o)
throws java.sql.SQLException
rs - The result set to be closed.stmt - The statement to be closed.o - The object for which to finish and commit a transaction.java.sql.SQLException - If there are problems closing the statement, the
result set or finishing and committing the transaction (or
releasing the savepoint), an SQLException will be
thrown.void closeQuietly(java.sql.Statement stmt)
stmt - The statement to be closed without exception.void closeQuietly(java.sql.ResultSet rs)
rs - The result set to be closed without exception.void closeQuietly(java.sql.ResultSet rs,
java.sql.Statement stmt)
rs - The result set to be closed without exception.stmt - The statement to be closed without exception.void closeTransaction(T o)
o - The object for which to close and rollback a transaction if not
finished normally.void closeTransaction(java.sql.Statement stmt,
T o)
stmt - The statement to be closed without exception.o - The object for which to close and rollback a transaction if not
finished normally.void closeTransaction(java.sql.ResultSet rs,
java.sql.Statement stmt,
T o)
rs - The result set to be closed without exception.stmt - The statement to be closed without exception.o - The object for which to close and rollback a transaction if not
finished normally.ManagedExtendedConnection startIteratorTransaction(T o) throws java.sql.SQLException
o - The object for which to start an iterator transaction.java.sql.SQLException - If there are problems creating a connection, an
SQLException will be thrown.InvalidServiceUsageException - If
there is still an iterator transaction open for this object, an
InvalidServiceUsageException will be thrown.void closeIteratorTransaction(T o)
o - The object for which to close and rollback an iterator transaction
if no remote iterator exists.void closeIteratorTransaction(java.sql.Statement stmt,
T o)
stmt - The statement to be closed without exception if no remote
iterator exists.o - The object for which to close and rollback an iterator transaction
if no remote iterator exists.void closeIteratorTransaction(java.sql.ResultSet rs,
java.sql.Statement stmt,
T o)
rs - The result set to be closed without exception if no remote
iterator exists.stmt - The statement to be closed without exception if no remote
iterator exists.o - The object for which to close and rollback an iterator transaction
if no remote iterator exists.<M extends ManagedExtendedConnection> M finishIteratorTransaction(M con)
con - The connection for which all remote iterator have been dropped.null if closing was successful, the designated managed
extended connection otherwise.