Shifted GSON conversion to main thread to avoid concurrent modification

https://github.com/BentoBoxWorld/BentoBox/issues/968
This commit is contained in:
tastybento 2019-09-30 15:38:00 -07:00
parent 2f7a4b2528
commit 8c037fc1a3
2 changed files with 6 additions and 7 deletions

View File

@ -155,17 +155,16 @@ public class SQLDatabaseHandler<T> extends AbstractJSONDatabaseHandler<T> {
return; return;
} }
// Async // Async
processQueue.add(() -> store(instance, sqlConfig.getSaveObjectSQL())); processQueue.add(() -> store(instance.getClass().getName(), getGson().toJson(instance), sqlConfig.getSaveObjectSQL()));
} }
private void store(T instance, String sb) { private void store(String name, String toStore, String sb) {
String toStore = getGson().toJson(instance);
try (PreparedStatement preparedStatement = connection.prepareStatement(sb)) { try (PreparedStatement preparedStatement = connection.prepareStatement(sb)) {
preparedStatement.setString(1, toStore); preparedStatement.setString(1, toStore);
preparedStatement.setString(2, toStore); preparedStatement.setString(2, toStore);
preparedStatement.execute(); preparedStatement.execute();
} catch (SQLException e) { } catch (SQLException e) {
plugin.logError("Could not save object " + instance.getClass().getName() + " " + e.getMessage()); plugin.logError("Could not save object " + name + " " + e.getMessage());
} }
} }

View File

@ -49,9 +49,9 @@ public class SQLiteDatabaseHandler<T> extends SQLDatabaseHandler<T> {
plugin.logError("This class is not a DataObject: " + instance.getClass().getName()); plugin.logError("This class is not a DataObject: " + instance.getClass().getName());
return; return;
} }
processQueue.add(() -> { Gson gson = getGson();
Gson gson = getGson(); String toStore = gson.toJson(instance);
String toStore = gson.toJson(instance); processQueue.add(() -> {
try (PreparedStatement preparedStatement = getConnection().prepareStatement(getSqlConfig().getSaveObjectSQL())) { try (PreparedStatement preparedStatement = getConnection().prepareStatement(getSqlConfig().getSaveObjectSQL())) {
preparedStatement.setString(1, toStore); preparedStatement.setString(1, toStore);
preparedStatement.setString(2, ((DataObject)instance).getUniqueId()); preparedStatement.setString(2, ((DataObject)instance).getUniqueId());