Class AbstractDatabase

java.lang.Object
com.github.darksoulq.abyssallib.common.database.relational.AbstractDatabase
Direct Known Subclasses:
Database, Database, Database, Database, Database

public abstract class AbstractDatabase extends Object
An abstract base class representing a database connection provider and transaction manager. It manages an ExecutorService for off-thread operations and provides robust transaction handling with automatic rollback capability.
  • Constructor Details

    • AbstractDatabase

      public AbstractDatabase(ExecutorService asyncPool)
      Initializes the database manager with an execution pool.
      Parameters:
      asyncPool - The ExecutorService to use for background tasks.
  • Method Details

    • getConnection

      public abstract Connection getConnection() throws SQLException
      Retrieves a connection from the database or connection pool.
      Returns:
      A valid Connection object.
      Throws:
      SQLException - If a database access error occurs.
    • getAsyncPool

      public ExecutorService getAsyncPool()
      Provides access to the asynchronous executor pool.
      Returns:
      The current ExecutorService.
    • executeTransaction

      public void executeTransaction(Consumer<Connection> action)
      Executes a database operation within a transaction that does not return a result. The transaction is automatically committed on success and rolled back on failure.
      Parameters:
      action - A Consumer receiving the Connection to perform tasks.
      Throws:
      RuntimeException - If the transaction fails or a database error occurs.
    • executeTransactionResult

      public <T> T executeTransactionResult(Function<Connection, T> action)
      Executes a database operation within a transaction and returns a value. Handles manual commit/rollback and ensures the auto-commit state is restored.
      Type Parameters:
      T - The type of the result returned by the transaction.
      Parameters:
      action - A Function receiving the Connection and returning a value of type T.
      Returns:
      The result produced by the action.
      Throws:
      RuntimeException - If the transaction fails or a database error occurs.