Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding better error handling for database issues #33

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading