java.lang.Object
org.dalesbred.DatabaseAccess
org.dalesbred.Database
The main abstraction of the library: represents a configured connection to database and provides a way to execute callbacks in transactions.
Usually you'll need only single instance of this in your application, unless you need to connect several different databases or need different default settings for different use cases.
-
Field Summary
Fields inherited from class org.dalesbred.DatabaseAccess
dialect, instantiatorRegistry -
Constructor Summary
ConstructorsConstructorDescriptionDatabase(@NotNull DataSource dataSource) Constructs a new Database that uses givenDataSourceand auto-detects the dialect to use.Database(@NotNull DataSource dataSource, @NotNull Dialect dialect) Constructs a new Database that uses givenDataSourceandDialect.Database(@NotNull ConnectionProvider connectionProvider) Constructs a new Database that uses givenConnectionProviderand auto-detects the dialect to use.Database(@NotNull ConnectionProvider connectionProvider, @NotNull Dialect dialect) Constructs a new Database that uses givenConnectionProviderandDialect.Database(@NotNull TransactionManager transactionManager) Constructs a new Database that uses givenTransactionManagerand auto-detects the dialect to use.Database(@NotNull TransactionManager transactionManager, @NotNull Dialect dialect) Constructs a new Database that uses givenTransactionManagerandDialect. -
Method Summary
Modifier and TypeMethodDescriptionstatic @NotNull DatabaseforDataSource(@NotNull DataSource dataSource) Returns a new Database that uses givenDataSourceto retrieve connections.static @NotNull DatabaseforJndiDataSource(@NotNull String jndiName) Returns a new Database that usesDataSourcewith given JNDI-name.static @NotNull DatabaseforUrlAndCredentials(@NotNull String url, @Nullable String username, @Nullable String password) Returns a new Database that uses given connection options to open connection.@NotNull TypeConversionRegistryReturnsTypeConversionRegistrythat can be used to register new type-conversions.booleanReturns true if and only if the current thread has an active transaction for this database.booleanReturns whether queries outside an active transaction will start a fresh transaction (true, the default) or throwNoActiveTransactionException(false).voidsetAllowImplicitTransactions(boolean allowImplicitTransactions) Controls whether queries outside an active transaction automatically start a fresh transaction.@NotNull StringtoString()Returns a string containing useful debug information about the state of this object.protected <T> TwithCurrentTransaction(@NotNull SqlQuery query, @NotNull TransactionCallback<T> callback) Executes the block of code within context of current transaction.<T> TwithTransaction(@NotNull Isolation isolation, @NotNull TransactionCallback<T> callback) Executes a block of code with given isolation.<T> TwithTransaction(@NotNull Propagation propagation, @NotNull Isolation isolation, @NotNull TransactionCallback<T> callback) Executes a block of code with given propagation and isolation.<T> TwithTransaction(@NotNull Propagation propagation, @NotNull TransactionCallback<T> callback) Executes a block of code with given propagation and configuration default isolation.<T> TwithTransaction(@NotNull TransactionCallback<T> callback) Executes a block of code within a context of a transaction, usingPropagation.REQUIREDpropagation.<T> TwithTransaction(@NotNull TransactionSettings settings, @NotNull TransactionCallback<T> callback) Executes a block of code with given transaction settings.voidwithVoidTransaction(@NotNull Isolation isolation, @NotNull VoidTransactionCallback callback) Executes a block of code with given isolation.voidwithVoidTransaction(@NotNull Propagation propagation, @NotNull Isolation isolation, @NotNull VoidTransactionCallback callback) Executes a block of code with given propagation and isolation.voidwithVoidTransaction(@NotNull Propagation propagation, @NotNull VoidTransactionCallback callback) Executes a block of code with given propagation and configuration default isolation.voidwithVoidTransaction(@NotNull TransactionSettings settings, @NotNull VoidTransactionCallback callback) Executes a block of code with given transaction settings.voidwithVoidTransaction(@NotNull VoidTransactionCallback callback) Executes a block of code within a context of a transaction, usingPropagation.REQUIREDpropagation.Methods inherited from class org.dalesbred.DatabaseAccess
bindArguments, bindQueryParameters, executeQuery, executeQuery, findAll, findAll, findAll, findAll, findMap, findMap, findOptional, findOptional, findOptional, findOptional, findOptionalDouble, findOptionalDouble, findOptionalInt, findOptionalInt, findOptionalLong, findOptionalLong, findTable, findTable, findUnique, findUnique, findUnique, findUnique, findUniqueBoolean, findUniqueBoolean, findUniqueInt, findUniqueInt, findUniqueLong, findUniqueLong, findUniqueOrNull, findUniqueOrNull, findUniqueOrNull, findUniqueOrNull, getDefaultTimeout, prepareStatementFromQuery, setDefaultTimeout, update, update, updateAndProcessGeneratedKeys, updateAndProcessGeneratedKeys, updateBatch, updateBatchAndProcessGeneratedKeys, updateUnique, updateUnique
-
Constructor Details
-
Database
Constructs a new Database that uses givenConnectionProviderand auto-detects the dialect to use. -
Database
public Database(@NotNull @NotNull ConnectionProvider connectionProvider, @NotNull @NotNull Dialect dialect) Constructs a new Database that uses givenConnectionProviderandDialect. -
Database
Constructs a new Database that uses givenTransactionManagerand auto-detects the dialect to use. -
Database
public Database(@NotNull @NotNull TransactionManager transactionManager, @NotNull @NotNull Dialect dialect) Constructs a new Database that uses givenTransactionManagerandDialect. -
Database
Constructs a new Database that uses givenDataSourceand auto-detects the dialect to use. -
Database
Constructs a new Database that uses givenDataSourceandDialect.
-
-
Method Details
-
forDataSource
Returns a new Database that uses givenDataSourceto retrieve connections. -
forJndiDataSource
Returns a new Database that usesDataSourcewith given JNDI-name. -
forUrlAndCredentials
@NotNull public static @NotNull Database forUrlAndCredentials(@NotNull @NotNull String url, @Nullable @Nullable String username, @Nullable @Nullable String password) Returns a new Database that uses given connection options to open connection. The database opens connections directly fromDriverManagerwithout performing connection pooling.- See Also:
-
withTransaction
Executes a block of code within a context of a transaction, usingPropagation.REQUIREDpropagation. -
withTransaction
public <T> T withTransaction(@NotNull @NotNull Propagation propagation, @NotNull @NotNull TransactionCallback<T> callback) Executes a block of code with given propagation and configuration default isolation. -
withTransaction
public <T> T withTransaction(@NotNull @NotNull Isolation isolation, @NotNull @NotNull TransactionCallback<T> callback) Executes a block of code with given isolation. -
withTransaction
public <T> T withTransaction(@NotNull @NotNull Propagation propagation, @NotNull @NotNull Isolation isolation, @NotNull @NotNull TransactionCallback<T> callback) Executes a block of code with given propagation and isolation. -
withTransaction
public <T> T withTransaction(@NotNull @NotNull TransactionSettings settings, @NotNull @NotNull TransactionCallback<T> callback) Executes a block of code with given transaction settings.- See Also:
-
withVoidTransaction
Executes a block of code within a context of a transaction, usingPropagation.REQUIREDpropagation. -
withVoidTransaction
public void withVoidTransaction(@NotNull @NotNull Propagation propagation, @NotNull @NotNull VoidTransactionCallback callback) Executes a block of code with given propagation and configuration default isolation. -
withVoidTransaction
public void withVoidTransaction(@NotNull @NotNull Isolation isolation, @NotNull @NotNull VoidTransactionCallback callback) Executes a block of code with given isolation. -
withVoidTransaction
public void withVoidTransaction(@NotNull @NotNull Propagation propagation, @NotNull @NotNull Isolation isolation, @NotNull @NotNull VoidTransactionCallback callback) Executes a block of code with given propagation and isolation. -
withVoidTransaction
public void withVoidTransaction(@NotNull @NotNull TransactionSettings settings, @NotNull @NotNull VoidTransactionCallback callback) Executes a block of code with given transaction settings.- See Also:
-
hasActiveTransaction
public boolean hasActiveTransaction()Returns true if and only if the current thread has an active transaction for this database. -
withCurrentTransaction
protected <T> T withCurrentTransaction(@NotNull @NotNull SqlQuery query, @NotNull @NotNull TransactionCallback<T> callback) Executes the block of code within context of current transaction. If there's no transaction in progress throwsNoActiveTransactionExceptionunless implicit transaction are allowed: in this case, starts a new transaction.- Specified by:
withCurrentTransactionin classDatabaseAccess- Throws:
NoActiveTransactionException- if there's no active transaction.- See Also:
-
getTypeConversionRegistry
ReturnsTypeConversionRegistrythat can be used to register new type-conversions. -
isAllowImplicitTransactions
public boolean isAllowImplicitTransactions()Returns whether queries outside an active transaction will start a fresh transaction (true, the default) or throwNoActiveTransactionException(false). -
setAllowImplicitTransactions
public void setAllowImplicitTransactions(boolean allowImplicitTransactions) Controls whether queries outside an active transaction automatically start a fresh transaction. When set tofalse, calling query methods without an active transaction throwsNoActiveTransactionException. Defaults totrue. -
toString
Returns a string containing useful debug information about the state of this object.
-