Improve stability for material table updates

This commit is contained in:
Brokkonaut
2018-11-27 06:05:00 +01:00
parent ab464e1dd5
commit d5ee15ebba
2 changed files with 54 additions and 6 deletions

View File

@ -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`");

View File

@ -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`");