Class LoadBalancedConnectionProxy
java.lang.Object
com.mysql.jdbc.MultiHostConnectionProxy
com.mysql.jdbc.LoadBalancedConnectionProxy
- All Implemented Interfaces:
PingTarget, InvocationHandler
A proxy for a dynamic com.mysql.jdbc.Connection implementation that load balances requests across a series of MySQL JDBC connections, where the balancing
takes place at transaction commit.
Therefore, for this to work (at all), you must use transactions, even if only reading data.
This implementation will invalidate connections that it detects have had communication errors when processing a request. Problematic hosts will be added to a
global blacklist for loadBalanceBlacklistTimeout ms, after which they will be removed from the blacklist and made eligible once again to be selected for new
connections.
This implementation is thread-safe, but it's questionable whether sharing a connection instance amongst threads is a good idea, given that transactions are
scoped to connections in JDBC.
-
Nested Class Summary
Nested classes/interfaces inherited from class MultiHostConnectionProxy
MultiHostConnectionProxy.JdbcInterfaceProxy -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final Stringprotected Map<String, ConnectionImpl> Fields inherited from class MultiHostConnectionProxy
autoReconnect, closedExplicitly, closedReason, currentConnection, hostList, isClosed, lastExceptionDealtWith, localProps, proxyConnection, thisAsConnection -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdds a host to the hosts list.voidaddToGlobalBlacklist(String host) Adds a host to the blacklist.voidaddToGlobalBlacklist(String host, long timeout) Adds a host to the blacklist with the given timeout.createConnectionForHost(String hostPortSpec) Creates a new physical connection for the given host:port and updates required internal mappings and statistics for that connection.static LoadBalancedConnectioncreateProxyInstance(List<String> hosts, Properties props) (package private) voidAborts all live connections, using the provided Executor.(package private) voidAborts all live connections(package private) voiddoClose()Closes all live connections.voiddoPing()Pings live connections.longlonglongReturns a local hosts blacklist, while cleaning up expired records from the global blacklist, or a blacklist with the hosts to be removed.(package private) MySQLConnectionWraps this object with a new load balanced Connection instance.(package private) static LoadBalancedConnectionlonglongboolean(package private) voidCloses specified connection and removes it from required mappings.invokeMore(Object proxy, Method method, Object[] args) Proxies method invocation on the java.sql.Connection interface, trapping "close", "isClosed" and "commit/rollback" to switch connections for load balancing.booleanChecks if host blacklist management was enabled.(package private) booleanAlways returns 'true' as there are no "masters" and "slaves" in this type of connection.(package private) voidPicks the "best" connection to use for the next transaction based on the BalanceStrategy in use.protected voidpropagateProxyDown(MySQLConnection proxyConn) Propagates the connection proxy down through all live connections.voidremoveHost(String hostPortPair) Removes a host from the host list.voidremoveHostWhenNotInUse(String hostPortPair) Removes a host from the host list, allowing it some time to be released gracefully if needed.(package private) booleanConsults the registered LoadBalanceExceptionChecker if the given exception should trigger a connection fail-over.(package private) voidsyncSessionState(Connection source, Connection target, boolean readOnly) Synchronizes session state between two connections, allowing to override the read-only status.
-
Field Details
-
liveConnections
-
BLACKLIST_TIMEOUT_PROPERTY_KEY
- See Also:
-
HOST_REMOVAL_GRACE_PERIOD_PROPERTY_KEY
- See Also:
-
-
Method Details
-
createProxyInstance
public static LoadBalancedConnection createProxyInstance(List<String> hosts, Properties props) throws SQLException - Throws:
SQLException
-
getNewWrapperForThisAsConnection
Wraps this object with a new load balanced Connection instance.- Overrides:
getNewWrapperForThisAsConnectionin classMultiHostConnectionProxy- Returns:
- The connection object instance that wraps 'this'.
- Throws:
SQLException
-
propagateProxyDown
Propagates the connection proxy down through all live connections.- Overrides:
propagateProxyDownin classMultiHostConnectionProxy- Parameters:
proxyConn- The top level connection in the multi-host connections chain.
-
shouldExceptionTriggerConnectionSwitch
Consults the registered LoadBalanceExceptionChecker if the given exception should trigger a connection fail-over.- Specified by:
shouldExceptionTriggerConnectionSwitchin classMultiHostConnectionProxy- Parameters:
t- The Throwable instance to analyze.ex- The Exception instance to check.
-
isMasterConnection
boolean isMasterConnection()Always returns 'true' as there are no "masters" and "slaves" in this type of connection.- Specified by:
isMasterConnectionin classMultiHostConnectionProxy
-
invalidateConnection
Closes specified connection and removes it from required mappings.- Overrides:
invalidateConnectionin classMultiHostConnectionProxy- Parameters:
conn-- Throws:
SQLException
-
pickNewConnection
Picks the "best" connection to use for the next transaction based on the BalanceStrategy in use.- Specified by:
pickNewConnectionin classMultiHostConnectionProxy- Throws:
SQLException
-
createConnectionForHost
Creates a new physical connection for the given host:port and updates required internal mappings and statistics for that connection.- Overrides:
createConnectionForHostin classMultiHostConnectionProxy- Parameters:
hostPortSpec- The host:port specification.- Returns:
- The new Connection instance.
- Throws:
SQLException
-
syncSessionState
Description copied from class:MultiHostConnectionProxySynchronizes session state between two connections, allowing to override the read-only status.- Overrides:
syncSessionStatein classMultiHostConnectionProxy- 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
void doAbortInternal()Aborts all live connections- Specified by:
doAbortInternalin classMultiHostConnectionProxy
-
doAbort
Aborts all live connections, using the provided Executor.- Specified by:
doAbortin classMultiHostConnectionProxy
-
invokeMore
Proxies method invocation on the java.sql.Connection interface, trapping "close", "isClosed" and "commit/rollback" to switch connections for load balancing. This is the continuation of MultiHostConnectionProxy#invoke(Object, Method, Object[]).- Specified by:
invokeMorein classMultiHostConnectionProxy- Throws:
Throwable
-
doPing
Pings live connections.- Specified by:
doPingin interfacePingTarget- Throws:
SQLException
-
addToGlobalBlacklist
Adds a host to the blacklist with the given timeout.- Parameters:
host- The host to be blacklisted.timeout- The blacklist timeout for this entry.
-
addToGlobalBlacklist
Adds a host to the blacklist.- Parameters:
host- The host to be blacklisted.
-
isGlobalBlacklistEnabled
public boolean isGlobalBlacklistEnabled()Checks if host blacklist management was enabled. -
getGlobalBlacklist
-
removeHostWhenNotInUse
Removes a host from the host list, allowing it some time to be released gracefully if needed.- Parameters:
hostPortPair- The host to be removed.- Throws:
SQLException
-
removeHost
Removes a host from the host list.- Parameters:
hostPortPair- The host to be removed.- Throws:
SQLException
-
addHost
Adds a host to the hosts list.- Parameters:
hostPortPair- The host to be added.
-
inTransaction
public boolean inTransaction() -
getTransactionCount
public long getTransactionCount() -
getActivePhysicalConnectionCount
public long getActivePhysicalConnectionCount() -
getTotalPhysicalConnectionCount
public long getTotalPhysicalConnectionCount() -
getConnectionGroupProxyID
public long getConnectionGroupProxyID() -
getCurrentActiveHost
-
getCurrentTransactionDuration
public long getCurrentTransactionDuration() -
getNullLoadBalancedConnectionInstance
-