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

public abstract Class MultiHostConnectionProxy

extends Object
implements InvocationHandler
Class Inheritance
All Implemented Interfaces
java.lang.reflect.InvocationHandler
Known Direct Subclasses
com.mysql.cj.jdbc.ha.ReplicationConnectionProxy, com.mysql.cj.jdbc.ha.FailoverConnectionProxy, com.mysql.cj.jdbc.ha.LoadBalancedConnectionProxy
Imports
java.lang.reflect.InvocationHandler, .InvocationTargetException, .Method, .Proxy, java.sql.SQLException, java.util.ArrayList, .List, .Properties, java.util.concurrent.Executor, java.util.concurrent.locks.Lock, .ReentrantLock, com.mysql.cj.conf.ConnectionUrl, .HostInfo, .PropertyKey, .RuntimeProperty, com.mysql.cj.jdbc.CloseOption, .ConnectionImpl, .JdbcConnection, com.mysql.cj.util.Util

An abstract class that processes generic multi-host configurations. This class has to be sub-classed by specific multi-host implementations, such as load-balancing and failover.

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv class
MultiHostConnectionProxy.JdbcInterfaceProxy

Proxy class to intercept and deal with errors that may occur in any object bound to the current connection.

Field Summary

Modifier and TypeField and Description
pack-priv boolean
pack-priv boolean
pack-priv String
protected ConnectionUrl
pack-priv JdbcConnection
pack-priv List<HostInfo>
pack-priv boolean
protected Throwable
private final Lock
private static final String
private static final String
private static final String
private static final String
private static final String
private static final String
private static final String
private static final String
private static final String
private static final String
private static final String
private static final String
pack-priv JdbcConnection
pack-priv JdbcConnection
pack-priv JdbcConnection

Constructor Summary

AccessConstructor and Description
pack-priv
MultiHostConnectionProxy()

Initializes a connection wrapper for this MultiHostConnectionProxy instance.

pack-priv
MultiHostConnectionProxy(ConnectionUrl
The connection URL.
connectionUrl
)

Constructs a MultiHostConnectionProxy instance for the given connection URL.

Method Summary

Modifier and TypeMethod and Description
protected boolean

Returns:

true if the given method is allowed on closed connections
allowedOnClosedConnection
(Method
method
method
)

Checks if the given method is allowed on closed connections.

pack-priv ConnectionImpl

Returns:

The new Connection instance.
createConnectionForHost
(HostInfo
The host info instance.
hostInfo
)

Creates a new physical connection for the given HostInfo.

pack-priv void
dealWithInvocationException(InvocationTargetException
The Exception instance to check.
e
)

Deals with InvocationException from proxied objects.

pack-priv abstract void
doAbort(Executor
executor
executor
)

Executes a abort() invocation;

pack-priv abstract void
doAbortInternal()

Executes a abortInternal() invocation;

pack-priv abstract void
doClose()

Executes a close() invocation;

public Lock
pack-priv InvocationHandler

Returns:

The new InvocationHandler instance.
getNewJdbcInterfaceProxy
(Object
The object instance to be proxied.
toProxy
)

Instantiates a new JdbcInterfaceProxy for the given object.

pack-priv JdbcConnection

Returns:

The connection object instance that wraps 'this'.
getNewWrapperForThisAsConnection
()

Wraps this object with a new multi-host Connection instance.

protected JdbcConnection

Returns:

Returns this connection's proxy if there is one.
getParentProxy
()

Get this connection's parent proxy.

protected JdbcConnection

Returns:

Returns this connection's proxy if there is one or itself if this is the first one.
getProxy
()

Get this connection's proxy.

pack-priv int

Returns:

The number of hosts found in the hosts list.
initializeHostsSpecs
(ConnectionUrl
The connection URL that initialized this multi-host connection.
connUrl
,
List<HostInfo>
The list of hosts for this multi-host connection.
hosts
)

