Package org.dalesbred

Class DatabaseConnection

java.lang.Object
org.dalesbred.DatabaseAccess
org.dalesbred.DatabaseConnection
All Implemented Interfaces:
AutoCloseable

public final class DatabaseConnection extends DatabaseAccess implements 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:
  • Method Details

    • withCurrentTransaction

      protected <T> T withCurrentTransaction(@NotNull @NotNull SqlQuery query, @NotNull @NotNull TransactionCallback<T> callback)
      Specified by:
      withCurrentTransaction in class DatabaseAccess
    • 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

      @NotNull public @NotNull Connection getConnection()
      Returns the underlying JDBC connection.
    • close

      public void close()
      Commits the pending transaction and closes the underlying JDBC connection.
      Specified by:
      close in interface AutoCloseable
      Throws:
      DatabaseException - if the commit, rollback, or close raises a SQL error