Package com.mysql.cj.jdbc.ha
Class MultiHostConnectionProxy
java.lang.Object
com.mysql.cj.jdbc.ha.MultiHostConnectionProxy
- All Implemented Interfaces:
java.lang.reflect.InvocationHandler
- Direct Known Subclasses:
ClusterAwareConnectionProxy
,FailoverConnectionProxy
,LoadBalancedConnectionProxy
,ReplicationConnectionProxy
public abstract class MultiHostConnectionProxy
extends java.lang.Object
implements java.lang.reflect.InvocationHandler
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.
-
Field Summary
Fields Modifier and Type Field Description protected boolean
autoReconnect
protected boolean
closedExplicitly
protected java.lang.String
closedReason
protected ConnectionUrl
connectionUrl
protected JdbcConnection
currentConnection
protected java.util.List<HostInfo>
hostsList
protected boolean
isClosed
protected java.lang.Throwable
lastExceptionDealtWith
protected JdbcConnection
thisAsConnection
-
Constructor Summary
Constructors Modifier Constructor Description protected
MultiHostConnectionProxy(ConnectionUrl connectionUrl)
Constructs a MultiHostConnectionProxy instance for the given connection URL. -
Method Summary
Modifier and Type Method Description protected boolean
allowedOnClosedConnection(java.lang.reflect.Method method)
Checks if the given method is allowed on closed connections.protected ConnectionImpl
createConnectionForHost(HostInfo hostInfo)
Creates a new physical connection for the givenHostInfo
.protected void
dealWithInvocationException(java.lang.reflect.InvocationTargetException e)
Deals with InvocationException from proxied objects.protected abstract void
doAbort(java.util.concurrent.Executor executor)
Executes a abort() invocation;protected abstract void
doAbortInternal()
Executes a abortInternal() invocation;protected abstract void
doClose()
Executes a close() invocation;protected java.lang.reflect.InvocationHandler
getNewJdbcInterfaceProxy(java.lang.Object toProxy)
Instantiates a new JdbcInterfaceProxy for the given object.protected JdbcConnection
getParentProxy()
Get this connection's parent proxy.protected JdbcConnection
getProxy()
Get this connection's proxy.protected void
invalidateConnection(JdbcConnection conn)
Invalidates the specified connection by closing it.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.protected abstract java.lang.Object
invokeMore(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)
Continuation of the method invocation process, to be implemented within each subclass.protected abstract boolean
isSourceConnection()
Checks if current connection is to a source host.protected abstract void
pickNewConnection()
Picks the "best" connection to use from now on.protected void
propagateProxyDown(JdbcConnection proxyConn)
Propagates the connection proxy down through the multi-host connections chain.protected java.lang.Object
proxyIfReturnTypeIsJdbcInterface(java.lang.Class<?> returnType, java.lang.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.protected void
setProxy(JdbcConnection proxyConn)
Sets this connection's proxy.protected abstract boolean
shouldExceptionTriggerConnectionSwitch(java.lang.Throwable t)
Checks if the given throwable should trigger a connection switch.protected void
syncSessionState(JdbcConnection source, JdbcConnection target, boolean readOnly)
Synchronizes session state between two connections, allowing to override the read-only status.
-
Field Details
-
hostsList
-
connectionUrl
-
autoReconnect
protected boolean autoReconnect -
thisAsConnection
-
currentConnection
-
isClosed
protected boolean isClosed -
closedExplicitly
protected boolean closedExplicitly -
closedReason
protected java.lang.String closedReason -
lastExceptionDealtWith
protected java.lang.Throwable lastExceptionDealtWith
-
-
Constructor Details
-
MultiHostConnectionProxy
Constructs a MultiHostConnectionProxy instance for the given connection URL.- Parameters:
connectionUrl
- The connection URL.- Throws:
java.sql.SQLException
- if an error occurs
-
-
Method Details
-
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:
- Returns this connection's proxy if there is one or itself if this is the first one.
-
getParentProxy
Get this connection's parent proxy.- Returns:
- Returns this connection's proxy if there is one.
-
setProxy
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
- The top level connection in the multi-host connections chain.
-
propagateProxyDown
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
- The top level connection in the multi-host connections chain.
-
proxyIfReturnTypeIsJdbcInterface
protected java.lang.Object proxyIfReturnTypeIsJdbcInterface(java.lang.Class<?> returnType, java.lang.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
- The type the object instance to proxy is supposed to be.toProxy
- The object instance to proxy.- Returns:
- The proxied object or the original one if it does not implement a JDBC interface.
-
getNewJdbcInterfaceProxy
protected java.lang.reflect.InvocationHandler getNewJdbcInterfaceProxy(java.lang.Object toProxy)Instantiates a new JdbcInterfaceProxy for the given object. Subclasses can override this to return instances of JdbcInterfaceProxy subclasses.- Parameters:
toProxy
- The object instance to be proxied.- Returns:
- The new InvocationHandler instance.
-
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.- 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
-
shouldExceptionTriggerConnectionSwitch
protected abstract boolean shouldExceptionTriggerConnectionSwitch(java.lang.Throwable t)Checks if the given throwable should trigger a connection switch.- Parameters:
t
- The Throwable instance to analyze.- Returns:
- true if the given throwable should trigger a connection switch
-
isSourceConnection
protected abstract boolean isSourceConnection()Checks if current connection is to a source host.- Returns:
- true if current connection is to a source host
-
invalidateCurrentConnection
protected void invalidateCurrentConnection() throws java.sql.SQLExceptionInvalidates the current connection.- Throws:
java.sql.SQLException
- if an error occurs
-
invalidateConnection
Invalidates the specified connection by closing it.- Parameters:
conn
- The connection instance to invalidate.- Throws:
java.sql.SQLException
- if an error occurs
-
pickNewConnection
protected abstract void pickNewConnection() throws java.sql.SQLExceptionPicks the "best" connection to use from now on. Each subclass needs to implement its connection switch strategy on it.- Throws:
java.sql.SQLException
- if an error occurs
-
createConnectionForHost
Creates a new physical connection for the givenHostInfo
.- Parameters:
hostInfo
- The host info instance.- Returns:
- The new Connection instance.
- Throws:
java.sql.SQLException
- if an error occurs
-
syncSessionState
protected void syncSessionState(JdbcConnection source, JdbcConnection target, boolean readOnly) throws java.sql.SQLExceptionSynchronizes session state between two connections, allowing to override the read-only status.- Parameters:
source
- The connection where to get state from.target
- The connection where to set state.readOnly
- The new read-only status.- Throws:
java.sql.SQLException
- if an error occurs
-
doClose
protected abstract void doClose() throws java.sql.SQLExceptionExecutes a close() invocation;- Throws:
java.sql.SQLException
- if an error occurs
-
doAbortInternal
protected abstract void doAbortInternal() throws java.sql.SQLExceptionExecutes a abortInternal() invocation;- Throws:
java.sql.SQLException
- if an error occurs
-
doAbort
protected abstract void doAbort(java.util.concurrent.Executor executor) throws java.sql.SQLExceptionExecutes a abort() invocation;- Parameters:
executor
- executor- Throws:
java.sql.SQLException
- if an error occurs
-
invoke
public java.lang.Object invoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args) throws java.lang.ThrowableProxies 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
- Parameters:
proxy
- proxy objectmethod
- method to invokeargs
- method parameters- Returns:
- method result
- Throws:
java.lang.Throwable
- if an error occurs
-
invokeMore
protected abstract java.lang.Object invokeMore(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args) throws java.lang.ThrowableContinuation of the method invocation process, to be implemented within each subclass.- Parameters:
proxy
- proxy objectmethod
- method to invokeargs
- method parameters- Returns:
- method result
- Throws:
java.lang.Throwable
- if an error occurs
-
allowedOnClosedConnection
protected boolean allowedOnClosedConnection(java.lang.reflect.Method method)Checks if the given method is allowed on closed connections.- Parameters:
method
- method- Returns:
- true if the given method is allowed on closed connections
-