Initializes the hosts lists and makes a "clean" local copy of the given connection properties so that it can be later used to create standard connections.

pack-priv void
invalidateConnection(JdbcConnection
The connection instance to invalidate.
conn
)

Invalidates the specified connection by closing it.

pack-priv void
invalidateCurrentConnection()

Invalidates the current connection.

public Object

Returns:

method result
invoke
(Object
proxy object
proxy
,
Method
method to invoke
method
,
Object[]
method parameters
args
)

Implements java.lang.reflect.InvocationHandler.invoke.

Proxies method invocation on the java.sql.Connection interface, trapping multi-host specific methods and generic methods.

pack-priv abstract Object

Returns:

method result
invokeMore
(Object
proxy object
proxy
,
Method
method to invoke
method
,
Object[]
method parameters
args
)

Continuation of the method invocation process, to be implemented within each subclass.

pack-priv abstract boolean

Returns:

true if current connection is to a source host
isSourceConnection
()

Checks if current connection is to a source host.

pack-priv abstract void
pickNewConnection()

Picks the "best" connection to use from now on.

protected void
propagateProxyDown(JdbcConnection
The top level connection in the multi-host connections chain.
proxyConn
)

Propagates the connection proxy down through the multi-host connections chain.

pack-priv Object

Returns:

The proxied object or the original one if it does not implement a JDBC interface.
proxyIfReturnTypeIsJdbcInterface
(Class<?>
The type the object instance to proxy is supposed to be.
returnType
,
Object
The object instance to proxy.
toProxy
)

If the given return type is or implements a JDBC interface, proxies the given object so that we can catch SQL errors and fire a connection switch.

protected final void
setProxy(JdbcConnection
The top level connection in the multi-host connections chain.
proxyConn
)

Sets this connection's proxy.

pack-priv abstract boolean

Returns:

true if the given throwable should trigger a connection switch
shouldExceptionTriggerConnectionSwitch
(Throwable
The Throwable instance to analyze.
t
)

Checks if the given throwable should trigger a connection switch.

pack-priv void
syncSessionState(JdbcConnection
The connection where to get state from.
source
,
JdbcConnection
The connection where to set state.
target
)

Synchronizes session state between two connections.

pack-priv void
syncSessionState(JdbcConnection
The connection where to get state from.
source
,
JdbcConnection
The connection where to set state.
target
,
boolean
The new read-only status.
readOnly
)

Synchronizes session state between two connections, allowing to override the read-only status.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

autoReconnectback to summary
pack-priv boolean autoReconnect
closedExplicitlyback to summary
pack-priv boolean closedExplicitly
closedReasonback to summary
pack-priv String closedReason
connectionUrlback to summary
protected ConnectionUrl connectionUrl
currentConnectionback to summary
pack-priv JdbcConnection currentConnection
hostsListback to summary
pack-priv List<HostInfo> hostsList
isClosedback to summary
pack-priv boolean isClosed
lastExceptionDealtWithback to summary
protected Throwable lastExceptionDealtWith
lockback to summary
private final Lock lock
METHOD_ABORTback to summary
private static final String METHOD_ABORT
METHOD_ABORT_INTERNALback to summary
private static final String METHOD_ABORT_INTERNAL
METHOD_CLOSEback to summary
private static final String METHOD_CLOSE
METHOD_EQUALSback to summary
private static final String METHOD_EQUALS
METHOD_GET_AUTO_COMMITback to summary
private static final String METHOD_GET_AUTO_COMMIT
METHOD_GET_CATALOGback to summary
private static final String METHOD_GET_CATALOG
METHOD_GET_DATABASEback to summary
private static final String METHOD_GET_DATABASE
METHOD_GET_MULTI_HOST_SAFE_PROXYback to summary
private static final String METHOD_GET_MULTI_HOST_SAFE_PROXY
METHOD_GET_SCHEMAback to summary
private static final String METHOD_GET_SCHEMA
METHOD_GET_SESSION_MAX_ROWSback to summary
private static final String METHOD_GET_SESSION_MAX_ROWS
METHOD_GET_TRANSACTION_ISOLATIONback to summary
private static final String METHOD_GET_TRANSACTION_ISOLATION
METHOD_IS_CLOSEDback to summary
private static final String METHOD_IS_CLOSED
parentProxyConnectionback to summary
pack-priv JdbcConnection parentProxyConnection
thisAsConnectionback to summary
pack-priv JdbcConnection thisAsConnection
topProxyConnectionback to summary
pack-priv JdbcConnection topProxyConnection

