Class Database

java.lang.Object
com.github.darksoulq.abyssallib.common.database.relational.AbstractDatabase
com.github.darksoulq.abyssallib.common.database.relational.postgres.Database

public class Database extends AbstractDatabase
A PostgreSQL database implementation that manages remote connections.

This class handles persistent connections to a PostgreSQL server, featuring an automated keep-alive scheduler to prevent connection timeouts and thread-safe connection retrieval.

  • Constructor Details

    • Database

      public Database(String host, int port, String database, String user, String password)
      Constructs a new PostgreSQL Database instance.
      Parameters:
      host - The server hostname.
      port - The server port.
      database - The database name.
      user - The login username.
      password - The login password.
  • Method Details

    • connect

      public void connect() throws Exception
      Establishes a connection to the PostgreSQL database and starts the keep-alive task.
      Throws:
      Exception - If the driver is missing or the connection fails.
    • disconnect

      public void disconnect() throws Exception
      Shuts down the keep-alive scheduler, the async pool, and closes the JDBC connection.
      Throws:
      Exception - If a database access error occurs during closure.
    • getConnection

      public Connection getConnection() throws SQLException
      Retrieves the active connection, automatically reconnecting if it has been closed or invalidated.
      Specified by:
      getConnection in class AbstractDatabase
      Returns:
      The active Connection.
      Throws:
      SQLException - If reconnection fails.
    • executor

      public QueryExecutor executor()
      Creates a new QueryExecutor instance for this database.
      Returns:
      A new QueryExecutor.
    • transaction

      public void transaction(Consumer<QueryExecutor> action)
      Executes an operation within a SQL transaction using a QueryExecutor.
      Parameters:
      action - A Consumer that performs the transaction logic.
    • transactionResult

      public <T> T transactionResult(Function<QueryExecutor, T> action)
      Executes an operation within a SQL transaction and returns a computed result.
      Type Parameters:
      T - The return type.
      Parameters:
      action - A Function that performs the transaction logic and returns a result.
      Returns:
      The result of the transaction.