]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/database/DatabaseConnection.java
upd: use try-with-resources to protect JDBC-Statement
[gigi.git] / src / org / cacert / gigi / database / DatabaseConnection.java
index b5684edd8bf8d4b0db46b2cbe500df7327ce1d2f..fd77be1ea1e7629d299f35a3c522205bf5b4f4f1 100644 (file)
@@ -122,7 +122,7 @@ public class DatabaseConnection {
 
     }
 
-    public static final int CURRENT_SCHEMA_VERSION = 23;
+    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;
+
     }
 }