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

public Class FailoverConnectionProxy

extends MultiHostConnectionProxy
Class Inheritance
Imports
java.lang.reflect.InvocationTargetException, .Method, java.sql.SQLException, java.util.concurrent.Executor, com.mysql.cj.conf.ConnectionUrl, .PropertyKey, com.mysql.cj.exceptions.CJCommunicationsException, .CJException, .MysqlErrorNumbers, com.mysql.cj.jdbc.ConnectionImpl, .JdbcConnection, .JdbcPropertySetImpl, com.mysql.cj.jdbc.exceptions.CommunicationsException, .SQLError, .SQLExceptionsMapping, com.mysql.cj.util.Util

A proxy for a dynamic com.mysql.cj.jdbc.JdbcConnection implementation that provides failover features for list of hosts. Connection switching occurs on communications related exceptions and/or user defined settings, namely when one of the conditions set in 'secondsBeforeRetrySource' or 'queriesBeforeRetrySource' is met.

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv class
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
private int
private static final int
private boolean
private boolean
private Boolean
private boolean
private static final String
private static final String
private static final String
private static final String
private static final int
private long
private int
private long
private long
private int
private int
Inherited from com.mysql.cj.jdbc.ha.MultiHostConnectionProxy:
autoReconnectclosedExplicitlyclosedReasonconnectionUrlcurrentConnectionhostsListisClosedlastExceptionDealtWithparentProxyConnectionthisAsConnectiontopProxyConnection

Constructor Summary

AccessConstructor and Description
private
FailoverConnectionProxy(ConnectionUrl
ConnectionUrl instance containing the lists of hosts available to switch on.
connectionUrl
)

Instantiates a new FailoverConnectionProxy for the given list of hosts and connection properties.

Method Summary

Modifier and TypeMethod and Description
pack-priv boolean

Returns:

true if so
connectedToPrimaryHost
()

Checks if this proxy is using the primary host in the underlying connection.

pack-priv boolean

Returns:

true if so
connectedToSecondaryHost
()

Checks if this proxy is using a secondary host in the underlying connection.

private void
connectTo(int
The host index in the global hosts list.
hostIndex
)

Connects this dynamic failover connection proxy to the host pointed out by the given host index.

pack-priv ConnectionImpl

Returns:

The new connection instance.
createConnectionForHostIndex
(int
The host index in the global hosts list.
hostIndex
)

Creates a new connection instance for host pointed out by the given host index.

public static JdbcConnection
pack-priv void
doAbort(Executor
executor
executor
)

Implements abstract com.mysql.cj.jdbc.ha.MultiHostConnectionProxy.doAbort.

Aborts current connection using the given executor.

pack-priv void
doAbortInternal()

Implements abstract com.mysql.cj.jdbc.ha.MultiHostConnectionProxy.doAbortInternal.

Aborts current connection.

pack-priv void
doClose()

Implements abstract com.mysql.cj.jdbc.ha.MultiHostConnectionProxy.doClose.

Closes current connection.

private void
failOver()

Initiates a default failover procedure starting at the current connection host index.

private void
failOver(int
The host index where to start from. First connection attempt will be the next one.
failedHostIdx
)

Initiates a default failover procedure starting at the given host index.

pack-priv void
fallBackToPrimaryIfAvailable()

Falls back to primary host or keep current connection if primary not available.

pack-priv MultiHostConnectionProxy.JdbcInterfaceProxy
getNewJdbcInterfaceProxy(Object
The object instance to be proxied.
toProxy
)

Overrides com.mysql.cj.jdbc.ha.MultiHostConnectionProxy.getNewJdbcInterfaceProxy.

Gets locally bound instances of FailoverJdbcInterfaceProxy.

pack-priv void
incrementQueriesIssuedSinceFailover()

Increments counter for query executions.

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

Implements abstract com.mysql.cj.jdbc.ha.MultiHostConnectionProxy.invokeMore.

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

pack-priv boolean

Returns:

true if there is a connection
isConnected
()

Checks if there is a underlying connection for this proxy.

pack-priv boolean

Returns:

true if so
isPrimaryHostIndex
(int
The host index in the global hosts list.
hostIndex
)

