Package org.jdbi.v3.core
Class Handle
java.lang.Object
org.jdbi.v3.core.Handle
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Configurable<Handle>
This represents a connection to the database system. It is a wrapper around
a JDBC Connection object. Handle provides essential methods for transaction
management, statement creation, and other operations tied to the database session.
-
Method Summary
Modifier and TypeMethodDescriptionafterCommit
(Runnable afterCommit) Execute an action the next time this Handle commits, unless it is rolled back first.afterRollback
(Runnable afterRollback) Execute an action the next time this Handle rolls back, unless it is committed first.<T> T
Create a Jdbi extension object of the specified type bound to this handle.begin()
Start a transaction.void
close()
Closes the handle, its connection, and any other database resources it is holding.commit()
Commit a transaction.Create a non-prepared (no bound parameters, but different SQL) batch statement.createCall
(CharSequence sql) Create a call to a stored procedure.createCall
(String sql) Create a call to a stored procedure.createQuery
(CharSequence sql) Return a Query instance that executes a statement with bound parameters and maps the result set into Java types.createQuery
(String sql) Return a Query instance that executes a statement with bound parameters and maps the result set into Java types.createScript
(CharSequence sql) Creates a Script from the given SQL script.createScript
(String sql) Create an Insert or Update statement which returns the number of rows modified.createUpdate
(CharSequence sql) Create an Insert or Update statement which returns the number of rows modified.createUpdate
(String sql) Create an Insert or Update statement which returns the number of rows modified.int
execute
(CharSequence sql, Object... args) Execute a SQL statement, and return the number of rows affected by the statement.int
Execute a SQL statement, and return the number of rows affected by the statement.Returns the configuration registry associated with this object.Get the JDBC Connection this Handle uses.Returns the extension method currently bound to the handle's context.getJdbi()
Returns the currentStatementBuilder
.Obtain the current transaction isolation level.<R,
X extends Exception>
RinTransaction
(HandleCallback<R, X> callback) Executescallback
in a transaction, and returns the result of the callback.<R,
X extends Exception>
RinTransaction
(TransactionIsolationLevel level, HandleCallback<R, X> callback) Executescallback
in a transaction, and returns the result of the callback.boolean
isClosed()
Returns true if theHandle
has been closed.boolean
Returns whether the handle is in a transaction.boolean
Whether the connection is in read-only mode.prepareBatch
(CharSequence sql) Prepare a batch to execute.prepareBatch
(String sql) Prepare a batch to execute.queryMetadata
(MetaData.MetaDataResultSetProvider metadataFunction) Access database metadata that returns aResultSet
.<T> T
queryMetadata
(MetaData.MetaDataValueProvider<T> metadataFunction) Access all database metadata that returns simple values.Release a previously created savepoint.rollback()
Rollback a transaction.rollbackToSavepoint
(String savepointName) Rollback a transaction to a named savepoint.Create a transaction savepoint with the name provided.select
(CharSequence sql, Object... args) Convenience method which creates a query with the given positional arguments.Convenience method which creates a query with the given positional arguments.setReadOnly
(boolean readOnly) Set the Handle readOnly.setStatementBuilder
(StatementBuilder builder) Specify the statement builder to use for this handle.void
setTransactionIsolation
(int level) Set the transaction isolation level on the underlying connection.void
Set the transaction isolation level on the underlying connection.<X extends Exception>
voiduseTransaction
(HandleConsumer<X> consumer) Executescallback
in a transaction.<X extends Exception>
voiduseTransaction
(TransactionIsolationLevel level, HandleConsumer<X> consumer) Executescallback
in a transaction.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.jdbi.v3.core.config.Configurable
addCustomizer, configure, define, getConfig, registerArgument, registerArgument, registerArrayType, registerArrayType, registerArrayType, registerArrayType, registerCodecFactory, registerCollector, registerColumnMapper, registerColumnMapper, registerColumnMapper, registerColumnMapper, registerColumnMapper, registerColumnMapper, registerExtension, registerRowMapper, registerRowMapper, registerRowMapper, registerRowMapper, setMapKeyColumn, setMapValueColumn, setSqlArrayArgumentStrategy, setSqlLogger, setSqlParser, setTemplateEngine
-
Method Details
-
getJdbi
-
getConfig
Description copied from interface:Configurable
Returns the configuration registry associated with this object.- Specified by:
getConfig
in interfaceConfigurable<Handle>
- Returns:
- the configuration registry associated with this object.
-
getConnection
Get the JDBC Connection this Handle uses.- Returns:
- the JDBC Connection this Handle uses
-
getStatementBuilder
Returns the currentStatementBuilder
.- Returns:
- the current
StatementBuilder
-
setStatementBuilder
Specify the statement builder to use for this handle.- Parameters:
builder
- StatementBuilder to be used- Returns:
- this
-
close
public void close()Closes the handle, its connection, and any other database resources it is holding.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
CloseException
- if any resources throw exception while closingTransactionException
- if called while the handle has a transaction open. The open transaction will be rolled back.
-
isClosed
public boolean isClosed()Returns true if theHandle
has been closed.- Returns:
- True if the Handle is closed.
-
select
Convenience method which creates a query with the given positional arguments.- Parameters:
sql
- SQL or named statementargs
- arguments to bind positionally- Returns:
- query object
-
select
Convenience method which creates a query with the given positional arguments. Takes a string argument for backwards compatibility reasons.- Parameters:
sql
- SQL or named statementargs
- arguments to bind positionally- Returns:
- query object
- See Also:
-
execute
Execute a SQL statement, and return the number of rows affected by the statement.- Parameters:
sql
- the SQL statement to execute, using positional parameters (if any).args
- positional arguments.- Returns:
- the number of rows affected.
-
execute
Execute a SQL statement, and return the number of rows affected by the statement. Takes a string argument for backwards compatibility reasons.- Parameters:
sql
- the SQL statement to execute, using positional parameters (if any).args
- positional arguments.- Returns:
- the number of rows affected.
- See Also:
-
createBatch
Create a non-prepared (no bound parameters, but different SQL) batch statement.- Returns:
- empty batch
- See Also:
-
prepareBatch
Prepare a batch to execute. This is for efficiently executing more than one of the same statements with different parameters bound.- Parameters:
sql
- the batch SQL.- Returns:
- a batch which can have "statements" added.
-
prepareBatch
Prepare a batch to execute. This is for efficiently executing more than one of the same statements with different parameters bound. Takes a string argument for backwards compatibility reasons.- Parameters:
sql
- the batch SQL.- Returns:
- a batch which can have "statements" added.
- See Also:
-
createCall
Create a call to a stored procedure.- Parameters:
sql
- the stored procedure sql.- Returns:
- the Call.
-
createCall
Create a call to a stored procedure. Takes a string argument for backwards compatibility reasons.- Parameters:
sql
- the stored procedure sql.- Returns:
- the Call.
- See Also:
-
createQuery
Return a Query instance that executes a statement with bound parameters and maps the result set into Java types.- Parameters:
sql
- SQL that may return results.- Returns:
- a Query builder.
-
createQuery
Return a Query instance that executes a statement with bound parameters and maps the result set into Java types. Takes a string argument for backwards compatibility reasons.- Parameters:
sql
- SQL that may return results.- Returns:
- a Query builder.
- See Also:
-
createScript
Creates a Script from the given SQL script.- Parameters:
sql
- the SQL script.- Returns:
- the created Script.
-
createScript
Create an Insert or Update statement which returns the number of rows modified. Takes a string argument for backwards compatibility reasons.- Parameters:
sql
- the statement sql.- Returns:
- the Update builder.
- See Also:
-
createUpdate
Create an Insert or Update statement which returns the number of rows modified.- Parameters:
sql
- the statement sql.- Returns:
- the Update builder.
-
createUpdate
Create an Insert or Update statement which returns the number of rows modified. Takes a string argument for backwards compatibility reasons.- Parameters:
sql
- the statement sql.- Returns:
- the Update builder.
- See Also:
-
queryMetadata
Access database metadata that returns aResultSet
. All methods ofResultBearing
can be used to format and map the returned results.List<String> catalogs = h.queryMetadata(DatabaseMetaData::getCatalogs) .mapTo(String.class) .list();
returns the list of catalogs from the current database.
- Parameters:
metadataFunction
- Maps the providedDatabaseMetaData
object onto aResultSet
object.- Returns:
- The metadata builder.
-
queryMetadata
Access all database metadata that returns simple values.boolean supportsTransactions = handle.queryMetadata(DatabaseMetaData::supportsTransactions);
- Parameters:
metadataFunction
- Maps the providedDatabaseMetaData
object to a response object.- Returns:
- The response object.
-
isInTransaction
public boolean isInTransaction()Returns whether the handle is in a transaction. Delegates to the underlyingTransactionHandler
.- Returns:
- True if the handle is in a transaction.
-
begin
Start a transaction.- Returns:
- the same handle
-
commit
Commit a transaction.- Returns:
- the same handle
-
rollback
Rollback a transaction.- Returns:
- the same handle
-
afterCommit
Execute an action the next time this Handle commits, unless it is rolled back first.- Parameters:
afterCommit
- the action to execute after commit- Returns:
- this Handle
-
afterRollback
Execute an action the next time this Handle rolls back, unless it is committed first.- Parameters:
afterRollback
- the action to execute after rollback- Returns:
- this Handle
-
rollbackToSavepoint
Rollback a transaction to a named savepoint.- Parameters:
savepointName
- the name of the savepoint, previously declared withsavepoint(java.lang.String)
- Returns:
- the same handle
-
savepoint
Create a transaction savepoint with the name provided.- Parameters:
name
- The name of the savepoint- Returns:
- The same handle
-
release
Release a previously created savepoint.- Parameters:
savepointName
- the name of the savepoint to release- Returns:
- the same handle
-
isReadOnly
public boolean isReadOnly()Whether the connection is in read-only mode.- Returns:
- True if the connection is in read-only mode.
- See Also:
-
setReadOnly
Set the Handle readOnly. This acts as a hint to the database to improve performance or concurrency. May not be called in an active transaction!- Parameters:
readOnly
- whether the Handle is readOnly- Returns:
- this Handle
- See Also:
-
inTransaction
Executescallback
in a transaction, and returns the result of the callback.- Type Parameters:
R
- type returned by callbackX
- exception type thrown by the callback, if any- Parameters:
callback
- a callback which will receive an open handle, in a transaction.- Returns:
- value returned from the callback
- Throws:
X
- any exception thrown by the callback
-
useTransaction
Executescallback
in a transaction.- Type Parameters:
X
- exception type thrown by the callback, if any- Parameters:
consumer
- a callback which will receive an open handle, in a transaction.- Throws:
X
- any exception thrown by the callback
-
inTransaction
public <R,X extends Exception> R inTransaction(TransactionIsolationLevel level, HandleCallback<R, X> callback) throws XExecutescallback
in a transaction, and returns the result of the callback.This form accepts a transaction isolation level which will be applied to the connection for the scope of this transaction, after which the original isolation level will be restored.
- Type Parameters:
R
- type returned by callbackX
- exception type thrown by the callback, if any- Parameters:
level
- the transaction isolation level which will be applied to the connection for the scope of this transaction, after which the original isolation level will be restored.callback
- a callback which will receive an open handle, in a transaction.- Returns:
- value returned from the callback
- Throws:
X
- any exception thrown by the callback
-
useTransaction
public <X extends Exception> void useTransaction(TransactionIsolationLevel level, HandleConsumer<X> consumer) throws X Executescallback
in a transaction.This form accepts a transaction isolation level which will be applied to the connection for the scope of this transaction, after which the original isolation level will be restored.
- Type Parameters:
X
- exception type thrown by the callback, if any- Parameters:
level
- the transaction isolation level which will be applied to the connection for the scope of this transaction, after which the original isolation level will be restored.consumer
- a callback which will receive an open handle, in a transaction.- Throws:
X
- any exception thrown by the callback
-
setTransactionIsolation
Set the transaction isolation level on the underlying connection.- Parameters:
level
- the isolation level to use- Throws:
UnableToManipulateTransactionIsolationLevelException
- if isolation level is not supported by the underlying connection or JDBC driver
-
setTransactionIsolation
public void setTransactionIsolation(int level) Set the transaction isolation level on the underlying connection.- Parameters:
level
- the isolation level to use
-
getTransactionIsolationLevel
Obtain the current transaction isolation level.- Returns:
- the current isolation level on the underlying connection
-
attach
Create a Jdbi extension object of the specified type bound to this handle. The returned extension's lifecycle is coupled to the lifecycle of this handle. Closing the handle will render the extension unusable.- Type Parameters:
T
- the extension type- Parameters:
extensionType
- the extension class- Returns:
- the new extension object bound to this handle
-
getExtensionMethod
Returns the extension method currently bound to the handle's context.- Returns:
- the extension method currently bound to the handle's context
-