Class Database

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

public class Database extends AbstractDatabase
A MariaDB database implementation managing remote JDBC connections.

This class includes a background scheduler to maintain connection stability through periodic keep-alive checks and provides thread-safe access to the underlying Connection.

  • Constructor Details

    • Database

      public Database(String host, int port, String database, String user, String password)
      Constructs a new MariaDB Database instance.
      Parameters:
      host - The server address.
      port - The connection port.
      database - The database name.
      user - The credential username.
      password - The credential password.
  • Method Details

    • connect

      public void connect() throws Exception
      Establishes the connection and starts the 30-minute keep-alive interval.
      Throws:
      Exception - If connection fails.
    • disconnect

      public void disconnect() throws Exception
      Gracefully shuts down the scheduler, async pool, and JDBC connection.
      Throws:
      Exception - If an error occurs during disconnect.
    • getConnection

      public Connection getConnection() throws SQLException
      Synchronized getter for the connection; performs a health check and auto-reconnects if the connection is dead.
      Specified by:
      getConnection in class AbstractDatabase
      Returns:
      A valid Connection.
      Throws:
      SQLException - If the database is unreachable.
    • executor

      public QueryExecutor executor()
      Spawns an executor for this database.
      Returns:
      A new QueryExecutor.
    • transaction

      public void transaction(Consumer<QueryExecutor> action)
      Executes a series of operations within a transactional scope.
      Parameters:
      action - The logic to execute.
    • transactionResult

      public <T> T transactionResult(Function<QueryExecutor, T> action)
      Executes a series of operations within a transactional scope and returns a value.
      Type Parameters:
      T - The result type.
      Parameters:
      action - The logic to execute.
      Returns:
      The result of type T.