Checks if the given host index points to the primary host.

pack-priv boolean
isSourceConnection()

Implements abstract com.mysql.cj.jdbc.ha.MultiHostConnectionProxy.isSourceConnection.

Checks if current connection is to a source host.

private int

Returns:

next host index
nextHost
(int
Current host index.
currHostIdx
,
boolean
Allows to return the primary host index even if the usual required conditions aren't met.
vouchForPrimaryHost
)

Gets the next host on the hosts list.

pack-priv void
pickNewConnection()

Implements abstract com.mysql.cj.jdbc.ha.MultiHostConnectionProxy.pickNewConnection.

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

private boolean

Returns:

value
queriesBeforeRetryPrimaryHostIsMet
()

Checks the condition set by the property 'queriesBeforeRetrySource'.

pack-priv boolean

Returns:

true if ready
readyToFallBackToPrimaryHost
()

Checks if at least one of the required conditions to fall back to primary host is met, which is determined by the properties 'queriesBeforeRetrySource' and 'secondsBeforeRetrySource'.

private void
resetAutoFallBackCounters()

Resets auto-fall back counters.

private boolean

Returns:

value
secondsBeforeRetryPrimaryHostIsMet
()

Checks the condition set by the property 'secondsBeforeRetrySource'.

pack-priv boolean
shouldExceptionTriggerConnectionSwitch(Throwable
The Throwable instance to analyze.
t
)

Implements abstract com.mysql.cj.jdbc.ha.MultiHostConnectionProxy.shouldExceptionTriggerConnectionSwitch.

Checks if the given throwable should trigger a connection switch.

private void
switchCurrentConnectionTo(int
The host index in the global hosts list that matches the given connection.
hostIndex
,
JdbcConnection
The connection instance to switch to.
connection
)

Replaces the previous underlying connection by the connection given.

Inherited from com.mysql.cj.jdbc.ha.MultiHostConnectionProxy:
allowedOnClosedConnectioncreateConnectionForHostdealWithInvocationExceptiongetLockgetNewWrapperForThisAsConnectiongetParentProxygetProxyinitializeHostsSpecsinvalidateConnectioninvalidateCurrentConnectioninvokepropagateProxyDownproxyIfReturnTypeIsJdbcInterfacesetProxysyncSessionStatesyncSessionState

Field Detail

currentHostIndexback to summary
private int currentHostIndex
DEFAULT_PRIMARY_HOST_INDEXback to summary
private static final int DEFAULT_PRIMARY_HOST_INDEX
enableFallBackToPrimaryHostback to summary
private boolean enableFallBackToPrimaryHost
explicitlyAutoCommitback to summary
private boolean explicitlyAutoCommit
explicitlyReadOnlyback to summary
private Boolean explicitlyReadOnly
failoverReadOnlyback to summary
private boolean failoverReadOnly
METHOD_COMMITback to summary
private static final String METHOD_COMMIT
METHOD_ROLLBACKback to summary
private static final String METHOD_ROLLBACK
METHOD_SET_AUTO_COMMITback to summary
private static final String METHOD_SET_AUTO_COMMIT
METHOD_SET_READ_ONLYback to summary
private static final String METHOD_SET_READ_ONLY
NO_CONNECTION_INDEXback to summary
private static final int NO_CONNECTION_INDEX
primaryHostFailTimeMillisback to summary
private long primaryHostFailTimeMillis
primaryHostIndexback to summary
private int primaryHostIndex
queriesBeforeRetryPrimaryHostback to summary
private long queriesBeforeRetryPrimaryHost
queriesIssuedSinceFailoverback to summary
private long queriesIssuedSinceFailover
retriesAllDownback to summary
private int retriesAllDown
secondsBeforeRetryPrimaryHostback to summary
private int secondsBeforeRetryPrimaryHost

Constructor Detail

FailoverConnectionProxyback to summary
private FailoverConnectionProxy(ConnectionUrl connectionUrl) throws SQLException

Instantiates a new FailoverConnectionProxy for the given list of hosts and connection properties.

Parameters
connectionUrl:ConnectionUrl

