Package org.jdbi.v3.core
Class Jdbi
- java.lang.Object
-
- org.jdbi.v3.core.Jdbi
-
- All Implemented Interfaces:
Configurable<Jdbi>
public class Jdbi extends java.lang.Object implements Configurable<Jdbi>
Main entry point; configurable wrapper around a JDBCDataSource. Use it to obtain Handle instances and provide configuration for all handles obtained from it.
-
-
Field Summary
Fields Modifier and Type Field Description private ConfigRegistryconfigprivate ConnectionFactoryconnectionFactoryprivate static org.slf4j.LoggerLOGprivate java.util.concurrent.CopyOnWriteArrayList<JdbiPlugin>pluginsprivate java.util.concurrent.atomic.AtomicReference<StatementBuilderFactory>statementBuilderFactoryprivate java.lang.ThreadLocal<HandleSupplier>threadHandleSupplierprivate java.util.concurrent.atomic.AtomicReference<TransactionHandler>transactionhandler
-
Constructor Summary
Constructors Modifier Constructor Description privateJdbi(ConnectionFactory connectionFactory)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private <R,E,X extends java.lang.Exception>
RcallWithExtension(java.lang.Class<E> extensionType, ExtensionCallback<R,E,X> callback, HandleSupplier handleSupplier)static Jdbicreate(java.lang.String url)Creates a newJdbiinstance from a database URL.static Jdbicreate(java.lang.String url, java.lang.String username, java.lang.String password)Creates a newJdbiinstance from a database URL.static Jdbicreate(java.lang.String url, java.util.Properties properties)Creates a newJdbiinstance from a database URL.static Jdbicreate(java.sql.Connection connection)Creates a newJdbiobject from aConnection.static Jdbicreate(javax.sql.DataSource dataSource)Creates a newJdbiobject from aDataSource.static Jdbicreate(ConnectionFactory connectionFactory)Factory used to allow for obtaining a Connection in a customized manner.ConfigRegistrygetConfig()Returns the configuration registry associated with this object.StatementBuilderFactorygetStatementBuilderFactory()Returns the currentStatementBuilderFactory.TransactionHandlergetTransactionHandler()Returns theTransactionHandler.JdbiinstallPlugin(JdbiPlugin plugin)Install a givenJdbiPlugininstance that will configure any providedHandleinstances.JdbiinstallPlugins()Use theServiceLoaderAPI to detect and install plugins automagically.<R,X extends java.lang.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 java.lang.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> EonDemand(java.lang.Class<E> extensionType)Creates an extension instance that uses the currentJdbiinstance for database operations.Handleopen()Obtain a Handle to the data source wrapped by this Jdbi instance.static Handleopen(java.lang.String url)Obtain a handle with just a JDBC URLstatic Handleopen(java.lang.String url, java.lang.String username, java.lang.String password)Obtain a handle with just a JDBC URLstatic Handleopen(java.lang.String url, java.util.Properties props)Obtain a handle with just a JDBC URLstatic Handleopen(java.sql.Connection connection)Create a Handle wrapping a particular JDBC Connectionstatic Handleopen(javax.sql.DataSource dataSource)Convenience method used to obtain a handle from a specific data sourcestatic Handleopen(ConnectionFactory connectionFactory)Convenience method used to obtain a handle from aConnectionFactory.JdbisetStatementBuilderFactory(StatementBuilderFactory factory)Allows customization of how prepared statements are created.JdbisetTransactionHandler(TransactionHandler handler)Specify the TransactionHandler instance to use.<E,X extends java.lang.Exception>
voiduseExtension(java.lang.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 java.lang.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 java.lang.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 java.lang.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 java.lang.Exception>
RwithExtension(java.lang.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 java.lang.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
clone, equals, finalize, 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
-
-
-
-
Field Detail
-
LOG
private static final org.slf4j.Logger LOG
-
config
private final ConfigRegistry config
-
connectionFactory
private final ConnectionFactory connectionFactory
-
transactionhandler
private final java.util.concurrent.atomic.AtomicReference<TransactionHandler> transactionhandler
-
statementBuilderFactory
private final java.util.concurrent.atomic.AtomicReference<StatementBuilderFactory> statementBuilderFactory
-
plugins
private final java.util.concurrent.CopyOnWriteArrayList<JdbiPlugin> plugins
-
threadHandleSupplier
private final java.lang.ThreadLocal<HandleSupplier> threadHandleSupplier
-
-
Constructor Detail
-
Jdbi
private Jdbi(ConnectionFactory connectionFactory)
-
-
Method Detail
-
create
public static Jdbi create(java.sql.Connection connection)
Creates a newJdbiobject from aConnection.- Parameters:
connection- AConnectionobject.- Returns:
- A
Jdbiinstance that uses a single database connection.
-
create
public static Jdbi create(javax.sql.DataSource dataSource)
Creates a newJdbiobject from aDataSource.- Parameters:
dataSource- the data source.- Returns:
- a Jdbi which uses the given data source as a connection factory.
-
create
public static Jdbi create(ConnectionFactory connectionFactory)
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
public static Jdbi create(java.lang.String url)
Creates a newJdbiinstance from a database URL.- Parameters:
url- A JDBC URL for connections.- Returns:
- a Jdbi which uses
DriverManageras a connection factory.
-
create
public static Jdbi create(java.lang.String url, java.util.Properties properties)
Creates a newJdbiinstance 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
DriverManageras a connection factory.
-
create
public static Jdbi create(java.lang.String url, java.lang.String username, java.lang.String password)
Creates a newJdbiinstance from a database URL.- Parameters:
url- JDBC URL for connectionsusername- User name for connection authenticationpassword- Password for connection authentication- Returns:
- a Jdbi which uses
DriverManageras a connection factory.
-
open
public static Handle open(javax.sql.DataSource dataSource)
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
public static Handle open(ConnectionFactory connectionFactory)
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
public static Handle open(java.sql.Connection connection)
Create a Handle wrapping a particular JDBC Connection- Parameters:
connection- the JDBC connection- Returns:
- Handle bound to connection
-
open
public static Handle open(java.lang.String url)
Obtain a handle with just a JDBC URL- Parameters:
url- JDBC Url- Returns:
- newly opened Handle
-
open
public static Handle open(java.lang.String url, java.lang.String username, java.lang.String password)
Obtain a handle with just a JDBC URL- Parameters:
url- JDBC Urlusername- JDBC username for authenticationpassword- JDBC password for authentication- Returns:
- newly opened Handle
-
open
public static Handle open(java.lang.String url, java.util.Properties props)
Obtain a handle with just a JDBC URL- Parameters:
url- JDBC Urlprops- JDBC properties- Returns:
- newly opened Handle
-
installPlugins
public Jdbi installPlugins()
Use theServiceLoaderAPI to detect and install plugins automagically. Some people consider this feature dangerous; some consider it essential -- use at your own risk.- Returns:
- this
-
installPlugin
public Jdbi installPlugin(JdbiPlugin plugin)
Install a givenJdbiPlugininstance that will configure any providedHandleinstances.- Parameters:
plugin- the plugin to install- Returns:
- this
-
setStatementBuilderFactory
public Jdbi setStatementBuilderFactory(StatementBuilderFactory factory)
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
public StatementBuilderFactory getStatementBuilderFactory()
Returns the currentStatementBuilderFactory.- Returns:
- the current
StatementBuilderFactory
-
getConfig
public ConfigRegistry getConfig()
Description copied from interface:ConfigurableReturns the configuration registry associated with this object.- Specified by:
getConfigin interfaceConfigurable<Jdbi>- Returns:
- the configuration registry associated with this object.
-
setTransactionHandler
public Jdbi setTransactionHandler(TransactionHandler handler)
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
public TransactionHandler getTransactionHandler()
Returns theTransactionHandler.- Returns:
- the
TransactionHandler
-
open
public Handle 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-resourcesblock is recommended.- Returns:
- an open Handle instance
- See Also:
useHandle(HandleConsumer),withHandle(HandleCallback)
-
withHandle
public <R,X extends java.lang.Exception> R withHandle(HandleCallback<R,X> callback) throws X extends java.lang.Exception
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 callbackX extends java.lang.Exception
-
useHandle
public <X extends java.lang.Exception> void useHandle(HandleConsumer<X> consumer) throws X extends java.lang.Exception
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 callbackX extends java.lang.Exception
-
inTransaction
public <R,X extends java.lang.Exception> R inTransaction(HandleCallback<R,X> callback) throws X extends java.lang.Exception
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 callbackX extends java.lang.Exception
-
useTransaction
public <X extends java.lang.Exception> void useTransaction(HandleConsumer<X> callback) throws X extends java.lang.Exception
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 callbackX extends java.lang.Exception
-
inTransaction
public <R,X extends java.lang.Exception> R inTransaction(TransactionIsolationLevel level, HandleCallback<R,X> callback) throws X extends java.lang.Exception
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:
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 callbackX extends java.lang.Exception
-
useTransaction
public <X extends java.lang.Exception> void useTransaction(TransactionIsolationLevel level, HandleConsumer<X> callback) throws X extends java.lang.Exception
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 callbackX extends java.lang.Exception
-
withExtension
public <R,E,X extends java.lang.Exception> R withExtension(java.lang.Class<E> extensionType, ExtensionCallback<R,E,X> callback) throws X extends java.lang.ExceptionA 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 noExtensionFactoryis registered which supports the given extension type.X- if thrown by the callback.X extends java.lang.Exception
-
callWithExtension
private <R,E,X extends java.lang.Exception> R callWithExtension(java.lang.Class<E> extensionType, ExtensionCallback<R,E,X> callback, HandleSupplier handleSupplier) throws X extends java.lang.Exception- Throws:
X extends java.lang.Exception
-
useExtension
public <E,X extends java.lang.Exception> void useExtension(java.lang.Class<E> extensionType, ExtensionConsumer<E,X> callback) throws X extends java.lang.ExceptionA 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 noExtensionFactoryis registered which supports the given extension type.X- if thrown by the callback.X extends java.lang.Exception
-
onDemand
public <E> E onDemand(java.lang.Class<E> extensionType)
Creates an extension instance that uses the currentJdbiinstance 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.
-
-