Class Database

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

public class Database extends AbstractDatabase
An H2 database implementation managing local file-based storage.

This class handles connection lifecycles for H2, automatically managing directory creation and filename sanitization for the MVStore format.

  • Constructor Details

    • Database

      public Database(File file)
      Constructs a new H2 Database instance.
      Parameters:
      file - The File representing the database location.
  • Method Details

    • connect

      public void connect() throws Exception
      Establishes a connection to the H2 database.

      Automatically creates parent directories and strips the ".mv.db" suffix from the path if present to satisfy JDBC URL requirements. Configured with MODE=MySQL for compatibility and AUTO_SERVER=TRUE for multi-connection support.

      Throws:
      Exception - If connection or directory creation fails.
    • disconnect

      public void disconnect() throws Exception
      Closes the database connection and shuts down the asynchronous pool.
      Throws:
      Exception - If a database access error occurs.
    • getConnection

      public Connection getConnection() throws SQLException
      Retrieves the current connection, attempting to reconnect if it has been closed.
      Specified by:
      getConnection in class AbstractDatabase
      Returns:
      A valid Connection.
      Throws:
      SQLException - If reconnection is unsuccessful.
    • executor

      public QueryExecutor executor()
      Returns:
      A new QueryExecutor linked to this database.
    • transaction

      public void transaction(Consumer<QueryExecutor> action)
      Executes logic within a transaction.
      Parameters:
      action - A Consumer receiving a transactional QueryExecutor.
    • transactionResult

      public <T> T transactionResult(Function<QueryExecutor, T> action)
      Executes logic within a transaction and returns a result.
      Type Parameters:
      T - The result type.
      Parameters:
      action - A Function receiving a transactional QueryExecutor.
      Returns:
      The result of the transaction.