Class AbstractDatabase
java.lang.Object
com.github.darksoulq.abyssallib.common.database.relational.AbstractDatabase
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 Summary
ConstructorsConstructorDescriptionAbstractDatabase(ExecutorService asyncPool) Initializes the database manager with an execution pool. -
Method Summary
Modifier and TypeMethodDescriptionvoidexecuteTransaction(Consumer<Connection> action) Executes a database operation within a transaction that does not return a result.<T> TexecuteTransactionResult(Function<Connection, T> action) Executes a database operation within a transaction and returns a value.Provides access to the asynchronous executor pool.abstract ConnectionRetrieves a connection from the database or connection pool.
-
Constructor Details
-
AbstractDatabase
Initializes the database manager with an execution pool.- Parameters:
asyncPool- TheExecutorServiceto use for background tasks.
-
-
Method Details
-
getConnection
Retrieves a connection from the database or connection pool.- Returns:
- A valid
Connectionobject. - Throws:
SQLException- If a database access error occurs.
-
getAsyncPool
Provides access to the asynchronous executor pool.- Returns:
- The current
ExecutorService.
-
executeTransaction
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- AConsumerreceiving theConnectionto perform tasks.- Throws:
RuntimeException- If the transaction fails or a database error occurs.
-
executeTransactionResult
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- AFunctionreceiving theConnectionand returning a value of typeT.- Returns:
- The result produced by the action.
- Throws:
RuntimeException- If the transaction fails or a database error occurs.
-