X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Fdatabase%2FDatabaseConnection.java;h=5d987191a248452717c61e312f77d28fceaa6b95;hp=49658fb7b3c7eed83aec542b3089d6c8a3867582;hb=ff0add5c210e30565099394d8d6b4c3f5595c6c6;hpb=53a66914a544d38be7e626aec2e74542745d4fda diff --git a/src/org/cacert/gigi/database/DatabaseConnection.java b/src/org/cacert/gigi/database/DatabaseConnection.java index 49658fb7..5d987191 100644 --- a/src/org/cacert/gigi/database/DatabaseConnection.java +++ b/src/org/cacert/gigi/database/DatabaseConnection.java @@ -41,10 +41,13 @@ public class DatabaseConnection { try { c = DriverManager.getConnection(credentials.getProperty("sql.url") + "?zeroDateTimeBehavior=convertToNull", credentials.getProperty("sql.user"), credentials.getProperty("sql.password")); PreparedStatement ps = c.prepareStatement("SET SESSION wait_timeout=?, time_zone='+0:00';"); - ps.setInt(1, CONNECTION_TIMEOUT); - ps.execute(); - ps.close(); - adHoc = c.createStatement(); + try { + ps.setInt(1, CONNECTION_TIMEOUT); + ps.execute(); + adHoc = c.createStatement(); + } finally { + ps.close(); + } } catch (SQLException e) { e.printStackTrace(); } @@ -119,19 +122,23 @@ public class DatabaseConnection { private static void upgrade(int version) { try { Statement s = getInstance().c.createStatement(); - while (version < CURRENT_SCHEMA_VERSION) { - InputStream resourceAsStream = DatabaseConnection.class.getResourceAsStream("upgrade/from_" + version + ".sql"); - if (resourceAsStream == null) { - throw new Error("Upgrade script from version " + version + " was not found."); + try { + while (version < CURRENT_SCHEMA_VERSION) { + try (InputStream resourceAsStream = DatabaseConnection.class.getResourceAsStream("upgrade/from_" + version + ".sql")) { + if (resourceAsStream == null) { + throw new Error("Upgrade script from version " + version + " was not found."); + } + SQLFileManager.addFile(s, resourceAsStream, ImportType.PRODUCTION); + } + version++; } - SQLFileManager.addFile(s, resourceAsStream, ImportType.PRODUCTION); - version++; + s.addBatch("UPDATE schemeVersion SET version='" + version + "'"); + System.out.println("UPGRADING Database to version " + version); + s.executeBatch(); + System.out.println("done."); + } finally { + s.close(); } - s.addBatch("INSERT INTO schemeVersion SET version='" + version + "'"); - System.out.println("UPGRADING Database to version " + version); - s.executeBatch(); - System.out.println("done."); - s.close(); } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) {