Class Database

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

public class Database extends AbstractDatabase
A MySQL database implementation that handles remote connectivity, connection pooling, and a keep-alive scheduler to prevent connection timeouts.
  • Constructor Details

    • Database

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

    • connect

      public void connect() throws Exception
      Connects to the MySQL server using JDBC. Configures SSL, public key retrieval, auto-reconnect, and UTF-8 encoding. Starts a heartbeat task that runs every 30 minutes.
      Throws:
      Exception - If the connection fails.
    • disconnect

      public void disconnect() throws Exception
      Shuts down the keep-alive scheduler, the async thread pool, and closes the SQL connection.
      Throws:
      Exception - If an error occurs during disconnection.
    • getConnection

      public Connection getConnection() throws SQLException
      Provides the active SQL connection. If the connection is closed or invalid, it attempts to reconnect synchronously.
      Specified by:
      getConnection in class AbstractDatabase
      Returns:
      A valid Connection.
      Throws:
      SQLException - If reconnection fails.
    • executor

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

      public void transaction(Consumer<QueryExecutor> action)
      Executes a transaction using a QueryExecutor.
      Parameters:
      action - The consumer logic.
    • transactionResult

      public <T> T transactionResult(Function<QueryExecutor, T> action)
      Executes a transaction that returns a result using a QueryExecutor.
      Type Parameters:
      T - The return type.
      Parameters:
      action - The function logic.
      Returns:
      The transaction result.