Top Description Inners Fields Constructors Methods
com.mysql.cj.jdbc

public Class AbandonedConnectionCleanupThread

extends Object
implements Runnable
Class Inheritance
All Implemented Interfaces
java.lang.Runnable
Imports
java.lang.ref.PhantomReference, .Reference, .ReferenceQueue, java.util.Set, java.util.concurrent.ConcurrentHashMap, .ExecutorService, .Executors, java.util.concurrent.locks.Lock, .ReentrantLock, com.mysql.cj.MysqlConnection, com.mysql.cj.conf.PropertyDefinitions, 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.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private static boolean
private static final ExecutorService
private static final Set<AbandonedConnectionCleanupThread.ConnectionFinalizerPhantomReference>
private static final ReferenceQueue<MysqlConnection>
private static Thread
private static Lock

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static void
checkedShutdown()

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.

private void
checkThreadContextClassLoader()

Checks if the thread's context ClassLoader is active.

private static boolean

Returns:

true if both threads share the same context ClassLoader, false otherwise
consistentClassLoaders
()

Checks if the context ClassLoaders from this and the caller thread are the same.

private static void
finalizeResource(AbandonedConnectionCleanupThread.ConnectionFinalizerPhantomReference reference)

Release resources from the given ConnectionFinalizerPhantomReference and remove it from the references set.

public static boolean

Returns:

true if the working thread is alive; false otherwise.
isAlive
()

Returns true if the working thread is alive.

public void
run()

Implements java.lang.Runnable.run.

Runs this operation.

private static void
shutdown(boolean
does a checked shutdown if true, unchecked otherwise
checked
)

Shuts down this thread either checking or not the context ClassLoaders from the involved threads.

protected static void
trackConnection(MysqlConnection
the Connection object to track for finalization
conn
,
NetworkResources
the network resources to close on the connection finalization
io
)

Tracks the finalization of a MysqlConnection object and keeps a reference to its NetworkResources so that they can be later released.

public static void
uncheckedShutdown()

Performs an unchecked shutdown, i.e., the shutdown is performed independently of the context ClassLoaders from the involved threads.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

abandonedConnectionCleanupDisabledback to summary
private static boolean abandonedConnectionCleanupDisabled
cleanupThreadExecutorServiceback to summary
private static final ExecutorService cleanupThreadExecutorService
connectionFinalizerPhantomRefsback to summary
private static final Set<AbandonedConnectionCleanupThread.ConnectionFinalizerPhantomReference> connectionFinalizerPhantomRefs
referenceQueueback to summary
private static final ReferenceQueue<MysqlConnection> referenceQueue
threadRefback to summary
private static Thread threadRef
threadRefLockback to summary
private static Lock threadRefLock

Constructor Detail

AbandonedConnectionCleanupThreadback to summary
private AbandonedConnectionCleanupThread()

Method Detail

checkedShutdownback to summary
public static void checkedShutdown()

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.

checkThreadContextClassLoaderback to summary
private void checkThreadContextClassLoader()

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.

consistentClassLoadersback to summary
private static boolean consistentClassLoaders()

Checks if the context ClassLoaders from this and the caller thread are the same.

Returns:boolean

true if both threads share the same context ClassLoader, false otherwise

finalizeResourceback to summary
private static void finalizeResource(AbandonedConnectionCleanupThread.ConnectionFinalizerPhantomReference reference)

Release resources from the given ConnectionFinalizerPhantomReference and remove it from the references set.

Parameters
reference:AbandonedConnectionCleanupThread.ConnectionFinalizerPhantomReference

the ConnectionFinalizerPhantomReference to finalize.

isAliveback to summary
public static boolean isAlive()

Returns true if the working thread is alive. It is alive if it was initialized successfully and wasn't shutdown yet.

Returns:boolean

true if the working thread is alive; false otherwise.

runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

Annotations
@Override
shutdownback to summary
private static void shutdown(boolean checked)

Shuts down this thread either checking or not the context ClassLoaders from the involved threads.

Parameters
checked:boolean

does a checked shutdown if true, unchecked otherwise

trackConnectionback to summary
protected static void trackConnection(MysqlConnection conn, NetworkResources io)

Tracks the finalization of a MysqlConnection object and keeps a reference to its NetworkResources so that they can be later released.

Parameters
conn:MysqlConnection

the Connection object to track for finalization

io:NetworkResources

the network resources to close on the connection finalization

uncheckedShutdownback to summary
public static void uncheckedShutdown()

Performs an unchecked shutdown, i.e., the shutdown is performed independently of the context ClassLoaders from the involved threads.

com.mysql.cj.jdbc back to summary

private Class AbandonedConnectionCleanupThread.ConnectionFinalizerPhantomReference

extends PhantomReference<MysqlConnection>
Class Inheritance

PhantomReference subclass to track MysqlConnection objects finalization. This class holds a reference to the Connection's NetworkResources so they it can be later closed.

Field Summary

Modifier and TypeField and Description
private NetworkResources

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv void
Inherited from java.lang.ref.PhantomReference:
get

Field Detail

networkResourcesback to summary
private NetworkResources networkResources

Constructor Detail

ConnectionFinalizerPhantomReferenceback to summary
pack-priv ConnectionFinalizerPhantomReference(MysqlConnection conn, NetworkResources networkResources, ReferenceQueue<? super MysqlConnection> refQueue)

Method Detail

finalizeResourcesback to summary
pack-priv void finalizeResources()