类 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 blocklist for loadBalanceBlocklistTimeout ms, after which they will be removed from the blocklist 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)已过时。voidaddToGlobalBlacklist(String host, long timeout)已过时。voidaddToGlobalBlocklist(String host)Adds a host to the blocklist.voidaddToGlobalBlocklist(String host, long timeout)Adds a host to the blocklist 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()已过时。Map<String,Long>getGlobalBlocklist()Returns a local hosts blocklist, while cleaning up expired records from the global blocklist, or a blocklist with the hosts to be removed.longgetTotalPhysicalConnectionCount()longgetTransactionCount()booleaninTransaction()booleanisGlobalBlacklistEnabled()已过时。booleanisGlobalBlocklistEnabled()Checks if host blocklist 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.voidremoveFromGlobalBlacklist(String host)已过时。voidremoveFromGlobalBlocklist(String host)Removes a host from the blocklist.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
-
doPing
public void doPing() throws SQLExceptionPings live connections.- 指定者:
doPing在接口中PingTarget- 抛出:
SQLException- if an error occurs
-
addToGlobalBlocklist
public void addToGlobalBlocklist(String host, long timeout)
Adds a host to the blocklist with the given timeout.- 参数:
host- The host to be blocklisted.timeout- The blocklist timeout for this entry.
-
removeFromGlobalBlocklist
public void removeFromGlobalBlocklist(String host)
Removes a host from the blocklist.- 参数:
host- The host to be removed from the blocklist.
-
removeFromGlobalBlacklist
@Deprecated public void removeFromGlobalBlacklist(String host)
已过时。UseremoveFromGlobalBlocklist(String)instead.- 参数:
host- host
-
addToGlobalBlacklist
@Deprecated public void addToGlobalBlacklist(String host, long timeout)
已过时。UseaddToGlobalBlocklist(String, long)instead.- 参数:
host- The host to be blocklisted.timeout- The blocklist timeout for this entry.
-
addToGlobalBlocklist
public void addToGlobalBlocklist(String host)
Adds a host to the blocklist.- 参数:
host- The host to be blocklisted.
-
addToGlobalBlacklist
@Deprecated public void addToGlobalBlacklist(String host)
已过时。UseaddToGlobalBlocklist(String)instead.- 参数:
host- The host to be blocklisted.
-
isGlobalBlocklistEnabled
public boolean isGlobalBlocklistEnabled()
Checks if host blocklist management was enabled.- 返回:
- true if host blocklist management was enabled
-
isGlobalBlacklistEnabled
@Deprecated public boolean isGlobalBlacklistEnabled()
已过时。UseisGlobalBlocklistEnabled()instead.- 返回:
- true if host blocklist management was enabled
-
getGlobalBlocklist
public Map<String,Long> getGlobalBlocklist()
Returns a local hosts blocklist, while cleaning up expired records from the global blocklist, or a blocklist with the hosts to be removed.- 返回:
- A local hosts blocklist.
-
getGlobalBlacklist
@Deprecated public Map<String,Long> getGlobalBlacklist()
已过时。UsegetGlobalBlocklist()instead.- 返回:
- A local hosts blocklist.
-
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()
-
-