ConnectionUrl instance containing the lists of hosts available to switch on.

Exceptions
SQLException:
if an error occurs

Method Detail

connectedToPrimaryHostback to summary
pack-priv boolean connectedToPrimaryHost()

Checks if this proxy is using the primary host in the underlying connection.

Returns:boolean

true if so

connectedToSecondaryHostback to summary
pack-priv boolean connectedToSecondaryHost()

Checks if this proxy is using a secondary host in the underlying connection.

Returns:boolean

true if so

connectToback to summary
private void connectTo(int hostIndex) throws SQLException

Connects this dynamic failover connection proxy to the host pointed out by the given host index.

Parameters
hostIndex:int

The host index in the global hosts list.

Exceptions
SQLException:
if an error occurs
createConnectionForHostIndexback to summary
pack-priv ConnectionImpl createConnectionForHostIndex(int hostIndex) throws SQLException

Creates a new connection instance for host pointed out by the given host index.

Parameters
hostIndex:int

The host index in the global hosts list.

Returns:ConnectionImpl

The new connection instance.

Exceptions
SQLException:
if an error occurs
createProxyInstanceback to summary
public static JdbcConnection createProxyInstance(ConnectionUrl connectionUrl) throws SQLException
doAbortback to summary
pack-priv void doAbort(Executor executor) throws SQLException

Implements abstract com.mysql.cj.jdbc.ha.MultiHostConnectionProxy.doAbort.

Aborts current connection using the given executor.

Parameters
executor:Executor

Doc from com.mysql.cj.jdbc.ha.MultiHostConnectionProxy.doAbort.

executor

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

Implements abstract com.mysql.cj.jdbc.ha.MultiHostConnectionProxy.doAbortInternal.

Aborts current connection.

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

Implements abstract com.mysql.cj.jdbc.ha.MultiHostConnectionProxy.doClose.

Closes current connection.

Annotations
@Override
Exceptions
SQLException:
if an error occurs
failOverback to summary
private void failOver() throws SQLException

Initiates a default failover procedure starting at the current connection host index.

Exceptions
SQLException:
if an error occurs
failOverback to summary
private void failOver(int failedHostIdx) throws SQLException

Initiates a default failover procedure starting at the given host index. This process tries to connect, sequentially, to the next host in the list. The primary host may or may not be excluded from the connection attempts.

Parameters
failedHostIdx:int

The host index where to start from. First connection attempt will be the next one.

Exceptions
SQLException:
if an error occurs
fallBackToPrimaryIfAvailableback to summary
pack-priv void fallBackToPrimaryIfAvailable()

Falls back to primary host or keep current connection if primary not available.

getNewJdbcInterfaceProxyback to summary
pack-priv MultiHostConnectionProxy.JdbcInterfaceProxy getNewJdbcInterfaceProxy(Object toProxy)

Overrides com.mysql.cj.jdbc.ha.MultiHostConnectionProxy.getNewJdbcInterfaceProxy.

Gets locally bound instances of FailoverJdbcInterfaceProxy.

Parameters
toProxy:Object

Doc from com.mysql.cj.jdbc.ha.MultiHostConnectionProxy.getNewJdbcInterfaceProxy.

The object instance to be proxied.

Returns:MultiHostConnectionProxy.JdbcInterfaceProxy

Doc from com.mysql.cj.jdbc.ha.MultiHostConnectionProxy.getNewJdbcInterfaceProxy.

The new InvocationHandler instance.

Annotations
@Override
incrementQueriesIssuedSinceFailoverback to summary
pack-priv void incrementQueriesIssuedSinceFailover()

Increments counter for query executions.

invokeMoreback to summary
public Object invokeMore(Object proxy, Method method, Object[] args) throws Throwable

Implements abstract com.mysql.cj.jdbc.ha.MultiHostConnectionProxy.invokeMore.

Doc from com.mysql.cj.jdbc.ha.MultiHostConnectionProxy.invokeMore.

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

Annotations
@Override
Exceptions
Throwable:
if an error occurs
isConnectedback to summary
pack-priv boolean isConnected()

Checks if there is a underlying connection for this proxy.

Returns:boolean