Constructor Detail

MultiHostConnectionProxyback to summary
pack-priv MultiHostConnectionProxy() throws SQLException

Initializes a connection wrapper for this MultiHostConnectionProxy instance.

Exceptions
SQLException:
if an error occurs
MultiHostConnectionProxyback to summary
pack-priv MultiHostConnectionProxy(ConnectionUrl connectionUrl) throws SQLException

Constructs a MultiHostConnectionProxy instance for the given connection URL.

Parameters
connectionUrl:ConnectionUrl

The connection URL.

Exceptions
SQLException:
if an error occurs

Method Detail

allowedOnClosedConnectionback to summary
protected boolean allowedOnClosedConnection(Method method)

Checks if the given method is allowed on closed connections.

Parameters
method:Method

method

Returns:boolean

true if the given method is allowed on closed connections

createConnectionForHostback to summary
pack-priv ConnectionImpl createConnectionForHost(HostInfo hostInfo) throws SQLException

Creates a new physical connection for the given HostInfo.

Parameters
hostInfo:HostInfo

The host info instance.

Returns:ConnectionImpl

The new Connection instance.

Exceptions
SQLException:
if an error occurs
dealWithInvocationExceptionback to summary
pack-priv void dealWithInvocationException(InvocationTargetException e) throws SQLException, Throwable, InvocationTargetException

Deals with InvocationException from proxied objects.

Parameters
e:InvocationTargetException

The Exception instance to check.

Exceptions
SQLException:
if an error occurs
Throwable:
if an error occurs
InvocationTargetException:
if an error occurs
doAbortback to summary
pack-priv abstract void doAbort(Executor executor) throws SQLException

Executes a abort() invocation;

Parameters
executor:Executor

executor

Exceptions
SQLException:
if an error occurs
doAbortInternalback to summary
pack-priv abstract void doAbortInternal() throws SQLException

Executes a abortInternal() invocation;

Exceptions
SQLException:
if an error occurs
doCloseback to summary
pack-priv abstract void doClose() throws SQLException

Executes a close() invocation;

Exceptions
SQLException:
if an error occurs
getLockback to summary
public Lock getLock()
getNewJdbcInterfaceProxyback to summary
pack-priv InvocationHandler getNewJdbcInterfaceProxy(Object toProxy)

Instantiates a new JdbcInterfaceProxy for the given object. Subclasses can override this to return instances of JdbcInterfaceProxy subclasses.

Parameters
toProxy:Object

The object instance to be proxied.

Returns:InvocationHandler

The new InvocationHandler instance.

getNewWrapperForThisAsConnectionback to summary
pack-priv JdbcConnection getNewWrapperForThisAsConnection() throws SQLException

Wraps this object with a new multi-host Connection instance.

Returns:JdbcConnection

The connection object instance that wraps 'this'.

Exceptions
SQLException:
if an error occurs
getParentProxyback to summary
protected JdbcConnection getParentProxy()

Get this connection's parent proxy.

Returns:JdbcConnection

Returns this connection's proxy if there is one.

getProxyback to summary
protected JdbcConnection getProxy()

Get this connection's proxy. A multi-host connection may not be at top level in the multi-host connections chain. In such case the first connection in the chain is available as a proxy.

Returns:JdbcConnection

Returns this connection's proxy if there is one or itself if this is the first one.

