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 JdbcConnection
autodetectClusterAndCreateProxyInstance(ConnectionUrl connectionUrl)
Checks if connection is associated with Aurora cluster and instantiates a new AuroraConnectionProxy if needed.protected void
createConnectionAndInitializeTopology(ConnectionUrl connUrl)
protected ConnectionImpl
createConnectionForHost(HostInfo baseHostInfo)
Creates a new physical connection for the givenHostInfo
.static JdbcConnection
createProxyInstance(ConnectionUrl connectionUrl)
Instantiates a new AuroraConnectionProxy.protected void
dealWithIllegalStateException(java.lang.IllegalStateException e)
protected void
dealWithInvocationException(java.lang.reflect.InvocationTargetException e)
Deals with InvocationException from proxied objects.protected void
doAbort(java.util.concurrent.Executor executor)
Aborts current connection using the given executor.protected void
doAbortInternal()
Aborts current connection.protected void
doClose()
Closes current connection.protected void
failover(int failedHostIdx)
Initiates the failover procedure.protected void
failoverReader(int failedHostIdx)
protected void
failoverWriter()
protected JdbcConnection
getConnection()
ConnectionLifecycleInterceptor
getConnectionLifecycleInterceptor()
protected java.lang.reflect.InvocationHandler
getNewJdbcInterfaceProxy(java.lang.Object toProxy)
Instantiates a new JdbcInterfaceProxy for the given object.protected void
initLogger(ConnectionUrl connUrl)
protected void
initProxy(ConnectionUrl connUrl)
protected void
initSettings(ConnectionUrl connectionUrl)
protected void
invalidateCurrentConnection()
Invalidates the current connection.java.lang.Object
invoke(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.Object
invokeMore(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)
Local method invocation handling for this proxy.boolean
isClusterTopologyAvailable()
Checks if proxy is connected to cluster that can report its topology.protected boolean
isCurrentConnectionReadOnly()
protected boolean
isCurrentConnectionWriter()
boolean
isFailoverEnabled()
Checks if cluster-aware failover is enabled/possible.boolean
isMultiWriterCluster()
Checks if proxy is connected to multi-writer cluster.boolean
isRds()
Checks if the proxy is connected to an RDS-hosted cluster.boolean
isRdsProxy()
Checks if the proxy is connected to a cluster using RDS proxy.protected boolean
isSourceConnection()
Checks if current connection is to a master (writer) host.protected void
pickNewConnection()
Local implementation for the new connection picker.protected void
setConnectionProxy(JdbcConnection conn)
protected boolean
shouldExceptionTriggerConnectionSwitch(java.lang.Throwable t)
Local implementation for the connection switch exception checker.protected void
updateTopologyAndConnectIfNeeded(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
-ConnectionUrl
instance 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
-ConnectionUrl
instance 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
-ConnectionUrl
instance 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:
pickNewConnection
in 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:
createConnectionForHost
in 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:MultiHostConnectionProxy
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.- Specified by:
invoke
in interfacejava.lang.reflect.InvocationHandler
- Overrides:
invoke
in 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:
invokeMore
in 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:
dealWithInvocationException
in 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:
shouldExceptionTriggerConnectionSwitch
in 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:
invalidateCurrentConnection
in classMultiHostConnectionProxy
- Throws:
java.sql.SQLException
- if an error occurs
-
doClose
protected void doClose() throws java.sql.SQLExceptionCloses current connection.- Specified by:
doClose
in 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:
doAbort
in classMultiHostConnectionProxy
- Parameters:
executor
- executor- Throws:
java.sql.SQLException
- if an error occurs
-
doAbortInternal
protected void doAbortInternal() throws java.sql.SQLExceptionAborts current connection.- Specified by:
doAbortInternal
in classMultiHostConnectionProxy
- Throws:
java.sql.SQLException
- if an error occurs
-
getNewJdbcInterfaceProxy
protected java.lang.reflect.InvocationHandler getNewJdbcInterfaceProxy(java.lang.Object toProxy)Description copied from class:MultiHostConnectionProxy
Instantiates a new JdbcInterfaceProxy for the given object. Subclasses can override this to return instances of JdbcInterfaceProxy subclasses.- Overrides:
getNewJdbcInterfaceProxy
in 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:
isSourceConnection
in classMultiHostConnectionProxy
- Returns:
- true if current connection is to a source host
-
getConnectionLifecycleInterceptor
- Specified by:
getConnectionLifecycleInterceptor
in 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
-