From: Felix Dörre Date: Thu, 15 Sep 2016 07:50:53 +0000 (+0200) Subject: fix: deadlock possibility in "DatabaseConnection" X-Git-Url: https://code.wpia.club/?p=gigi.git;a=commitdiff_plain;h=7689e5f2beef288a40be98470ae3be8452acf8b5 fix: deadlock possibility in "DatabaseConnection" Change-Id: I987cd3d9a0940f1fe3cf9289ec7512b785eca5df --- diff --git a/src/org/cacert/gigi/database/DatabaseConnection.java b/src/org/cacert/gigi/database/DatabaseConnection.java index c22fc49a..672dbe30 100644 --- a/src/org/cacert/gigi/database/DatabaseConnection.java +++ b/src/org/cacert/gigi/database/DatabaseConnection.java @@ -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; + } }