Skip to content

Commit

Permalink
Adding better error handling for database issues
Browse files Browse the repository at this point in the history
When we are unable to connect to a database report the reason why
instead of wrapping and forgetting it. Also be more clear in the error
message for failure cause and remove NPE
  • Loading branch information
epag committed Jul 11, 2024
1 parent 1c709b9 commit dd34cad
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
17 changes: 15 additions & 2 deletions src/wres/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() )
{
Expand All @@ -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 );
}
}
}
Expand Down Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion wres-io/src/wres/io/database/ConnectionSupplier.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit dd34cad

Please sign in to comment.