initializeHostsSpecsback to summary
pack-priv int initializeHostsSpecs(ConnectionUrl connUrl, List<HostInfo> hosts)

Initializes the hosts lists and makes a "clean" local copy of the given connection properties so that it can be later used to create standard connections.

Parameters
connUrl:ConnectionUrl

The connection URL that initialized this multi-host connection.

hosts:List<HostInfo>

The list of hosts for this multi-host connection.

Returns:int

The number of hosts found in the hosts list.

invalidateConnectionback to summary
pack-priv void invalidateConnection(JdbcConnection conn) throws SQLException

Invalidates the specified connection by closing it.

Parameters
conn:JdbcConnection

The connection instance to invalidate.

Exceptions
SQLException:
if an error occurs
invalidateCurrentConnectionback to summary
pack-priv void invalidateCurrentConnection() throws SQLException

Invalidates the current connection.

Exceptions
SQLException:
if an error occurs
invokeback to summary
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable

Implements java.lang.reflect.InvocationHandler.invoke.

Proxies method invocation on the java.sql.Connection interface, trapping multi-host specific methods and generic methods. Subclasses have to override this to complete the method invocation process, deal with exceptions and decide when to switch connection. To avoid unnecessary additional exception handling overriders should consult #canDealWith(Method) before chaining here.

Parameters
proxy:Object

proxy object

method:Method

method to invoke

args:Object[]

method parameters

Returns:Object

method result

Annotations
@Override
Exceptions
Throwable:
if an error occurs
invokeMoreback to summary
pack-priv abstract Object invokeMore(Object proxy, Method method, Object[] args) throws Throwable

Continuation of the method invocation process, to be implemented within each subclass.

Parameters
proxy:Object

proxy object

method:Method

method to invoke

args:Object[]

method parameters

Returns:Object

method result

Exceptions
Throwable:
if an error occurs
isSourceConnectionback to summary
pack-priv abstract boolean isSourceConnection()

Checks if current connection is to a source host.

Returns:boolean

true if current connection is to a source host

pickNewConnectionback to summary
pack-priv abstract void pickNewConnection() throws SQLException

Picks the "best" connection to use from now on. Each subclass needs to implement its connection switch strategy on it.

Exceptions
SQLException:
if an error occurs
propagateProxyDownback to summary
protected void propagateProxyDown(JdbcConnection proxyConn)

Propagates the connection proxy down through the multi-host connections chain. This method is intended to be overridden in subclasses that manage more than one active connection at same time.

Parameters
proxyConn:JdbcConnection

The top level connection in the multi-host connections chain.

proxyIfReturnTypeIsJdbcInterfaceback to summary
pack-priv Object proxyIfReturnTypeIsJdbcInterface(Class<?> returnType, Object toProxy)

If the given return type is or implements a JDBC interface, proxies the given object so that we can catch SQL errors and fire a connection switch.

Parameters
returnType:Class<?>

The type the object instance to proxy is supposed to be.

toProxy:Object

The object instance to proxy.

Returns:Object

The proxied object or the original one if it does not implement a JDBC interface.

setProxyback to summary
protected final void setProxy(JdbcConnection proxyConn)

Sets this connection's proxy. This proxy should be the first connection in the multi-host connections chain. After setting the connection proxy locally, propagates it through the dependent connections.

Parameters
proxyConn:JdbcConnection

The top level connection in the multi-host connections chain.

shouldExceptionTriggerConnectionSwitchback to summary
pack-priv abstract boolean shouldExceptionTriggerConnectionSwitch(Throwable t)

Checks if the given throwable should trigger a connection switch.

Parameters
t:Throwable

The Throwable instance to analyze.

Returns:boolean

true if the given throwable should trigger a connection switch

syncSessionStateback to summary
pack-priv void syncSessionState(JdbcConnection source, JdbcConnection target) throws SQLException

Synchronizes session state between two connections.

Parameters
source:JdbcConnection

