Fix repo connection closed not thrown/handled correctly

This commit is contained in:
James Seibel
2024-01-14 15:05:47 -06:00
parent 8a9f63ab25
commit 22c11b3eeb
2 changed files with 51 additions and 30 deletions
@@ -139,8 +139,9 @@ public abstract class AbstractDhRepo<TDTO extends IBaseDTO>
{
this.query(statement);
}
catch (DbConnectionClosedException ignored)
catch (DbConnectionClosedException ignored)
{
LOGGER.warn("Attempted to insert ["+this.dtoClass.getSimpleName()+"] with primary key ["+(dto != null ? dto.getPrimaryKeyString() : "NULL")+"] on closed repo ["+this.connectionString+"].");
}
catch (SQLException e)
{
@@ -155,8 +156,9 @@ public abstract class AbstractDhRepo<TDTO extends IBaseDTO>
{
this.query(statement);
}
catch (DbConnectionClosedException ignored)
catch (DbConnectionClosedException e)
{
LOGGER.warn("Attempted to update ["+this.dtoClass.getSimpleName()+"] with primary key ["+(dto != null ? dto.getPrimaryKeyString() : "NULL")+"] on closed repo ["+this.connectionString+"].");
}
catch (SQLException e)
{
@@ -236,12 +238,16 @@ public abstract class AbstractDhRepo<TDTO extends IBaseDTO>
// SQL exceptions generally only happen when something is wrong with
// the database or the query and should cause the system to blow up to notify the developer
if (e.toString().equals("database connection closed"))
if (DbConnectionClosedException.IsClosedException(e))
{
throw new DbConnectionClosedException(e);
String message = "Unexpected Query error: ["+e.getMessage()+"], for prepared statement: ["+statement+"].";
LOGGER.error(message);
throw new RuntimeException(message, e);
}
else
{
String message = "Unexpected Query error: [" + e.getMessage() + "], for prepared statement: [" + statement + "].";
LOGGER.error(message);
throw new RuntimeException(message, e);
}
}
}
/** note: this can only handle 1 command at a time */
@@ -263,12 +269,16 @@ public abstract class AbstractDhRepo<TDTO extends IBaseDTO>
// SQL exceptions generally only happen when something is wrong with
// the database or the query and should cause the system to blow up to notify the developer
if (e.toString().equals("database connection closed"))
if (DbConnectionClosedException.IsClosedException(e))
{
throw new DbConnectionClosedException(e);
String message = "Unexpected Query error: ["+e.getMessage()+"], for script: ["+sql+"].";
LOGGER.error(message);
throw new RuntimeException(message, e);
}
else
{
String message = "Unexpected Query error: [" + e.getMessage() + "], for script: [" + sql + "].";
LOGGER.error(message);
throw new RuntimeException(message, e);
}
}
}
private List<Map<String, Object>> parseQueryResult(ResultSet resultSet, boolean resultSetPresent) throws SQLException
@@ -291,7 +301,7 @@ public abstract class AbstractDhRepo<TDTO extends IBaseDTO>
}
public PreparedStatement createPreparedStatement(String sql)
public PreparedStatement createPreparedStatement(String sql) throws DbConnectionClosedException
{
try
{
@@ -301,9 +311,19 @@ public abstract class AbstractDhRepo<TDTO extends IBaseDTO>
}
catch(SQLException e)
{
// SQL exceptions generally only happen when something is wrong with
// the database or the query and should cause the system to blow up to notify the developer
throw new RuntimeException(e);
if (DbConnectionClosedException.IsClosedException(e))
{
throw new DbConnectionClosedException(e);
}
else
{
// SQL exceptions generally only happen when something is wrong with
// the database or the query and should cause the system to blow up to notify the developer
String message = "Unexpected error: [" + e.getMessage() + "], preparing statement: [" + sql + "].";
LOGGER.error(message);
throw new RuntimeException(message, e);
}
}
}
@@ -339,7 +359,7 @@ public abstract class AbstractDhRepo<TDTO extends IBaseDTO>
{
if(this.connection != null)
{
LOGGER.debug("Closing database connection ["+this.connectionString+"]");
LOGGER.info("Closing database connection ["+this.connectionString+"]");
CONNECTIONS_BY_CONNECTION_STRING.remove(this.connectionString);
this.connection.close();
}
@@ -1,20 +1,21 @@
package com.seibel.distanthorizons.core.sql;
public class DbConnectionClosedException extends Exception
import java.sql.SQLException;
/**
* Used to simplify handling when a database has been closed
* since Java doesn't have a specific exception to handle closed databases
*/
public class DbConnectionClosedException extends SQLException
{
public DbConnectionClosedException() {
super("The database connection is closed.");
}
public DbConnectionClosedException() { super("The database connection is closed."); }
public DbConnectionClosedException(String message) { super(message); }
public DbConnectionClosedException(String message, Throwable cause) { super(message, cause); }
public DbConnectionClosedException(Throwable cause) { super(cause); }
public DbConnectionClosedException(String message) {
super(message);
}
public DbConnectionClosedException(String message, Throwable cause) {
super(message, cause);
}
// helper methods //
public static boolean IsClosedException(SQLException e) { return e.getMessage().toLowerCase().contains("connection closed"); }
public DbConnectionClosedException(Throwable cause) {
super(cause);
}
}