java.lang.Object
org.dalesbred.DatabaseAccess
org.dalesbred.DatabaseConnection
- All Implemented Interfaces:
AutoCloseable
A database connection with an explicit lifetime managed by the caller.
Wraps a single JDBC
Connection opened from a DatabaseSource.
Instances are obtained from DatabaseSource.openConnection(), not constructed
directly. Use try-with-resources to ensure the connection is always closed:
try (DatabaseConnection conn = source.openConnection()) {
conn.update("insert into orders ...");
conn.update("insert into order_items ...");
}
- See Also:
-
Field Summary
Fields inherited from class org.dalesbred.DatabaseAccess
dialect, instantiatorRegistry -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Commits the pending transaction and closes the underlying JDBC connection.@NotNull ConnectionReturns the underlying JDBC connection.voidMarks the current transaction as rollback-only, indicating that the transaction should be rolled back rather than committed when it is finalized.protected <T> TwithCurrentTransaction(@NotNull SqlQuery query, @NotNull TransactionCallback<T> callback) 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
-
Method Details
-
withCurrentTransaction
protected <T> T withCurrentTransaction(@NotNull @NotNull SqlQuery query, @NotNull @NotNull TransactionCallback<T> callback) - Specified by:
withCurrentTransactionin classDatabaseAccess
-
setRollbackOnly
public void setRollbackOnly()Marks the current transaction as rollback-only, indicating that the transaction should be rolled back rather than committed when it is finalized. -
getConnection
Returns the underlying JDBC connection. -
close
public void close()Commits the pending transaction and closes the underlying JDBC connection.- Specified by:
closein interfaceAutoCloseable- Throws:
DatabaseException- if the commit, rollback, or close raises a SQL error
-