Class SqlConnectionManager

java.lang.Object
tk.airshipcraft.commonlib.db.SqlConnectionManager

public class SqlConnectionManager extends Object
Manages SQL database connections using HikariCP for connection pooling. This manager is responsible for creating and configuring a pool of database connections that can be reused for database operations, improving performance by reducing the overhead of establishing connections for each operation.
Since:
2023-12-27
Version:
1.0.0
Author:
notzune
  • Constructor Details

    • SqlConnectionManager

      public SqlConnectionManager(DatabaseConfig config)
      Initializes a new SqlConnectionManager with the provided database configuration. Sets up a connection pool using HikariCP with the configuration settings specified in the provided DatabaseConfig object.
      Parameters:
      config - The database configuration object containing properties for setting up the connection pool.
  • Method Details

    • getConnection

      public Connection getConnection() throws SQLException
      Retrieves a database connection from the connection pool. The connection is ready to be used for executing SQL statements.
      Returns:
      A Connection object representing the database connection.
      Throws:
      SQLException - If a database access error occurs or the data source has been closed.
    • close

      public void close()
      Closes the data source and releases all pooled connections. This method should be called when the connection manager is no longer needed, typically at the application shutdown, to ensure that all resources are properly released.
    • beginTransaction

      public void beginTransaction(Connection conn) throws SQLException
      Begins a new database transaction.
      Parameters:
      conn - The database connection.
      Throws:
      SQLException - If an error occurs during setting auto-commit to false.
    • commitTransaction

      public void commitTransaction(Connection conn) throws SQLException
      Commits the current transaction.
      Parameters:
      conn - The database connection.
      Throws:
      SQLException - If an error occurs during the transaction commit.
    • rollbackTransaction

      public void rollbackTransaction(Connection conn)
      Rolls back the current transaction.
      Parameters:
      conn - The database connection.
    • setSavepoint

      public Savepoint setSavepoint(Connection conn, String name) throws SQLException
      Sets a savepoint in the current transaction.
      Parameters:
      conn - The database connection.
      name - The name of the savepoint.
      Returns:
      A Savepoint object.
      Throws:
      SQLException - If an error occurs during setting the savepoint.
    • releaseSavepoint

      public void releaseSavepoint(Connection conn, Savepoint savepoint) throws SQLException
      Releases a previously set savepoint.
      Parameters:
      conn - The database connection.
      savepoint - The savepoint to release.
      Throws:
      SQLException - If an error occurs during releasing the savepoint.
    • resetConnection

      public void resetConnection(Connection conn) throws SQLException
      Resets the connection to its default auto-commit state.
      Parameters:
      conn - The database connection.
      Throws:
      SQLException - If an error occurs during setting auto-commit to true.