-
Notifications
You must be signed in to change notification settings - Fork 569
/
AbandonedConnectionCleanupThread.java
255 lines (233 loc) · 10.7 KB
/
AbandonedConnectionCleanupThread.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
* Copyright (c) 2013, 2020, Oracle and/or its affiliates.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 2.0, as published by the
* Free Software Foundation.
*
* This program is also distributed with certain software (including but not
* limited to OpenSSL) that is licensed under separate terms, as designated in a
* particular file or component or in included license documentation. The
* authors of MySQL hereby grant you an additional permission to link the
* program and your derivative works with the separately licensed software that
* they have included with MySQL.
*
* Without limiting anything contained in the foregoing, this file, which is
* part of MySQL Connector/J, is also subject to the Universal FOSS Exception,
* version 1.0, a copy of which can be found at
* http://oss.oracle.com/licenses/universal-foss-exception.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.mysql.cj.jdbc;
import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import com.mysql.cj.MysqlConnection;
import com.mysql.cj.conf.PropertyDefinitions;
import com.mysql.cj.protocol.NetworkResources;
/**
* This class implements a thread that is responsible for closing abandoned MySQL connections, i.e., connections that are not explicitly closed.
* There is only one instance of this class and there is a single thread to do this task. This thread's executor is statically referenced in this same class.
*/
public class AbandonedConnectionCleanupThread implements Runnable {
private static final Set<ConnectionFinalizerPhantomReference> connectionFinalizerPhantomRefs = ConcurrentHashMap.newKeySet();
private static final ReferenceQueue<MysqlConnection> referenceQueue = new ReferenceQueue<>();
private static final ExecutorService cleanupThreadExecutorService;
private static Thread threadRef = null;
private static Lock threadRefLock = new ReentrantLock();
private static boolean abandonedConnectionCleanupDisabled = Boolean.getBoolean(PropertyDefinitions.SYSP_disableAbandonedConnectionCleanup);
static {
if (abandonedConnectionCleanupDisabled) {
cleanupThreadExecutorService = null;
} else {
cleanupThreadExecutorService = Executors.newSingleThreadExecutor(r -> {
Thread t = new Thread(r, "mysql-cj-abandoned-connection-cleanup");
t.setDaemon(true);
// Tie the thread's context ClassLoader to the ClassLoader that loaded the class instead of inheriting the context ClassLoader from the current
// thread, which would happen by default.
// Application servers may use this information if they attempt to shutdown this thread. By leaving the default context ClassLoader this thread
// could end up being shut down even when it is shared by other applications and, being it statically initialized, thus, never restarted again.
ClassLoader classLoader = AbandonedConnectionCleanupThread.class.getClassLoader();
if (classLoader == null) {
// This class was loaded by the Bootstrap ClassLoader, so let's tie the thread's context ClassLoader to the System ClassLoader instead.
classLoader = ClassLoader.getSystemClassLoader();
}
t.setContextClassLoader(classLoader);
return threadRef = t;
});
cleanupThreadExecutorService.execute(new AbandonedConnectionCleanupThread());
}
}
private AbandonedConnectionCleanupThread() {
}
public void run() {
for (;;) {
try {
checkThreadContextClassLoader();
Reference<? extends MysqlConnection> reference = referenceQueue.remove(5000);
if (reference != null) {
finalizeResource((ConnectionFinalizerPhantomReference) reference);
}
} catch (InterruptedException e) {
threadRefLock.lock();
try {
threadRef = null;
// Finalize remaining references.
Reference<? extends MysqlConnection> reference;
while ((reference = referenceQueue.poll()) != null) {
finalizeResource((ConnectionFinalizerPhantomReference) reference);
}
connectionFinalizerPhantomRefs.clear();
} finally {
threadRefLock.unlock();
}
return;
} catch (Exception ex) {
// Nowhere to really log this.
}
}
}
/**
* Checks if the thread's context ClassLoader is active. This is usually true but some application managers implement a life-cycle mechanism in their
* ClassLoaders that is linked to the corresponding application's life-cycle. As such, a stopped/ended application will have a ClassLoader unable to load
* anything and, eventually, they throw an exception when trying to do so. When this happens, this thread has no point in being alive anymore.
*/
private void checkThreadContextClassLoader() {
try {
threadRef.getContextClassLoader().getResource("");
} catch (Throwable e) {
// Shutdown no matter what.
uncheckedShutdown();
}
}
/**
* Checks if the context ClassLoaders from this and the caller thread are the same.
*
* @return true if both threads share the same context ClassLoader, false otherwise
*/
private static boolean consistentClassLoaders() {
threadRefLock.lock();
try {
if (threadRef == null) {
return false;
}
ClassLoader callerCtxClassLoader = Thread.currentThread().getContextClassLoader();
ClassLoader threadCtxClassLoader = threadRef.getContextClassLoader();
return callerCtxClassLoader != null && threadCtxClassLoader != null && callerCtxClassLoader == threadCtxClassLoader;
} finally {
threadRefLock.unlock();
}
}
/**
* Shuts down this thread either checking or not the context ClassLoaders from the involved threads.
*
* @param checked
* does a checked shutdown if true, unchecked otherwise
*/
private static void shutdown(boolean checked) {
if (checked && !consistentClassLoaders()) {
// This thread can't be shutdown from the current thread's context ClassLoader. Doing so would most probably prevent from restarting this thread
// later on. An unchecked shutdown can still be done if needed by calling shutdown(false).
return;
}
if (cleanupThreadExecutorService != null) {
cleanupThreadExecutorService.shutdownNow();
}
}
/**
* Performs a checked shutdown, i.e., the context ClassLoaders from this and the caller thread are checked for consistency prior to performing the shutdown
* operation.
*/
public static void checkedShutdown() {
shutdown(true);
}
/**
* Performs an unchecked shutdown, i.e., the shutdown is performed independently of the context ClassLoaders from the involved threads.
*/
public static void uncheckedShutdown() {
shutdown(false);
}
/**
* Returns true if the working thread is alive. It is alive if it was initialized successfully and wasn't shutdown yet.
*
* @return true if the working thread is alive; false otherwise.
*/
public static boolean isAlive() {
threadRefLock.lock();
try {
return threadRef != null && threadRef.isAlive();
} finally {
threadRefLock.unlock();
}
}
/**
* Tracks the finalization of a {@link MysqlConnection} object and keeps a reference to its {@link NetworkResources} so that they can be later released.
*
* @param conn
* the Connection object to track for finalization
* @param io
* the network resources to close on the connection finalization
*/
protected static void trackConnection(MysqlConnection conn, NetworkResources io) {
if (abandonedConnectionCleanupDisabled) {
return;
}
threadRefLock.lock();
try {
if (isAlive()) {
ConnectionFinalizerPhantomReference reference = new ConnectionFinalizerPhantomReference(conn, io, referenceQueue);
connectionFinalizerPhantomRefs.add(reference);
}
} finally {
threadRefLock.unlock();
}
}
/**
* Release resources from the given {@link ConnectionFinalizerPhantomReference} and remove it from the references set.
*
* @param reference
* the {@link ConnectionFinalizerPhantomReference} to finalize.
*/
private static void finalizeResource(ConnectionFinalizerPhantomReference reference) {
try {
reference.finalizeResources();
reference.clear();
} finally {
connectionFinalizerPhantomRefs.remove(reference);
}
}
/**
* {@link PhantomReference} subclass to track {@link MysqlConnection} objects finalization.
* This class holds a reference to the Connection's {@link NetworkResources} so they it can be later closed.
*/
private static class ConnectionFinalizerPhantomReference extends PhantomReference<MysqlConnection> {
private NetworkResources networkResources;
ConnectionFinalizerPhantomReference(MysqlConnection conn, NetworkResources networkResources, ReferenceQueue<? super MysqlConnection> refQueue) {
super(conn, refQueue);
this.networkResources = networkResources;
}
void finalizeResources() {
if (this.networkResources != null) {
try {
this.networkResources.forceClose();
} finally {
this.networkResources = null;
}
}
}
}
}