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 c22fc49..672dbe3 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();
-        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;
+
     }
 }