类 LoadBalancedConnectionProxy
- java.lang.Object
-
- com.mysql.cj.jdbc.ha.MultiHostConnectionProxy
-
- com.mysql.cj.jdbc.ha.LoadBalancedConnectionProxy
-
- 所有已实现的接口:
PingTarget,InvocationHandler
public class LoadBalancedConnectionProxy extends MultiHostConnectionProxy implements PingTarget
A proxy for a dynamic com.mysql.cj.jdbc.JdbcConnection 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.
-
-
字段概要
字段 修饰符和类型 字段 说明 protected Map<String,ConnectionImpl>liveConnections-
从类继承的字段 com.mysql.cj.jdbc.ha.MultiHostConnectionProxy
connectionUrl, lastExceptionDealtWith
-
-
构造器概要
构造器 构造器 说明 LoadBalancedConnectionProxy(ConnectionUrl connectionUrl)Creates a proxy for java.sql.Connection that routes requests between the hosts in the connection URL.
-
方法概要
所有方法 静态方法 实例方法 具体方法 已过时的方法 修饰符和类型 方法 说明 booleanaddHost(String hostPortPair)Adds 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.ConnectionImplcreateConnectionForHost(HostInfo hostInfo)Creates a new physical connection for the givenHostInfoand updates required internal mappings and statistics for that connection.ConnectionImplcreateConnectionForHost(String hostPortPair)Creates a new physical connection for the given host:port info.static LoadBalancedConnectioncreateProxyInstance(ConnectionUrl connectionUrl)Static factory to createLoadBalancedConnectioninstances.voiddoPing()Pings live connections.longgetActivePhysicalConnectionCount()longgetConnectionGroupProxyID()StringgetCurrentActiveHost()longgetCurrentTransactionDuration()Map<String,Long>getGlobalBlacklist()Returns a local hosts blacklist, while cleaning up expired records from the global blacklist, or a blacklist with the hosts to be removed.longgetTotalPhysicalConnectionCount()longgetTransactionCount()booleaninTransaction()ObjectinvokeMore(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.booleanisGlobalBlacklistEnabled()Checks if host blacklist management was enabled.voidpickNewConnection()Picks the "best" connection to use for the next transaction based on the BalanceStrategy in use.protected voidpropagateProxyDown(JdbcConnection 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.booleanshouldExceptionTriggerFailover(Throwable t)已过时。-
从类继承的方法 com.mysql.cj.jdbc.ha.MultiHostConnectionProxy
allowedOnClosedConnection, getParentProxy, getProxy, invoke, setProxy
-
-
-
-
字段详细资料
-
liveConnections
protected Map<String,ConnectionImpl> liveConnections
-
-
构造器详细资料
-
LoadBalancedConnectionProxy
public LoadBalancedConnectionProxy(ConnectionUrl connectionUrl) throws SQLException
Creates a proxy for java.sql.Connection that routes requests between the hosts in the connection URL.- 参数:
connectionUrl- The connection URL containing the hosts to load balance.- 抛出:
SQLException- if an error occurs
-
-
方法详细资料
-
createProxyInstance
public static LoadBalancedConnection createProxyInstance(ConnectionUrl connectionUrl) throws SQLException
Static factory to createLoadBalancedConnectioninstances.- 参数:
connectionUrl- The connection URL containing the hosts in a load-balance setup.- 返回:
- A
LoadBalancedConnectionproxy. - 抛出:
SQLException- if an error occurs
-
propagateProxyDown
protected void propagateProxyDown(JdbcConnection proxyConn)
Propagates the connection proxy down through all live connections.- 覆盖:
propagateProxyDown在类中MultiHostConnectionProxy- 参数:
proxyConn- The top level connection in the multi-host connections chain.
-
shouldExceptionTriggerFailover
@Deprecated public boolean shouldExceptionTriggerFailover(Throwable t)
已过时。
-
pickNewConnection
public void pickNewConnection() throws SQLExceptionPicks the "best" connection to use for the next transaction based on the BalanceStrategy in use.- 抛出:
SQLException- if an error occurs
-
createConnectionForHost
public ConnectionImpl createConnectionForHost(HostInfo hostInfo) throws SQLException
Creates a new physical connection for the givenHostInfoand updates required internal mappings and statistics for that connection.- 参数:
hostInfo- The host info instance.- 返回:
- The new Connection instance.
- 抛出:
SQLException- if an error occurs
-
createConnectionForHost
public ConnectionImpl createConnectionForHost(String hostPortPair) throws SQLException
Creates a new physical connection for the given host:port info. If the this connection's connection URL knows about this host:port then its host info is used, otherwise a new host info based on current connection URL defaults is spawned.- 参数:
hostPortPair- The host:port pair identifying the host to connect to.- 返回:
- The new Connection instance.
- 抛出:
SQLException- if an error occurs
-
invokeMore
public Object invokeMore(Object proxy, Method method, Object[] args) throws Throwable
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[]).- 抛出:
Throwable
-
doPing
public void doPing() throws SQLExceptionPings live connections.- 指定者:
doPing在接口中PingTarget- 抛出:
SQLException- if an error occurs
-
addToGlobalBlacklist
public void addToGlobalBlacklist(String host, long timeout)
Adds a host to the blacklist with the given timeout.- 参数:
host- The host to be blacklisted.timeout- The blacklist timeout for this entry.
-
addToGlobalBlacklist
public void addToGlobalBlacklist(String host)
Adds a host to the blacklist.- 参数:
host- The host to be blacklisted.
-
isGlobalBlacklistEnabled
public boolean isGlobalBlacklistEnabled()
Checks if host blacklist management was enabled.- 返回:
- true if host blacklist management was enabled
-
getGlobalBlacklist
public Map<String,Long> getGlobalBlacklist()
Returns a local hosts blacklist, while cleaning up expired records from the global blacklist, or a blacklist with the hosts to be removed.- 返回:
- A local hosts blacklist.
-
removeHostWhenNotInUse
public void removeHostWhenNotInUse(String hostPortPair) throws SQLException
Removes a host from the host list, allowing it some time to be released gracefully if needed.- 参数:
hostPortPair- The host to be removed.- 抛出:
SQLException- if an error occurs
-
removeHost
public void removeHost(String hostPortPair) throws SQLException
Removes a host from the host list.- 参数:
hostPortPair- The host to be removed.- 抛出:
SQLException- if an error occurs
-
addHost
public boolean addHost(String hostPortPair)
Adds a host to the hosts list.- 参数:
hostPortPair- The host to be added.- 返回:
- true if host was added and false if the host list already contains it
-
inTransaction
public boolean inTransaction()
-
getTransactionCount
public long getTransactionCount()
-
getActivePhysicalConnectionCount
public long getActivePhysicalConnectionCount()
-
getTotalPhysicalConnectionCount
public long getTotalPhysicalConnectionCount()
-
getConnectionGroupProxyID
public long getConnectionGroupProxyID()
-
getCurrentActiveHost
public String getCurrentActiveHost()
-
getCurrentTransactionDuration
public long getCurrentTransactionDuration()
-
-