From 7ba3fc0dda72be8218e75bc6058ca7d8538ad3af Mon Sep 17 00:00:00 2001 From: s809 <43530948+s809@users.noreply.github.com> Date: Sat, 4 Nov 2023 23:03:30 +0500 Subject: [PATCH] Ignore input and return empty data when connection is closed --- .../core/sql/AbstractDhRepo.java | 39 ++++++++++++++++--- .../core/sql/DbConnectionClosedException.java | 20 ++++++++++ 2 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 core/src/main/java/com/seibel/distanthorizons/core/sql/DbConnectionClosedException.java diff --git a/core/src/main/java/com/seibel/distanthorizons/core/sql/AbstractDhRepo.java b/core/src/main/java/com/seibel/distanthorizons/core/sql/AbstractDhRepo.java index 617adc11d..ef588d9be 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/sql/AbstractDhRepo.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/sql/AbstractDhRepo.java @@ -126,6 +126,9 @@ public abstract class AbstractDhRepo { this.query(statement); } + catch (DbConnectionClosedException ignored) + { + } catch (SQLException e) { String message = "Unexpected insert statement error: ["+e.getMessage()+"]."; @@ -139,6 +142,9 @@ public abstract class AbstractDhRepo { this.query(statement); } + catch (DbConnectionClosedException ignored) + { + } catch (SQLException e) { String message = "Unexpected update statement error: ["+e.getMessage()+"]."; @@ -172,17 +178,34 @@ public abstract class AbstractDhRepo // low level DB // //==============// - public List> queryDictionary(String sql) { return this.query(sql); } + public List> queryDictionary(String sql) + { + try + { + return this.query(sql); + } + catch (DbConnectionClosedException e) + { + return new ArrayList<>(); + } + } @Nullable public Map queryDictionaryFirst(String sql) { - List> objectList = this.query(sql); - return !objectList.isEmpty() ? objectList.get(0) : null; + try + { + List> objectList = this.query(sql); + return !objectList.isEmpty() ? objectList.get(0) : null; + } + catch (DbConnectionClosedException e) + { + return null; + } } /** note: this can only handle 1 command at a time */ - private List> query(PreparedStatement statement) throws RuntimeException + private List> query(PreparedStatement statement) throws RuntimeException, DbConnectionClosedException { try { @@ -200,13 +223,16 @@ public abstract class AbstractDhRepo // 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")) + throw new DbConnectionClosedException(e); + 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 */ - private List> query(String sql) throws RuntimeException + private List> query(String sql) throws RuntimeException, DbConnectionClosedException { try (Statement statement = this.connection.createStatement()) { @@ -224,6 +250,9 @@ public abstract class AbstractDhRepo // 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")) + throw new DbConnectionClosedException(e); + String message = "Unexpected Query error: ["+e.getMessage()+"], for script: ["+sql+"]."; LOGGER.error(message); throw new RuntimeException(message, e); diff --git a/core/src/main/java/com/seibel/distanthorizons/core/sql/DbConnectionClosedException.java b/core/src/main/java/com/seibel/distanthorizons/core/sql/DbConnectionClosedException.java new file mode 100644 index 000000000..4c9dd400a --- /dev/null +++ b/core/src/main/java/com/seibel/distanthorizons/core/sql/DbConnectionClosedException.java @@ -0,0 +1,20 @@ +package com.seibel.distanthorizons.core.sql; + +public class DbConnectionClosedException extends Exception +{ + 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); + } +}