The connection where to get state from.

target:JdbcConnection

The connection where to set state.

Exceptions
SQLException:
if an error occurs
syncSessionStateback to summary
pack-priv void syncSessionState(JdbcConnection source, JdbcConnection target, boolean readOnly) throws SQLException

Synchronizes session state between two connections, allowing to override the read-only status.

Parameters
source:JdbcConnection

The connection where to get state from.

target:JdbcConnection

The connection where to set state.

readOnly:boolean

The new read-only status.

Exceptions
SQLException:
if an error occurs
com.mysql.cj.jdbc.ha back to summary

pack-priv Class MultiHostConnectionProxy.JdbcInterfaceProxy

extends Object
implements InvocationHandler
Class Inheritance
  • java.lang.Object
  • com.mysql.cj.jdbc.ha.MultiHostConnectionProxy.JdbcInterfaceProxy
All Implemented Interfaces
java.lang.reflect.InvocationHandler
Known Direct Subclasses
com.mysql.cj.jdbc.ha.FailoverConnectionProxy.FailoverJdbcInterfaceProxy

Proxy class to intercept and deal with errors that may occur in any object bound to the current connection.

Field Summary

Modifier and TypeField and Description
pack-priv Object

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public Object
invoke(Object
the proxy instance that the method was invoked on
proxy
,
Method
the Method instance corresponding to the interface method invoked on the proxy instance. The declaring class of the Method object will be the interface that the method was declared in, which may be a superinterface of the proxy interface that the proxy class inherits the method through.
method
,
Object[]
an array of objects containing the values of the arguments passed in the method invocation on the proxy instance, or null if interface method takes no arguments. Arguments of primitive types are wrapped in instances of the appropriate primitive wrapper class, such as java.lang.Integer or java.lang.Boolean.
args
)

Implements java.lang.reflect.InvocationHandler.invoke.

Processes a method invocation on a proxy instance and returns the result.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

invokeOnback to summary
pack-priv Object invokeOn

Constructor Detail

JdbcInterfaceProxyback to summary
pack-priv JdbcInterfaceProxy(Object toInvokeOn)

Method Detail

invokeback to summary
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable

Implements java.lang.reflect.InvocationHandler.invoke.

Doc from java.lang.reflect.InvocationHandler.invoke.

Processes a method invocation on a proxy instance and returns the result. This method will be invoked on an invocation handler when a method is invoked on a proxy instance that it is associated with.

Parameters
proxy:Object

the proxy instance that the method was invoked on

method:Method

the Method instance corresponding to the interface method invoked on the proxy instance. The declaring class of the Method object will be the interface that the method was declared in, which may be a superinterface of the proxy interface that the proxy class inherits the method through.

args:Object[]

an array of objects containing the values of the arguments passed in the method invocation on the proxy instance, or null if interface method takes no arguments. Arguments of primitive types are wrapped in instances of the appropriate primitive wrapper class, such as java.lang.Integer or java.lang.Boolean.

Returns:Object

the value to return from the method invocation on the proxy instance. If the declared return type of the interface method is a primitive type, then the value returned by this method must be an instance of the corresponding primitive wrapper class; otherwise, it must be a type assignable to the declared return type. If the value returned by this method is null and the interface method's return type is primitive, then a NullPointerException will be thrown by the method invocation on the proxy instance. If the value returned by this method is otherwise not compatible with the interface method's declared return type as described above, a ClassCastException will be thrown by the method invocation on the proxy instance.

Annotations
@Override
Exceptions
Throwable:
the exception to throw from the method invocation on the proxy instance. The exception's type must be assignable either to any of the exception types declared in the throws clause of the interface method or to the unchecked exception types java.lang.RuntimeException or java.lang.Error. If a checked exception is thrown by this method that is not assignable to any of the exception types declared in the throws clause of the interface method, then an UndeclaredThrowableException containing the exception that was thrown by this method will be thrown by the method invocation on the proxy instance.