diff --git a/src/main/java/de/diddiz/LogBlock/EntityTypeConverter.java b/src/main/java/de/diddiz/LogBlock/EntityTypeConverter.java index 83e8c1b..b55c6e5 100644 --- a/src/main/java/de/diddiz/LogBlock/EntityTypeConverter.java +++ b/src/main/java/de/diddiz/LogBlock/EntityTypeConverter.java @@ -18,7 +18,9 @@ public class EntityTypeConverter { public static int getOrAddEntityTypeId(EntityType entityType) { Integer key = entityTypeToId.get(entityType); - while (key == null) { + int tries = 0; + while (key == null && tries < 10) { + tries++; key = nextEntityTypeId; Connection conn = LogBlock.getInstance().getConnection(); try { @@ -34,8 +36,12 @@ public class EntityTypeConverter { } else { initializeEntityTypes(conn); } - } catch (SQLException e) { + } catch (Exception e) { LogBlock.getInstance().getLogger().log(Level.SEVERE, "Could not update lb-entitytypes", e); + reinitializeEntityTypesCatchException(); + if (tries == 10) { + throw new RuntimeException(e); + } } finally { try { conn.close(); @@ -52,6 +58,21 @@ public class EntityTypeConverter { return idToEntityType[entityTypeId]; } + private static void reinitializeEntityTypesCatchException() { + Connection conn = LogBlock.getInstance().getConnection(); + try { + initializeEntityTypes(conn); + } catch (Exception e) { + LogBlock.getInstance().getLogger().log(Level.SEVERE, "Could not reinitialize lb-entitytypes", e); + } finally { + try { + conn.close(); + } catch (Exception e) { + // ignored + } + } + } + public static void initializeEntityTypes(Connection connection) throws SQLException { Statement smt = connection.createStatement(); ResultSet rs = smt.executeQuery("SELECT id, name FROM `lb-entitytypes`"); diff --git a/src/main/java/de/diddiz/LogBlock/MaterialConverter.java b/src/main/java/de/diddiz/LogBlock/MaterialConverter.java index ff89953..8babf3d 100644 --- a/src/main/java/de/diddiz/LogBlock/MaterialConverter.java +++ b/src/main/java/de/diddiz/LogBlock/MaterialConverter.java @@ -42,7 +42,9 @@ public class MaterialConverter { materialString = blockDataString.substring(0, dataPart); } Integer key = materialToID.get(materialString); - while (key == null) { + int tries = 0; + while (key == null && tries < 10) { + tries++; key = nextMaterialId; Connection conn = LogBlock.getInstance().getConnection(); try { @@ -58,8 +60,12 @@ public class MaterialConverter { } else { initializeMaterials(conn); } - } catch (SQLException e) { + } catch (Exception e) { LogBlock.getInstance().getLogger().log(Level.SEVERE, "Could not update lb-materials", e); + reinitializeMaterialsCatchException(); + if (tries == 10) { + throw new RuntimeException(e); + } } finally { try { conn.close(); @@ -79,7 +85,9 @@ public class MaterialConverter { } String materialString = blockDataString.substring(dataPart); Integer key = blockStateToID.get(materialString); - while (key == null) { + int tries = 0; + while (key == null && tries < 10) { + tries++; key = nextBlockStateId; Connection conn = LogBlock.getInstance().getConnection(); try { @@ -95,8 +103,12 @@ public class MaterialConverter { } else { initializeMaterials(conn); } - } catch (SQLException e) { + } catch (Exception e) { LogBlock.getInstance().getLogger().log(Level.SEVERE, "Could not update lb-blockstates", e); + reinitializeMaterialsCatchException(); + if (tries == 10) { + throw new RuntimeException(e); + } } finally { try { conn.close(); @@ -121,6 +133,21 @@ public class MaterialConverter { return materialKeyToMaterial.get(idToMaterial[materialId]); } + private static void reinitializeMaterialsCatchException() { + Connection conn = LogBlock.getInstance().getConnection(); + try { + initializeMaterials(conn); + } catch (Exception e) { + LogBlock.getInstance().getLogger().log(Level.SEVERE, "Could not reinitialize lb-materials", e); + } finally { + try { + conn.close(); + } catch (Exception e) { + // ignored + } + } + } + public static void initializeMaterials(Connection connection) throws SQLException { Statement smt = connection.createStatement(); ResultSet rs = smt.executeQuery("SELECT id, name FROM `lb-materials`");