diff --git a/src/wres/Functions.java b/src/wres/Functions.java index 3ca021c257..3fa454d84f 100644 --- a/src/wres/Functions.java +++ b/src/wres/Functions.java @@ -856,7 +856,16 @@ private static void openResources( SharedResources sharedResources ) && ( Objects.isNull( Functions.database ) || Functions.database.isShutdown() ) ) { - Functions.database = new Database( new ConnectionSupplier( sharedResources.systemSettings ) ); + // Try to create a database connection and catch exceptions to log the reason why our connection failed + try + { + Functions.database = new Database( new ConnectionSupplier( sharedResources.systemSettings ) ); + } + catch ( IllegalStateException ise ) + { + LOGGER.error( "Unable to create database with: ", ise ); + } + // Migrate the database, as needed if ( Functions.database.getAttemptToMigrate() ) { @@ -866,7 +875,7 @@ private static void openResources( SharedResources sharedResources ) } catch ( SQLException e ) { - throw new IllegalStateException( "Failed to migrate the WRES database.", e ); + throw new InternalWresException( "Failed to migrate the WRES database.", e ); } } } @@ -932,6 +941,10 @@ private static void logExecution( SharedResources sharedResources, if ( sharedResources.systemSettings() .isUseDatabase() ) { + if ( Objects.isNull( Functions.database ) ) + { + throw new InternalWresException( "Unable to log failure in database because connection does not exist" ); + } Instant endedExecution = Instant.now(); // Log both the operation and the args diff --git a/wres-io/src/wres/io/database/ConnectionSupplier.java b/wres-io/src/wres/io/database/ConnectionSupplier.java index 69a2cc5428..b6a8bfe01c 100644 --- a/wres-io/src/wres/io/database/ConnectionSupplier.java +++ b/wres-io/src/wres/io/database/ConnectionSupplier.java @@ -6,6 +6,7 @@ import com.github.marschall.jfr.jdbc.JfrDataSource; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; +import com.zaxxer.hikari.pool.HikariPool; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -80,7 +81,15 @@ private DataSource makeConnectionPool() int maxPoolSize = databaseConfiguration.getMaxPoolSize(); LOGGER.info( "Creating a database connection pool with {} connections...", maxPoolSize ); long connectionTimeoutMs = databaseConfiguration.getConnectionTimeoutMs(); - DataSource inner = createDataSource( maxPoolSize, connectionTimeoutMs ); + DataSource inner; + try + { + inner = createDataSource( maxPoolSize, connectionTimeoutMs ); + } + catch ( HikariPool.PoolInitializationException e ) + { + throw new IllegalStateException( "Unable to create pool", e ); + } return new JfrDataSource( inner ); // Monitor JDBC traffic with JFR: #61680 }