public final class TransactionContext extends java.lang.Object
There are also facilities for setting a timeout, after
which the TransactionContext will automatically become invalidated, and
subsequent operations on it will throw a RuntimeException
.
Constructor and Description |
---|
TransactionContext(java.sql.Connection connection)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Releases the internal connection back the pool and invalidates this context
object.
|
void |
commit()
Commits the internal Connection if the transaction has not timed out and is
still valid.
|
java.sql.Statement |
createStatement()
Creates a
Statement object for sending
SQL statements to the database. |
int |
getTransactionIsolation()
Retrieves the current transaction isolation level for the underlying
Connection . |
boolean |
isClosed()
Checks whether this TransactionContext is closed.
|
boolean |
isTimedOut()
Checks whether this TransactionContext has timed out.
|
java.sql.CallableStatement |
prepareCall(java.lang.String sql)
Creates a
CallableStatement object for calling
database stored procedures. |
java.sql.PreparedStatement |
prepareStatement(java.lang.String sql)
Creates a
PreparedStatement object for sending
parameterized SQL statements to the database. |
void |
rollBack()
Rolls back the internal Connection if the transaction has not timed out and
is still valid.
|
Entry |
searchToRawEntry(java.sql.PreparedStatement statement,
java.lang.String rdnColumn)
Executes the given
PreparedStatement and converts the results into
a raw LDAP Entry , where the attribute names are the raw column
names from the
result of the query. |
Entry |
searchToRawEntry(java.lang.String sql,
java.lang.String rdnColumn)
Executes the given SQL query and converts the results into a raw LDAP
Entry , where the attribute names are the raw column names from the
result of the query. |
void |
setTimeout(long timeOutMillis)
Sets a timeout for this TransactionContext.
|
void |
setTransactionIsolation(int level)
Attempts to change the transaction isolation level for the underlying
Connection object to the one given. |
public TransactionContext(java.sql.Connection connection)
connection
- a fresh JDBC Connection from the poolpublic Entry searchToRawEntry(java.lang.String sql, java.lang.String rdnColumn) throws java.sql.SQLException
Entry
, where the attribute names are the raw column names from the
result of the query. The rdnColumn
parameter is the column
name
to use as the RDN attribute for the entry. For example if the rdnColumn is
"accountID", then the DN of the returned Entry will be
"accountID={accountID}".sql
- the SQL query to executerdnColumn
- the column name in the result set which is to be used for
the DN of the returned Entry
java.sql.SQLException
- if a database access error occurs or the query matches more than
one rowpublic Entry searchToRawEntry(java.sql.PreparedStatement statement, java.lang.String rdnColumn) throws java.sql.SQLException
PreparedStatement
and converts the results into
a raw LDAP Entry
, where the attribute names are the raw column
names from the
result of the query. The rdnColumn
parameter is the column
name
to use as the RDN attribute for the entry. For example if the rdnColumn is
"accountID", then the DN of the returned Entry will be
"accountID={accountID}".statement
- the PreparedStatement to executerdnColumn
- the column name in the result set which is to be used for
the DN of the returned Entry
java.sql.SQLException
- if a database access error occurs or the query matches more than
one rowpublic java.sql.Statement createStatement() throws java.sql.SQLException
Statement
object for sending
SQL statements to the database.
SQL statements without parameters are normally
executed using Statement
objects. If the same SQL statement
is executed many times, it may be more efficient to use a
PreparedStatement
object.
Result sets created using the returned Statement
object will
by default be type TYPE_FORWARD_ONLY
and have a concurrency
level of CONCUR_READ_ONLY
. The holdability of the created
result sets can be determined by calling Connection.getHoldability()
.
Statement
objectjava.sql.SQLException
- if a database access error occurs
or this method is called on a closed connectionpublic java.sql.PreparedStatement prepareStatement(java.lang.String sql) throws java.sql.SQLException
PreparedStatement
object for sending
parameterized SQL statements to the database.
A SQL statement with or without IN parameters can be pre-compiled and
stored in a PreparedStatement
object. This object can then be
used to efficiently execute this statement multiple times.
Note: This method is optimized for handling parametric SQL
statements that benefit from precompilation. If the driver supports
precompilation, the method prepareStatement
will send the
statement to the database for precompilation. Some drivers may not support
precompilation. In this case, the statement may not be sent to the database
until the PreparedStatement
object is executed. This has no
direct effect on users; however, it does affect which methods throw certain
SQLException
objects.
Result sets created using the returned PreparedStatement
object will by default be type TYPE_FORWARD_ONLY
and have a
concurrency level of CONCUR_READ_ONLY
. The holdability of the
created result sets can be determined by calling
Connection.getHoldability()
.
sql
- an SQL statement that may contain one or more '?' IN
parameter placeholdersPreparedStatement
object containing the
pre-compiled SQL statementjava.sql.SQLException
- if a database access error occurs
or this method is called on a closed connectionpublic java.sql.CallableStatement prepareCall(java.lang.String sql) throws java.sql.SQLException
CallableStatement
object for calling
database stored procedures.
The CallableStatement
object provides
methods for setting up its IN and OUT parameters, and
methods for executing the call to a stored procedure.
Note: This method is optimized for handling stored procedure call
statements. Some drivers may send the call statement to the database when
the method prepareCall
is done; others may wait until the
CallableStatement
object is executed. This has no direct
effect on users; however, it does affect which method throws certain
SQLExceptions.
Result sets created using the returned CallableStatement
object will by default be type TYPE_FORWARD_ONLY
and have a
concurrency level of CONCUR_READ_ONLY
. The holdability of the
created result sets can be determined by calling
Connection.getHoldability()
.
sql
- an SQL statement that may contain one or more '?'
parameter placeholders. Typically this statement is specified
using JDBC
call escape syntax.CallableStatement
object containing the
pre-compiled SQL statementjava.sql.SQLException
- if a database access error occurs
or this method is called on a closed connectionpublic int getTransactionIsolation() throws java.sql.SQLException
Connection
.Connection.TRANSACTION_READ_UNCOMMITTED
,
Connection.TRANSACTION_READ_COMMITTED
,
Connection.TRANSACTION_REPEATABLE_READ
,
Connection.TRANSACTION_SERIALIZABLE
, or
Connection.TRANSACTION_NONE
.java.sql.SQLException
- if a database access error occurs or this method is called on a
closed connectionpublic void setTransactionIsolation(int level) throws java.sql.SQLException
Connection
object to the one given.
The constants defined in the interface Connection
are the possible
transaction isolation levels.
Note: If this method is called during a transaction, the result is implementation-defined.
level
- one of the following Connection
constants:
Connection.TRANSACTION_READ_UNCOMMITTED
,
Connection.TRANSACTION_READ_COMMITTED
,
Connection.TRANSACTION_REPEATABLE_READ
, or
Connection.TRANSACTION_SERIALIZABLE
.
(Note that Connection.TRANSACTION_NONE
cannot be used
because it specifies that transactions are not supported.)java.sql.SQLException
- if a database access error occurs, this
method is called on a closed connection
or the given parameter is not one of the Connection
constantspublic void commit() throws java.sql.SQLException
java.sql.SQLException
- if a database access error occurspublic void rollBack() throws java.sql.SQLException
java.sql.SQLException
- if a database access error occurspublic void close()
public void setTimeout(long timeOutMillis)
After the timeout has expired, all methods except isTimedOut
and
isClosed
will throw a IllegalStateException
.
timeOutMillis
- the delay in milliseconds after which this TransactionContext will
time outpublic boolean isTimedOut()
After the timeout has expired, all methods except isTimedOut
,
isClosed
, and log[Error|Info|Debug|Exception]
will
throw an IllegalStateException
.
public boolean isClosed()