Package com.mysql.cj.jdbc.ha.ca
Class ClusterAwareConnectionProxy
java.lang.Object
com.mysql.cj.jdbc.ha.MultiHostConnectionProxy
com.mysql.cj.jdbc.ha.ca.ClusterAwareConnectionProxy
- All Implemented Interfaces:
ConnectionLifecycleInterceptorProvider,java.lang.reflect.InvocationHandler
public class ClusterAwareConnectionProxy extends MultiHostConnectionProxy implements ConnectionLifecycleInterceptorProvider
A proxy for a dynamic com.mysql.cj.jdbc.JdbcConnection implementation that provides cluster-aware
failover features. Connection switching occurs on communications related exceptions and/or
cluster topology changes.
-
Field Summary
Fields inherited from class com.mysql.cj.jdbc.ha.MultiHostConnectionProxy
autoReconnect, closedExplicitly, closedReason, connectionUrl, currentConnection, hostsList, isClosed, lastExceptionDealtWith, thisAsConnection -
Constructor Summary
Constructors Constructor Description ClusterAwareConnectionProxy(ConnectionUrl connectionUrl)Instantiates a new AuroraConnectionProxy for the given list of hosts and connection properties. -
Method Summary
Modifier and Type Method Description static JdbcConnectionautodetectClusterAndCreateProxyInstance(ConnectionUrl connectionUrl)Checks if connection is associated with Aurora cluster and instantiates a new AuroraConnectionProxy if needed.protected voidcreateConnectionAndInitializeTopology(ConnectionUrl connUrl)protected ConnectionImplcreateConnectionForHost(HostInfo baseHostInfo)Creates a new physical connection for the givenHostInfo.static JdbcConnectioncreateProxyInstance(ConnectionUrl connectionUrl)Instantiates a new AuroraConnectionProxy.protected voiddealWithIllegalStateException(java.lang.IllegalStateException e)protected voiddealWithInvocationException(java.lang.reflect.InvocationTargetException e)Deals with InvocationException from proxied objects.protected voiddoAbort(java.util.concurrent.Executor executor)Aborts current connection using the given executor.protected voiddoAbortInternal()Aborts current connection.protected voiddoClose()Closes current connection.protected voidfailover(int failedHostIdx)Initiates the failover procedure.protected voidfailoverReader(int failedHostIdx)protected voidfailoverWriter()protected JdbcConnectiongetConnection()ConnectionLifecycleInterceptorgetConnectionLifecycleInterceptor()protected java.lang.reflect.InvocationHandlergetNewJdbcInterfaceProxy(java.lang.Object toProxy)Instantiates a new JdbcInterfaceProxy for the given object.protected voidinitLogger(ConnectionUrl connUrl)protected voidinitProxy(ConnectionUrl connUrl)protected voidinitSettings(ConnectionUrl connectionUrl)protected voidinvalidateCurrentConnection()Invalidates the current connection.java.lang.Objectinvoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)Proxies method invocation on the java.sql.Connection interface, trapping multi-host specific methods and generic methods.java.lang.ObjectinvokeMore(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)Local method invocation handling for this proxy.booleanisClusterTopologyAvailable()Checks if proxy is connected to cluster that can report its topology.protected booleanisCurrentConnectionReadOnly()protected booleanisCurrentConnectionWriter()booleanisFailoverEnabled()Checks if cluster-aware failover is enabled/possible.booleanisMultiWriterCluster()Checks if proxy is connected to multi-writer cluster.booleanisRds()Checks if the proxy is connected to an RDS-hosted cluster.booleanisRdsProxy()Checks if the proxy is connected to a cluster using RDS proxy.protected booleanisSourceConnection()Checks if current connection is to a master (writer) host.protected voidpickNewConnection()Local implementation for the new connection picker.protected voidsetConnectionProxy(JdbcConnection conn)protected booleanshouldExceptionTriggerConnectionSwitch(java.lang.Throwable t)Local implementation for the connection switch exception checker.protected voidupdateTopologyAndConnectIfNeeded(boolean forceUpdate)Methods inherited from class com.mysql.cj.jdbc.ha.MultiHostConnectionProxy
allowedOnClosedConnection, getParentProxy, getProxy, invalidateConnection, propagateProxyDown, proxyIfReturnTypeIsJdbcInterface, setProxy, syncSessionState
-
Field Details
-
NULL_LOGGER
Null logger shared by all connections at startup. -
log
The logger we're going to use. -
DEFAULT_SOCKET_TIMEOUT_MS
protected static final int DEFAULT_SOCKET_TIMEOUT_MS- See Also:
- Constant Field Values
-
DEFAULT_CONNECT_TIMEOUT_MS
protected static final int DEFAULT_CONNECT_TIMEOUT_MS- See Also:
- Constant Field Values
-
NO_CONNECTION_INDEX
protected static final int NO_CONNECTION_INDEX- See Also:
- Constant Field Values
-
WRITER_CONNECTION_INDEX
protected static final int WRITER_CONNECTION_INDEX- See Also:
- Constant Field Values
-
currentHostIndex
protected int currentHostIndex -
initialConnectionProps
protected java.util.Map<java.lang.String,java.lang.String> initialConnectionProps -
explicitlyReadOnly
protected java.lang.Boolean explicitlyReadOnly -
inTransaction
protected boolean inTransaction -
explicitlyAutoCommit
protected boolean explicitlyAutoCommit -
isClusterTopologyAvailable
protected boolean isClusterTopologyAvailable -
isMultiWriterCluster
protected boolean isMultiWriterCluster -
isRdsProxy
protected boolean isRdsProxy -
isRds
protected boolean isRds -
topologyService
-
hosts
-
writerFailoverHandler
-
readerFailoverHandler
-
connectionProvider
-
metrics
-
enableFailoverSetting
protected boolean enableFailoverSetting -
clusterTopologyRefreshRateMsSetting
protected int clusterTopologyRefreshRateMsSetting -
gatherPerfMetricsSetting
protected boolean gatherPerfMetricsSetting -
failoverTimeoutMsSetting
protected int failoverTimeoutMsSetting -
failoverClusterTopologyRefreshRateMsSetting
protected int failoverClusterTopologyRefreshRateMsSetting -
failoverWriterReconnectIntervalMsSetting
protected int failoverWriterReconnectIntervalMsSetting -
failoverReaderConnectTimeoutMsSetting
protected int failoverReaderConnectTimeoutMsSetting -
clusterIdSetting
protected java.lang.String clusterIdSetting -
clusterInstanceHostPatternSetting
protected java.lang.String clusterInstanceHostPatternSetting -
failoverConnectTimeoutMs
protected int failoverConnectTimeoutMs -
failoverSocketTimeoutMs
protected int failoverSocketTimeoutMs
-
-
Constructor Details
-
ClusterAwareConnectionProxy
Instantiates a new AuroraConnectionProxy for the given list of hosts and connection properties.- Parameters:
connectionUrl-ConnectionUrlinstance containing the lists of hosts available to switch on.- Throws:
java.sql.SQLException- if an error occurs
-
-
Method Details
-
autodetectClusterAndCreateProxyInstance
public static JdbcConnection autodetectClusterAndCreateProxyInstance(ConnectionUrl connectionUrl) throws java.sql.SQLExceptionChecks if connection is associated with Aurora cluster and instantiates a new AuroraConnectionProxy if needed. Otherwise it returns a single-host connection.- Parameters:
connectionUrl-ConnectionUrlinstance containing the lists of hosts available to switch on.- Throws:
java.sql.SQLException- if an error occurs
-
isFailoverEnabled
public boolean isFailoverEnabled()Checks if cluster-aware failover is enabled/possible.- Returns:
- true if cluster-aware failover is enabled
-
createProxyInstance
public static JdbcConnection createProxyInstance(ConnectionUrl connectionUrl) throws java.sql.SQLExceptionInstantiates a new AuroraConnectionProxy.- Parameters:
connectionUrl-ConnectionUrlinstance containing the lists of hosts available to switch on.- Throws:
java.sql.SQLException- if an error occurs
-
initSettings
- Throws:
java.sql.SQLException
-
initLogger
-
initProxy
- Throws:
java.sql.SQLException
-
createConnectionAndInitializeTopology
protected void createConnectionAndInitializeTopology(ConnectionUrl connUrl) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
setConnectionProxy
-
pickNewConnection
protected void pickNewConnection() throws java.sql.SQLExceptionLocal implementation for the new connection picker.- Specified by:
pickNewConnectionin classMultiHostConnectionProxy- Throws:
java.sql.SQLException- if an error occurs
-
createConnectionForHost
protected ConnectionImpl createConnectionForHost(HostInfo baseHostInfo) throws java.sql.SQLExceptionCreates a new physical connection for the givenHostInfo.- Overrides:
createConnectionForHostin classMultiHostConnectionProxy- Parameters:
baseHostInfo- The host info instance to base the connection off of.- Returns:
- The new Connection instance.
- Throws:
java.sql.SQLException- if an error occurs
-
failover
protected void failover(int failedHostIdx) throws java.sql.SQLExceptionInitiates the failover procedure. This process tries to establish a new connection to an instance in the topology.- Parameters:
failedHostIdx- The index of the host that failed- Throws:
java.sql.SQLException- if an error occurs
-
failoverWriter
protected void failoverWriter() throws java.sql.SQLException- Throws:
java.sql.SQLException
-
failoverReader
protected void failoverReader(int failedHostIdx) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
updateTopologyAndConnectIfNeeded
protected void updateTopologyAndConnectIfNeeded(boolean forceUpdate) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
isRds
public boolean isRds()Checks if the proxy is connected to an RDS-hosted cluster.- Returns:
- true if the proxy is connected to an RDS-hosted cluster
-
isRdsProxy
public boolean isRdsProxy()Checks if the proxy is connected to a cluster using RDS proxy.- Returns:
- true if the proxy is connected to a cluster using RDS proxy
-
invoke
public java.lang.Object invoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args) throws java.lang.ThrowableDescription copied from class:MultiHostConnectionProxyProxies 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.- Specified by:
invokein interfacejava.lang.reflect.InvocationHandler- Overrides:
invokein classMultiHostConnectionProxy- Parameters:
proxy- proxy objectmethod- method to invokeargs- method parameters- Returns:
- method result
- Throws:
java.lang.Throwable- if an error occurs
-
invokeMore
public java.lang.Object invokeMore(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args) throws java.lang.ThrowableLocal method invocation handling for this proxy. This is the continuation of MultiHostConnectionProxy#invoke(Object, Method, Object[]).- Specified by:
invokeMorein classMultiHostConnectionProxy- Parameters:
proxy- proxy objectmethod- method to invokeargs- method parameters- Returns:
- method result
- Throws:
java.lang.Throwable- if an error occurs
-
dealWithInvocationException
protected void dealWithInvocationException(java.lang.reflect.InvocationTargetException e) throws java.sql.SQLException, java.lang.Throwable, java.lang.reflect.InvocationTargetExceptionDeals with InvocationException from proxied objects.- Overrides:
dealWithInvocationExceptionin classMultiHostConnectionProxy- Parameters:
e- The Exception instance to check.- Throws:
java.sql.SQLException- if an error occursjava.lang.Throwable- if an error occursjava.lang.reflect.InvocationTargetException- if an error occurs
-
dealWithIllegalStateException
protected void dealWithIllegalStateException(java.lang.IllegalStateException e) throws java.lang.Throwable- Throws:
java.lang.Throwable
-
shouldExceptionTriggerConnectionSwitch
protected boolean shouldExceptionTriggerConnectionSwitch(java.lang.Throwable t)Local implementation for the connection switch exception checker.- Specified by:
shouldExceptionTriggerConnectionSwitchin classMultiHostConnectionProxy- Parameters:
t- The Throwable instance to analyze.- Returns:
- true if the given throwable should trigger a connection switch
-
invalidateCurrentConnection
protected void invalidateCurrentConnection() throws java.sql.SQLExceptionInvalidates the current connection.- Overrides:
invalidateCurrentConnectionin classMultiHostConnectionProxy- Throws:
java.sql.SQLException- if an error occurs
-
doClose
protected void doClose() throws java.sql.SQLExceptionCloses current connection.- Specified by:
doClosein classMultiHostConnectionProxy- Throws:
java.sql.SQLException- if an error occurs
-
doAbort
protected void doAbort(java.util.concurrent.Executor executor) throws java.sql.SQLExceptionAborts current connection using the given executor.- Specified by:
doAbortin classMultiHostConnectionProxy- Parameters:
executor- executor- Throws:
java.sql.SQLException- if an error occurs
-
doAbortInternal
protected void doAbortInternal() throws java.sql.SQLExceptionAborts current connection.- Specified by:
doAbortInternalin classMultiHostConnectionProxy- Throws:
java.sql.SQLException- if an error occurs
-
getNewJdbcInterfaceProxy
protected java.lang.reflect.InvocationHandler getNewJdbcInterfaceProxy(java.lang.Object toProxy)Description copied from class:MultiHostConnectionProxyInstantiates a new JdbcInterfaceProxy for the given object. Subclasses can override this to return instances of JdbcInterfaceProxy subclasses.- Overrides:
getNewJdbcInterfaceProxyin classMultiHostConnectionProxy- Parameters:
toProxy- The object instance to be proxied.- Returns:
- The new InvocationHandler instance.
-
isSourceConnection
protected boolean isSourceConnection()Checks if current connection is to a master (writer) host.- Specified by:
isSourceConnectionin classMultiHostConnectionProxy- Returns:
- true if current connection is to a source host
-
getConnectionLifecycleInterceptor
- Specified by:
getConnectionLifecycleInterceptorin interfaceConnectionLifecycleInterceptorProvider
-
isCurrentConnectionReadOnly
protected boolean isCurrentConnectionReadOnly() -
isCurrentConnectionWriter
protected boolean isCurrentConnectionWriter() -
getConnection
-
isClusterTopologyAvailable
public boolean isClusterTopologyAvailable()Checks if proxy is connected to cluster that can report its topology.- Returns:
- true if proxy is connected to cluster that can report its topology
-
isMultiWriterCluster
public boolean isMultiWriterCluster()Checks if proxy is connected to multi-writer cluster.- Returns:
- true if proxy is connected to multi-writer cluster
-