Package org.jdbi.v3.core
Class Jdbi
java.lang.Object
org.jdbi.v3.core.Jdbi
- All Implemented Interfaces:
Configurable<Jdbi>
Main entry point; configurable wrapper around a JDBC
DataSource
.
Use it to obtain Handle instances and provide configuration
for all handles obtained from it.-
Method Summary
Modifier and TypeMethodDescriptionstatic Jdbi
Creates a newJdbi
instance from a database URL.static Jdbi
Creates a newJdbi
instance from a database URL.static Jdbi
create
(String url, Properties properties) Creates a newJdbi
instance from a database URL.static Jdbi
create
(Connection connection) Creates a newJdbi
object from aConnection
.static Jdbi
create
(DataSource dataSource) Creates a newJdbi
object from aDataSource
.static Jdbi
create
(ConnectionFactory connectionFactory) Factory used to allow for obtaining a Connection in a customized manner.Returns the configuration registry associated with this object.Returns theHandleCallbackDecorator
.final HandleScope
Returns the internalHandleScope
object.Returns the currentStatementBuilderFactory
.Returns theTransactionHandler
.installPlugin
(JdbiPlugin plugin) Install a givenJdbiPlugin
instance that will configure any providedHandle
instances.Use theServiceLoader
API to detect and install plugins automagically.<R,
X extends Exception>
RinTransaction
(HandleCallback<R, X> callback) A convenience function which manages the lifecycle of a handle and yields it to a callback for use by clients.<R,
X extends Exception>
RinTransaction
(TransactionIsolationLevel level, HandleCallback<R, X> callback) A convenience function which manages the lifecycle of a handle and yields it to a callback for use by clients.<E> E
Creates an extension instance that uses the currentJdbi
instance for database operations.open()
Obtain a Handle to the data source wrapped by this Jdbi instance.static Handle
Obtain a handle with just a JDBC URLstatic Handle
Obtain a handle with just a JDBC URLstatic Handle
open
(String url, Properties props) Obtain a handle with just a JDBC URLstatic Handle
open
(Connection connection) Create a Handle wrapping a particular JDBC Connectionstatic Handle
open
(DataSource dataSource) Convenience method used to obtain a handle from a specific data sourcestatic Handle
open
(ConnectionFactory connectionFactory) Convenience method used to obtain a handle from aConnectionFactory
.setHandleCallbackDecorator
(HandleCallbackDecorator handleCallbackDecorator) Specify theHandleCallbackDecorator
instance to use.final void
setHandleScope
(HandleScope handleScope) Set theHandleScope
object.Allows customization of how prepared statements are created.setTransactionHandler
(TransactionHandler handler) Specify the TransactionHandler instance to use.<E,
X extends Exception>
voiduseExtension
(Class<E> extensionType, ExtensionConsumer<E, X> callback) A convenience method which opens an extension of the given type, and yields it to a callback.<X extends Exception>
voiduseHandle
(HandleConsumer<X> consumer) A convenience function which manages the lifecycle of a handle and yields it to a callback for use by clients.<X extends Exception>
voiduseTransaction
(HandleConsumer<X> callback) A convenience function which manages the lifecycle of a handle and yields it to a callback for use by clients.<X extends Exception>
voiduseTransaction
(TransactionIsolationLevel level, HandleConsumer<X> callback) A convenience function which manages the lifecycle of a handle and yields it to a callback for use by clients.<R,
E, X extends Exception>
RwithExtension
(Class<E> extensionType, ExtensionCallback<R, E, X> callback) A convenience method which opens an extension of the given type, yields it to a callback, and returns the result of the callback.<R,
X extends Exception>
RwithHandle
(HandleCallback<R, X> callback) A convenience function which manages the lifecycle of a handle and yields it to a callback for use by clients.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, registerCollector, registerColumnMapper, registerColumnMapper, registerColumnMapper, registerColumnMapper, registerColumnMapper, registerColumnMapper, registerExtension, registerRowMapper, registerRowMapper, registerRowMapper, registerRowMapper, setMapKeyColumn, setMapValueColumn, setSqlArrayArgumentStrategy, setSqlLogger, setSqlParser, setTemplateEngine, setTimingCollector
-
Method Details
-
create
Creates a newJdbi
object from aConnection
.- Parameters:
connection
- AConnection
object.- Returns:
- A
Jdbi
instance that uses a single database connection.
-
create
Creates a newJdbi
object from aDataSource
.- Parameters:
dataSource
- the data source.- Returns:
- a Jdbi which uses the given data source as a connection factory.
-
create
Factory used to allow for obtaining a Connection in a customized manner.The
ConnectionFactory.openConnection()
method will be invoked to obtain a connection instance whenever a Handle is opened.- Parameters:
connectionFactory
- Provides JDBC connections to Handle instances- Returns:
- a Jdbi which uses the given connection factory.
-
create
Creates a newJdbi
instance from a database URL.- Parameters:
url
- A JDBC URL for connections.- Returns:
- a Jdbi which uses
DriverManager
as a connection factory.
-
create
Creates a newJdbi
instance from a database URL.- Parameters:
url
- JDBC URL for connectionsproperties
- Properties to pass to DriverManager.getConnection(url, props) for each new handle- Returns:
- a Jdbi which uses
DriverManager
as a connection factory.
-
create
Creates a newJdbi
instance from a database URL.- Parameters:
url
- JDBC URL for connectionsusername
- User name for connection authenticationpassword
- Password for connection authentication- Returns:
- a Jdbi which uses
DriverManager
as a connection factory.
-
open
Convenience method used to obtain a handle from a specific data source- Parameters:
dataSource
- the JDBC data source.- Returns:
- Handle using a Connection obtained from the provided DataSource
-
open
Convenience method used to obtain a handle from aConnectionFactory
.- Parameters:
connectionFactory
- the connection factory- Returns:
- Handle using a Connection obtained from the provided connection factory
-
open
Create a Handle wrapping a particular JDBC Connection- Parameters:
connection
- the JDBC connection- Returns:
- Handle bound to connection
-
open
-
open
-
open
Obtain a handle with just a JDBC URL- Parameters:
url
- JDBC Urlprops
- JDBC properties- Returns:
- newly opened Handle
-
installPlugins
Use theServiceLoader
API to detect and install plugins automagically. Some people consider this feature dangerous; some consider it essential -- use at your own risk.- Returns:
- this
-
installPlugin
Install a givenJdbiPlugin
instance that will configure any providedHandle
instances.- Parameters:
plugin
- the plugin to install- Returns:
- this
-
setStatementBuilderFactory
Allows customization of how prepared statements are created. When a Handle is created against this Jdbi instance the factory will be used to create a StatementBuilder for that specific handle. When the handle is closed, the StatementBuilder's close method will be invoked.- Parameters:
factory
- the new statement builder factory.- Returns:
- this
-
getStatementBuilderFactory
Returns the currentStatementBuilderFactory
.- Returns:
- the current
StatementBuilderFactory
-
getConfig
Description copied from interface:Configurable
Returns the configuration registry associated with this object.- Specified by:
getConfig
in interfaceConfigurable<Jdbi>
- Returns:
- the configuration registry associated with this object.
-
setTransactionHandler
Specify the TransactionHandler instance to use. This allows overriding transaction semantics, or mapping into different transaction management systems.The default version uses local transactions on the database Connection instances obtained.
- Parameters:
handler
- The TransactionHandler to use for all Handle instances obtained from this Jdbi- Returns:
- this
-
getTransactionHandler
Returns theTransactionHandler
.- Returns:
- the
TransactionHandler
-
setHandleCallbackDecorator
Specify theHandleCallbackDecorator
instance to use. This allows overriding callbacks foruseHandle(org.jdbi.v3.core.HandleConsumer<X>)
,withHandle(org.jdbi.v3.core.HandleCallback<R, X>)
,useTransaction(HandleConsumer)
andinTransaction(HandleCallback)
. The default version is a pass-through that returns the callback unchanged.- Parameters:
handleCallbackDecorator
- TheHandleCallbackDecorator
to use for alluseHandle(org.jdbi.v3.core.HandleConsumer<X>)
,withHandle(org.jdbi.v3.core.HandleCallback<R, X>)
,useTransaction(HandleConsumer)
andinTransaction(HandleCallback)
from this Jdbi. Must not be null- Returns:
- this
-
getHandleCallbackDecorator
Returns theHandleCallbackDecorator
.- Returns:
- the
HandleCallbackDecorator
-
getHandleScope
Returns the internalHandleScope
object. The Jdbi instance uses this to provide handles in a given scope. The default scope is per-thread, so every thread manages its own handle.
This is an internal method and not part of the public API!- Returns:
- A
HandleScope
object
-
setHandleScope
Set theHandleScope
object. The Jdbi instance uses this to provide handles in a given scope.- Parameters:
handleScope
- AHandleScope
object. Must not be null!
-
open
Obtain a Handle to the data source wrapped by this Jdbi instance. You own this expensive resource and are required to close it or risk leaks. Using atry-with-resources
block is recommended.- Returns:
- an open Handle instance
- See Also:
-
withHandle
A convenience function which manages the lifecycle of a handle and yields it to a callback for use by clients.- Type Parameters:
R
- type returned by the callbackX
- exception type thrown by the callback, if any.- Parameters:
callback
- A callback which will receive an open Handle- Returns:
- the value returned by callback
- Throws:
X
- any exception thrown by the callback
-
useHandle
A convenience function which manages the lifecycle of a handle and yields it to a callback for use by clients.- Type Parameters:
X
- exception type thrown by the callback, if any.- Parameters:
consumer
- A callback which will receive an open Handle- Throws:
X
- any exception thrown by the callback
-
inTransaction
A convenience function which manages the lifecycle of a handle and yields it to a callback for use by clients. The handle will be in a transaction when the callback is invoked, and that transaction will be committed if the callback finishes normally, or rolled back if the callback raises an exception.- Type Parameters:
R
- type returned by the callbackX
- exception type thrown by the callback, if any.- Parameters:
callback
- A callback which will receive an open Handle, in a transaction- Returns:
- the value returned by callback
- Throws:
X
- any exception thrown by the callback
-
useTransaction
A convenience function which manages the lifecycle of a handle and yields it to a callback for use by clients. The handle will be in a transaction when the callback is invoked, and that transaction will be committed if the callback finishes normally, or rolled back if the callback raises an exception.- Type Parameters:
X
- exception type thrown by the callback, if any.- Parameters:
callback
- 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 XA convenience function which manages the lifecycle of a handle and yields it to a callback for use by clients. The handle will be in a transaction when the callback is invoked, and that transaction will be committed if the callback finishes normally, or rolled back if the callback raises an exception.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 the 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:
- the value returned by callback
- Throws:
X
- any exception thrown by the callback
-
useTransaction
public <X extends Exception> void useTransaction(TransactionIsolationLevel level, HandleConsumer<X> callback) throws X A convenience function which manages the lifecycle of a handle and yields it to a callback for use by clients. The handle will be in a transaction when the callback is invoked, and that transaction will be committed if the callback finishes normally, or rolled back if the callback raises an exception.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.callback
- A callback which will receive an open Handle, in a transaction- Throws:
X
- any exception thrown by the callback
-
withExtension
public <R,E, R withExtensionX extends Exception> (Class<E> extensionType, ExtensionCallback<R, E, throws XX> callback) A convenience method which opens an extension of the given type, yields it to a callback, and returns the result of the callback. A handle is opened if needed by the extension, and closed before returning to the caller.- Type Parameters:
R
- the return typeE
- the extension typeX
- the exception type optionally thrown by the callback- Parameters:
extensionType
- the type of extension.callback
- a callback which will receive the extension.- Returns:
- the value returned by the callback.
- Throws:
NoSuchExtensionException
- if noExtensionFactory
is registered which supports the given extension type.X
- if thrown by the callback.
-
useExtension
public <E,X extends Exception> void useExtension(Class<E> extensionType, ExtensionConsumer<E, X> callback) throws XA convenience method which opens an extension of the given type, and yields it to a callback. A handle is opened if needed by the extention, and closed before returning to the caller.- Type Parameters:
E
- the extension typeX
- the exception type optionally thrown by the callback- Parameters:
extensionType
- the type of extensioncallback
- a callback which will receive the extension- Throws:
NoSuchExtensionException
- if noExtensionFactory
is registered which supports the given extension type.X
- if thrown by the callback.
-
onDemand
Creates an extension instance that uses the currentJdbi
instance for database operations.- Type Parameters:
E
- the extension type- Parameters:
extensionType
- the type of extension. Must be a public interface type.- Returns:
- an extension which opens and closes handles (as needed) for individual method calls. Only public interface types may be used as on-demand extensions.
-