X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Fdatabase%2FDatabaseConnection.java;h=fd77be1ea1e7629d299f35a3c522205bf5b4f4f1;hp=e7c9848812f7637ced866d04942703cd7386fddb;hb=d653987f7a1bcfb28948d162097994f01fcad379;hpb=9def69bd08ea69eb27786d5b34f00e154e09e9f3 diff --git a/src/org/cacert/gigi/database/DatabaseConnection.java b/src/org/cacert/gigi/database/DatabaseConnection.java index e7c98488..fd77be1e 100644 --- a/src/org/cacert/gigi/database/DatabaseConnection.java +++ b/src/org/cacert/gigi/database/DatabaseConnection.java @@ -122,7 +122,7 @@ public class DatabaseConnection { } - public static final int CURRENT_SCHEMA_VERSION = 20; + public static final int CURRENT_SCHEMA_VERSION = 25; public static final int CONNECTION_TIMEOUT = 24 * 60 * 60; @@ -235,6 +235,24 @@ public class DatabaseConnection { } credentials = conf; try (Link i = newLink(false)) { + try (GigiPreparedStatement empty = new GigiPreparedStatement("SELECT * from information_schema.tables WHERE table_schema='public' AND table_name='schemeVersion'")) { + if ( !empty.executeQuery().next()) { + try (InputStream resourceAsStream = DatabaseConnection.class.getResourceAsStream("tableStructure.sql")) { + if (resourceAsStream == null) { + throw new Error("DB-Install-Script not found."); + } + try (Statement s = getInstance().c.createStatement()) { + SQLFileManager.addFile(s, resourceAsStream, ImportType.PRODUCTION); + s.executeBatch(); + } + } + return; + } + } catch (IOException e) { + throw new Error(e); + } catch (SQLException e) { + throw new Error(e); + } int version = 0; try (GigiPreparedStatement gigiPreparedStatement = new GigiPreparedStatement("SELECT version FROM \"schemeVersion\" ORDER BY version DESC LIMIT 1;")) { GigiResultSet rs = gigiPreparedStatement.executeQuery(); @@ -256,8 +274,7 @@ public class DatabaseConnection { private static void upgrade(int version) { try { - Statement s = getInstance().c.createStatement(); - try { + try (Statement s = getInstance().c.createStatement()) { while (version < CURRENT_SCHEMA_VERSION) { addUpgradeScript(Integer.toString(version), s); version++; @@ -266,8 +283,6 @@ public class DatabaseConnection { System.out.println("UPGRADING Database to version " + version); s.executeBatch(); System.out.println("done."); - } finally { - s.close(); } } catch (SQLException e) { e.printStackTrace(); @@ -344,22 +359,28 @@ public class DatabaseConnection { } } - public static synchronized Link newLink(boolean readOnly) throws InterruptedException { - if (instances.get(Thread.currentThread()) != null) { - throw new Error("There is already a connection allocated for this thread."); - } - if (pool.isEmpty() && connCount < 5) { - pool.addLast(new DatabaseConnection()); - connCount++; + public static Link newLink(boolean readOnly) throws InterruptedException { + synchronized (DatabaseConnection.class) { + + if (instances.get(Thread.currentThread()) != null) { + throw new Error("There is already a connection allocated for this thread."); + } + if (pool.isEmpty() && connCount < 5) { + pool.addLast(new DatabaseConnection()); + connCount++; + } } DatabaseConnection conn = pool.takeFirst(); - try { - conn.c.setReadOnly(readOnly); - } catch (SQLException e) { - throw new Error(e); + synchronized (DatabaseConnection.class) { + try { + conn.c.setReadOnly(readOnly); + } catch (SQLException e) { + throw new Error(e); + } + Link l = new Link(conn); + instances.put(Thread.currentThread(), l); + return l; } - Link l = new Link(conn); - instances.put(Thread.currentThread(), l); - return l; + } }