true if there is a connection

isPrimaryHostIndexback to summary
pack-priv boolean isPrimaryHostIndex(int hostIndex)

Checks if the given host index points to the primary host.

Parameters
hostIndex:int

The host index in the global hosts list.

Returns:boolean

true if so

isSourceConnectionback to summary
pack-priv boolean isSourceConnection()

Implements abstract com.mysql.cj.jdbc.ha.MultiHostConnectionProxy.isSourceConnection.

Checks if current connection is to a source host.

Returns:boolean

Doc from com.mysql.cj.jdbc.ha.MultiHostConnectionProxy.isSourceConnection.

true if current connection is to a source host

Annotations
@Override
nextHostback to summary
private int nextHost(int currHostIdx, boolean vouchForPrimaryHost)

Gets the next host on the hosts list. Uses a round-robin algorithm to find the next element, but it may skip the index for the primary host. General rules to include the primary host are: - not currently connected to any host. - primary host is vouched (usually because connection to all secondary hosts has failed). - conditions to fall back to primary host are met (or they are disabled).

Parameters
currHostIdx:int

Current host index.

vouchForPrimaryHost:boolean

Allows to return the primary host index even if the usual required conditions aren't met.

Returns:int

next host index

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

Implements abstract com.mysql.cj.jdbc.ha.MultiHostConnectionProxy.pickNewConnection.

Doc from com.mysql.cj.jdbc.ha.MultiHostConnectionProxy.pickNewConnection.

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

Annotations
@Override
Exceptions
SQLException:
if an error occurs
queriesBeforeRetryPrimaryHostIsMetback to summary
private boolean queriesBeforeRetryPrimaryHostIsMet()

Checks the condition set by the property 'queriesBeforeRetrySource'.

Returns:boolean

value

readyToFallBackToPrimaryHostback to summary
pack-priv boolean readyToFallBackToPrimaryHost()

Checks if at least one of the required conditions to fall back to primary host is met, which is determined by the properties 'queriesBeforeRetrySource' and 'secondsBeforeRetrySource'.

Returns:boolean

true if ready

resetAutoFallBackCountersback to summary
private void resetAutoFallBackCounters()

Resets auto-fall back counters.

secondsBeforeRetryPrimaryHostIsMetback to summary
private boolean secondsBeforeRetryPrimaryHostIsMet()

Checks the condition set by the property 'secondsBeforeRetrySource'.

Returns:boolean

value

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

Implements abstract com.mysql.cj.jdbc.ha.MultiHostConnectionProxy.shouldExceptionTriggerConnectionSwitch.

Doc from com.mysql.cj.jdbc.ha.MultiHostConnectionProxy.shouldExceptionTriggerConnectionSwitch.

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

Annotations
@Override
switchCurrentConnectionToback to summary
private void switchCurrentConnectionTo(int hostIndex, JdbcConnection connection) throws SQLException

Replaces the previous underlying connection by the connection given. State from previous connection, if any, is synchronized with the new one.

Parameters
hostIndex:int

The host index in the global hosts list that matches the given connection.

connection:JdbcConnection

The connection instance to switch to.

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

pack-priv Class FailoverConnectionProxy.FailoverJdbcInterfaceProxy

extends JdbcInterfaceProxy
Class Inheritance

Proxy class to intercept and deal with errors that may occur in any object bound to the current connection. Additionally intercepts query executions and triggers an execution count on the outer class.

Field Summary

Inherited from com.mysql.cj.jdbc.ha.MultiHostConnectionProxy.JdbcInterfaceProxy:
invokeOn

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
)

Overrides com.mysql.cj.jdbc.ha.MultiHostConnectionProxy.JdbcInterfaceProxy.invoke.

Implements java.lang.reflect.InvocationHandler.invoke.

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

Constructor Detail

FailoverJdbcInterfaceProxyback to summary
pack-priv FailoverJdbcInterfaceProxy(Object toInvokeOn)

Method Detail

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

Overrides com.mysql.cj.jdbc.ha.MultiHostConnectionProxy.JdbcInterfaceProxy.invoke.

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
@SuppressWarnings:synthetic-access
@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.