Class DbConnectionPool

java.lang.Object
com.tbg.wms.db.DbConnectionPool
All Implemented Interfaces:
AutoCloseable

public final class DbConnectionPool extends Object implements AutoCloseable
Manages the Oracle database connection pool using HikariCP.

This service is responsible for:

  • Creating and configuring the HikariCP connection pool
  • Validating connectivity with detailed error diagnostics
  • Mapping raw SQL exceptions to WmsDbConnectivityException with remediation hints
  • Providing connection lifecycle management (open/close)

Usage:


 AppConfig config = new AppConfig();
 DbConnectionPool pool = new DbConnectionPool(config);
 pool.testConnectivity(); // throws WmsDbConnectivityException on failure

 // Later, obtain connections via DataSource
 DataSource ds = pool.getDataSource();
 try (Connection c = ds.getConnection()) {
     // use connection
 }

 // Finally, close pool at shutdown
 pool.close();
 
  • Constructor Details

    • DbConnectionPool

      public DbConnectionPool(AppConfig config)
      Creates a new connection pool with the given configuration.

      The pool is created immediately but not validated. Call testConnectivity() to verify the database is reachable.

      Parameters:
      config - the application configuration containing Oracle connection details
      Throws:
      WmsDbConnectivityException - if pool creation fails
  • Method Details

    • getDataSource

      public DataSource getDataSource()
      Returns the underlying DataSource.
      Returns:
      the HikariDataSource for obtaining connections
    • testConnectivity

      public DbConnectivityDiagnostics testConnectivity()
      Tests connectivity to the database and returns detailed diagnostics.

      This method executes a simple query (SELECT 1 FROM dual) to verify:

      • Host is reachable (DNS, network, firewall)
      • Port is open
      • Service/SID exists
      • Credentials are valid
      • Connection pool can create connections
      Returns:
      diagnostic information (pool stats, driver version, etc.)
      Throws:
      WmsDbConnectivityException - if connectivity test fails
    • close

      public void close()
      Closes the connection pool and releases all resources. Should be called on application shutdown.
      Specified by:
      close in interface AutoCloseable
    • activeJdbcUrl

      public String activeJdbcUrl()
      Returns the JDBC URL that successfully created the pool.
      Returns:
      active JDBC URL