Skip to content

Commit 6ca73ba

Browse files
committed
[mysql] support "disabling" abandoned connection cleanup thread
due leaks on hot re-deploys - will probably be shutdown by default on the next major version or AR-JDBC
1 parent 6ca59c1 commit 6ca73ba

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/java/arjdbc/mysql/MySQLRubyJdbcConnection.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import arjdbc.jdbc.Callable;
3030

3131
import java.lang.reflect.Field;
32+
import java.lang.reflect.InvocationTargetException;
3233
import java.lang.reflect.Proxy;
3334
import java.sql.Connection;
3435
import java.sql.PreparedStatement;
@@ -234,10 +235,48 @@ protected String caseConvertIdentifierForRails(final Connection connection, fina
234235
@Override
235236
protected Connection newConnection() throws RaiseException, SQLException {
236237
final Connection connection = super.newConnection();
238+
if ( doStopCleanupThread() ) shutdownCleanupThread();
237239
if ( doKillCancelTimer(connection) ) killCancelTimer(connection);
238240
return connection;
239241
}
240242

243+
private static Boolean stopCleanupThread;
244+
static {
245+
final String stopThread = System.getProperty("arjdbc.mysql.stop_cleanup_thread");
246+
if ( stopThread != null ) stopCleanupThread = Boolean.parseBoolean(stopThread);
247+
}
248+
249+
private static boolean doStopCleanupThread() throws SQLException {
250+
// TODO when refactoring default behavior to "stop" consider not doing so for JNDI
251+
return stopCleanupThread != null && stopCleanupThread.booleanValue();
252+
}
253+
254+
private static boolean cleanupThreadShutdown;
255+
256+
private static void shutdownCleanupThread() {
257+
if ( cleanupThreadShutdown ) return;
258+
try {
259+
Class threadClass = Class.forName("com.mysql.jdbc.AbandonedConnectionCleanupThread");
260+
threadClass.getMethod("shutdown").invoke(null);
261+
}
262+
catch (ClassNotFoundException e) {
263+
debugMessage("INFO: missing MySQL JDBC cleanup thread: " + e);
264+
}
265+
catch (NoSuchMethodException e) {
266+
debugMessage( e.toString() );
267+
}
268+
catch (IllegalAccessException e) {
269+
debugMessage( e.toString() );
270+
}
271+
catch (InvocationTargetException e) {
272+
debugMessage( e.getTargetException().toString() );
273+
}
274+
catch (SecurityException e) {
275+
debugMessage( e.toString() );
276+
}
277+
finally { cleanupThreadShutdown = true; }
278+
}
279+
241280
private static Boolean killCancelTimer;
242281
static {
243282
final String killTimer = System.getProperty("arjdbc.mysql.kill_cancel_timer");

0 commit comments

Comments
 (0)