Skip to content

Commit 9eef604

Browse files
author
Kelven Yang
committed
bug 7685: a race condition caused DB connection from the pool to be left alone which can trigger mysql driver NPE exception
1 parent 49281a2 commit 9eef604

2 files changed

Lines changed: 45 additions & 49 deletions

File tree

‎utils/src/com/cloud/utils/db/DbUtil.java‎

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -49,39 +49,33 @@ public class DbUtil {
4949
private static Map<String, Connection> s_connectionForGlobalLocks = new HashMap<String, Connection>();
5050

5151
public static Connection getConnectionForGlobalLocks(String name, boolean forLock) {
52-
while(true) {
53-
synchronized(s_connectionForGlobalLocks) {
54-
if(forLock) {
55-
if(s_connectionForGlobalLocks.get(name) != null) {
56-
s_logger.error("Sanity check failed, global lock name " + name + " is in already in use");
57-
}
58-
59-
Connection connection = Transaction.getStandaloneConnection();
60-
if(connection != null) {
61-
try {
62-
connection.setAutoCommit(true);
63-
} catch (SQLException e) {
64-
try {
65-
connection.close();
66-
} catch(SQLException sqlException) {
67-
}
68-
return null;
52+
synchronized(s_connectionForGlobalLocks) {
53+
if(forLock) {
54+
if(s_connectionForGlobalLocks.get(name) != null) {
55+
s_logger.error("Sanity check failed, global lock name " + name + " is already in use");
56+
assert(false);
57+
}
58+
59+
Connection connection = Transaction.getStandaloneConnection();
60+
if(connection != null) {
61+
try {
62+
connection.setAutoCommit(true);
63+
} catch (SQLException e) {
64+
try {
65+
connection.close();
66+
} catch(SQLException sqlException) {
6967
}
70-
s_connectionForGlobalLocks.put(name, connection);
71-
return connection;
72-
}
73-
} else {
74-
Connection connection = s_connectionForGlobalLocks.get(name);
75-
s_connectionForGlobalLocks.remove(name);
76-
return connection;
77-
}
78-
}
79-
80-
s_logger.warn("Unable to acquire dabase connection for global lock " + name + ", waiting for someone to release and retrying...");
81-
try {
82-
Thread.sleep(1000);
83-
} catch (InterruptedException e) {
84-
}
68+
return null;
69+
}
70+
s_connectionForGlobalLocks.put(name, connection);
71+
return connection;
72+
}
73+
return null;
74+
} else {
75+
Connection connection = s_connectionForGlobalLocks.get(name);
76+
s_connectionForGlobalLocks.remove(name);
77+
return connection;
78+
}
8579
}
8680
}
8781

@@ -237,7 +231,7 @@ public static boolean getGlobalLock(String name, int timeoutSeconds) {
237231
if (pstmt != null) {
238232
try {
239233
pstmt.close();
240-
} catch (SQLException e) {
234+
} catch (Throwable e) {
241235
s_logger.error("What the heck? ", e);
242236
}
243237
}

‎utils/src/com/cloud/utils/db/GlobalLock.java‎

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public class GlobalLock {
4545
protected final static Logger s_logger = Logger.getLogger(GlobalLock.class);
4646

4747
private String name;
48-
private volatile int lockCount = 0;
48+
private int lockCount = 0;
4949
private Thread ownerThread = null;
50-
50+
5151
private int referenceCount = 0;
5252
private long holdingStartTick = 0;
5353

@@ -65,21 +65,21 @@ public int addRef() {
6565
}
6666

6767
public int releaseRef() {
68-
boolean releaseInternLock = false;
69-
int refCount;
70-
synchronized(this) {
71-
referenceCount--;
72-
refCount = referenceCount;
73-
74-
if(referenceCount < 0)
75-
s_logger.warn("Unmatched Global lock " + name + " reference usage detected, check your code!");
76-
77-
if(referenceCount == 0)
78-
releaseInternLock = true;
68+
int refCount;
69+
70+
synchronized(s_lockMap) { // // lock in sequence to prevent deadlock
71+
synchronized(this) {
72+
referenceCount--;
73+
refCount = referenceCount;
74+
75+
if(referenceCount < 0)
76+
s_logger.warn("Unmatched Global lock " + name + " reference usage detected, check your code!");
77+
78+
if(referenceCount == 0)
79+
releaseInternLock(name);
80+
}
7981
}
8082

81-
if(releaseInternLock)
82-
releaseInternLock(name);
8383
return refCount;
8484
}
8585

@@ -137,9 +137,9 @@ public boolean lock(int timeoutSeconds) {
137137
continue;
138138
} else {
139139
// we will discount the time that has been spent in previous waiting
140+
ownerThread = Thread.currentThread();
140141
if(DbUtil.getGlobalLock(name, remainingMilliSeconds / 1000)) {
141142
lockCount++;
142-
ownerThread = Thread.currentThread();
143143
holdingStartTick = System.currentTimeMillis();
144144

145145
// keep the lock in the intern map when we got the lock from database
@@ -148,6 +148,8 @@ public boolean lock(int timeoutSeconds) {
148148
if(s_logger.isTraceEnabled())
149149
s_logger.trace("lock " + name + " is acquired, lock count :" + lockCount);
150150
return true;
151+
} else {
152+
ownerThread = null;
151153
}
152154
return false;
153155
}

0 commit comments

Comments
 (0)