]> WPIA git - gigi.git/commitdiff
fix: deadlock possibility in "DatabaseConnection"
authorFelix Dörre <felix@dogcraft.de>
Thu, 15 Sep 2016 07:50:53 +0000 (09:50 +0200)
committerFelix Dörre <felix@dogcraft.de>
Thu, 15 Sep 2016 07:51:07 +0000 (09:51 +0200)
Change-Id: I987cd3d9a0940f1fe3cf9289ec7512b785eca5df

src/org/cacert/gigi/database/DatabaseConnection.java

index c22fc49af5c805b6d5f46a5e506f2c2df13f5327..672dbe30e336c5f6d4479485115d484eeed8b488 100644 (file)
@@ -344,22 +344,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();
         }
         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;
+
     }
 }
     }
 }