Class MultiHostConnectionProxy
java.lang.Object
com.mysql.jdbc.MultiHostConnectionProxy
- All Implemented Interfaces:
InvocationHandler
- Direct Known Subclasses:
FailoverConnectionProxy, LoadBalancedConnectionProxy, ReplicationConnectionProxy
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 Class Summary
Nested ClassesModifier and TypeClassDescription(package private) classProxy class to intercept and deal with errors that may occur in any object bound to the current connection. -
Field Summary
FieldsModifier and TypeFieldDescription(package private) boolean(package private) boolean(package private) String(package private) MySQLConnection(package private) booleanprotected Throwable(package private) Properties(package private) MySQLConnection(package private) MySQLConnection -
Constructor Summary
ConstructorsConstructorDescriptionInitializes a connection wrapper for this MultiHostConnectionProxy instance.MultiHostConnectionProxy(List<String> hosts, Properties props) Constructs a MultiHostConnectionProxy instance for the given list of hosts and connection properties. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanallowedOnClosedConnection(Method method) Checks if the given method is allowed on closed connections.(package private) ConnectionImplcreateConnectionForHost(String hostPortSpec) Creates a new physical connection for the given host:port.(package private) voidDeals with InvocationException from proxied objects.(package private) abstract voidExecutes a abort() invocation;(package private) abstract voidExecutes a abortInternal() invocation;(package private) abstract voiddoClose()Executes a close() invocation;(package private) InvocationHandlergetNewJdbcInterfaceProxy(Object toProxy) Instantiates a new JdbcInterfaceProxy for the given object.(package private) MySQLConnectionWraps this object with a new multi-host Connection instance.protected MySQLConnectiongetProxy()Get this connection's proxy.(package private) intinitializeHostsSpecs(List<String> hosts, Properties props) 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.(package private) voidInvalidates the specified connection by closing it.(package private) voidInvalidates the current connection.Proxies method invocation on the java.sql.Connection interface, trapping multi-host specific methods and generic methods.(package private) abstract ObjectinvokeMore(Object proxy, Method method, Object[] args) Continuation of the method invocation process, to be implemented within each subclass.(package private) abstract booleanChecks if current connection is to a master host.(package private) abstract voidPicks the "best" connection to use from now on.protected voidpropagateProxyDown(MySQLConnection proxyConn) Propagates the connection proxy down through the multi-host connections chain.(package private) ObjectproxyIfReturnTypeIsJdbcInterface(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.protected final voidsetProxy(MySQLConnection proxyConn) Sets this connection's proxy.(package private) abstract booleanChecks if the given throwable should trigger a connection switch.(package private) voidsyncSessionState(Connection source, Connection target) Synchronizes session state between two connections.(package private) voidsyncSessionState(Connection source, Connection target, boolean readOnly) Synchronizes session state between two connections, allowing to override the read-only status.
-
Field Details
-
hostList
-
localProps
Properties localProps -
autoReconnect
boolean autoReconnect -
thisAsConnection
MySQLConnection thisAsConnection -
proxyConnection
MySQLConnection proxyConnection -
currentConnection
MySQLConnection currentConnection -
isClosed
boolean isClosed -
closedExplicitly
boolean closedExplicitly -
closedReason
String closedReason -
lastExceptionDealtWith
-
-
Constructor Details
-
MultiHostConnectionProxy
MultiHostConnectionProxy() throws SQLExceptionInitializes a connection wrapper for this MultiHostConnectionProxy instance.- Parameters:
props- The properties to be used in new internal connections.- Throws:
SQLException
-
MultiHostConnectionProxy
MultiHostConnectionProxy(List<String> hosts, Properties props) throws SQLException Constructs a MultiHostConnectionProxy instance for the given list of hosts and connection properties.- Parameters:
hosts- The lists of hosts available to switch on.props- The properties to be used in new internal connections.- Throws:
SQLException
-
-
Method Details
-
initializeHostsSpecs
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:
hosts- The list of hosts for this multi-host connection.props- Connection properties from where to get initial settings and to be used in new connections.- Returns:
- The number of hosts found in the hosts list.
-
getNewWrapperForThisAsConnection
Wraps this object with a new multi-host Connection instance.- Returns:
- The connection object instance that wraps 'this'.
- Throws:
SQLException
-
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.
-
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 dependant 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
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
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
void dealWithInvocationException(InvocationTargetException e) throws SQLException, Throwable, InvocationTargetException Deals with InvocationException from proxied objects.- Parameters:
e- The Exception instance to check.- Throws:
SQLExceptionThrowableInvocationTargetException
-
shouldExceptionTriggerConnectionSwitch
Checks if the given throwable should trigger a connection switch.- Parameters:
t- The Throwable instance to analyze.
-
isMasterConnection
abstract boolean isMasterConnection()Checks if current connection is to a master host. -
invalidateCurrentConnection
Invalidates the current connection.- Throws:
SQLException
-
invalidateConnection
Invalidates the specified connection by closing it.- Parameters:
conn- The connection instance to invalidate.- Throws:
SQLException
-
pickNewConnection
Picks the "best" connection to use from now on. Each subclass needs to implement its connection switch strategy on it.- Throws:
SQLException
-
createConnectionForHost
Creates a new physical connection for the given host:port.- Parameters:
hostPortSpec- The host:port specification.- Returns:
- The new Connection instance.
- Throws:
SQLException
-
syncSessionState
Synchronizes session state between two connections.- Parameters:
source- The connection where to get state from.target- The connection where to set state.- Throws:
SQLException
-
syncSessionState
Synchronizes 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:
SQLException
-
doClose
-
doAbortInternal
Executes a abortInternal() invocation;- Throws:
SQLException
-
doAbort
Executes a abort() invocation;- Throws:
SQLException
-
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.- Specified by:
invokein interfaceInvocationHandler- Throws:
Throwable
-
invokeMore
-
allowedOnClosedConnection
Checks if the given method is allowed on closed connections.
-