Merge branch 'fix/dimension-switch-sql-errors' into 'main'

Ignore input and return empty data when database connection is closed

See merge request jeseibel/distant-horizons-core!35
This commit is contained in:
James Seibel
2023-11-10 13:02:28 +00:00
2 changed files with 54 additions and 5 deletions
@@ -126,6 +126,9 @@ public abstract class AbstractDhRepo<TDTO extends IBaseDTO>
{
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<TDTO extends IBaseDTO>
{
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<TDTO extends IBaseDTO>
// low level DB //
//==============//
public List<Map<String, Object>> queryDictionary(String sql) { return this.query(sql); }
public List<Map<String, Object>> queryDictionary(String sql)
{
try
{
return this.query(sql);
}
catch (DbConnectionClosedException e)
{
return new ArrayList<>();
}
}
@Nullable
public Map<String, Object> queryDictionaryFirst(String sql)
{
List<Map<String, Object>> objectList = this.query(sql);
return !objectList.isEmpty() ? objectList.get(0) : null;
try
{
List<Map<String, Object>> 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<Map<String, Object>> query(PreparedStatement statement) throws RuntimeException
private List<Map<String, Object>> query(PreparedStatement statement) throws RuntimeException, DbConnectionClosedException
{
try
{
@@ -200,13 +223,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"))
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<Map<String, Object>> query(String sql) throws RuntimeException
private List<Map<String, Object>> query(String sql) throws RuntimeException, DbConnectionClosedException
{
try (Statement statement = this.connection.createStatement())
{
@@ -224,6 +250,9 @@ 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"))
throw new DbConnectionClosedException(e);
String message = "Unexpected Query error: ["+e.getMessage()+"], for script: ["+sql+"].";
LOGGER.error(message);
throw new RuntimeException(message, e);
@@ -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);
}
}