Package com.mysql.cj.jdbc.ha
Class ReplicationConnectionProxy
java.lang.Object
com.mysql.cj.jdbc.ha.MultiHostConnectionProxy
com.mysql.cj.jdbc.ha.ReplicationConnectionProxy
- All Implemented Interfaces:
PingTarget,java.lang.reflect.InvocationHandler
public class ReplicationConnectionProxy extends MultiHostConnectionProxy implements PingTarget
Connection that opens two connections, one two a replication source, and another to one or more replicas, and decides to use source when the connection is
not
read-only, and use replica(s) when the connection is read-only.
-
Field Summary
Fields Modifier and Type Field Description protected booleanallowReplicaDownConnectionsprotected booleanallowSourceDownConnectionsprotected booleanenableJMXprotected booleanreadFromSourceWhenNoReplicasprotected booleanreadFromSourceWhenNoReplicasOriginalprotected booleanreadOnlyprotected LoadBalancedConnectionreplicasConnectionprotected LoadBalancedConnectionsourceConnectionFields inherited from class com.mysql.cj.jdbc.ha.MultiHostConnectionProxy
autoReconnect, closedExplicitly, closedReason, connectionUrl, currentConnection, hostsList, isClosed, lastExceptionDealtWith, thisAsConnection -
Method Summary
Modifier and Type Method Description voidaddReplicaHost(java.lang.String hostPortPair)voidaddSlaveHost(java.lang.String hostPortPair)Deprecated.static ReplicationConnectioncreateProxyInstance(ConnectionUrl connectionUrl)Static factory to createReplicationConnectioninstances.protected voiddoAbort(java.util.concurrent.Executor executor)Executes a abort() invocation;protected voiddoAbortInternal()Executes a abortInternal() invocation;protected voiddoClose()Executes a close() invocation;voiddoPing()Pings both l/b connections.longgetConnectionGroupId()JdbcConnectiongetCurrentConnection()JdbcConnectiongetMasterConnection()Deprecated.JdbcConnectiongetReplicasConnection()JdbcConnectiongetSlavesConnection()Deprecated.JdbcConnectiongetSourceConnection()protected java.lang.ObjectinvokeMore(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)Proxies method invocation on the java.sql.Connection interface.booleanisHostMaster(java.lang.String hostPortPair)Deprecated.booleanisHostReplica(java.lang.String hostPortPair)booleanisHostSlave(java.lang.String hostPortPair)Deprecated.booleanisHostSource(java.lang.String hostPortPair)booleanisReadOnly()booleanisReplicasConnection()Checks if current connection is the replicas l/b connection.booleanisSlavesConnection()Deprecated.booleanisSourceConnection()Checks if current connection is the sources l/b connection.protected voidpickNewConnection()Picks the "best" connection to use from now on.voidpromoteReplicaToSource(java.lang.String hostPortPair)voidpromoteSlaveToMaster(java.lang.String hostPortPair)Deprecated.protected voidpropagateProxyDown(JdbcConnection proxyConn)Propagates the connection proxy down through all live connections.voidremoveMasterHost(java.lang.String hostPortPair)Deprecated.voidremoveMasterHost(java.lang.String hostPortPair, boolean waitUntilNotInUse)Deprecated.voidremoveMasterHost(java.lang.String hostPortPair, boolean waitUntilNotInUse, boolean isNowReplica)Deprecated.voidremoveReplica(java.lang.String hostPortPair)voidremoveReplica(java.lang.String hostPortPair, boolean closeGently)voidremoveSlave(java.lang.String hostPortPair)Deprecated.voidremoveSlave(java.lang.String hostPortPair, boolean closeGently)Deprecated.voidremoveSourceHost(java.lang.String hostPortPair)voidremoveSourceHost(java.lang.String hostPortPair, boolean waitUntilNotInUse)voidremoveSourceHost(java.lang.String hostPortPair, boolean waitUntilNotInUse, boolean isNowReplica)voidsetReadOnly(boolean readOnly)protected booleanshouldExceptionTriggerConnectionSwitch(java.lang.Throwable t)Has no use in replication connections.protected voidsyncSessionState(JdbcConnection source, JdbcConnection target, boolean readonly)Synchronizes session state between two connections, allowing to override the read-only status.Methods inherited from class com.mysql.cj.jdbc.ha.MultiHostConnectionProxy
allowedOnClosedConnection, createConnectionForHost, dealWithInvocationException, getNewJdbcInterfaceProxy, getParentProxy, getProxy, invalidateConnection, invalidateCurrentConnection, invoke, proxyIfReturnTypeIsJdbcInterface, setProxy
-
Field Details
-
enableJMX
protected boolean enableJMX -
allowSourceDownConnections
protected boolean allowSourceDownConnections -
allowReplicaDownConnections
protected boolean allowReplicaDownConnections -
readFromSourceWhenNoReplicas
protected boolean readFromSourceWhenNoReplicas -
readFromSourceWhenNoReplicasOriginal
protected boolean readFromSourceWhenNoReplicasOriginal -
readOnly
protected boolean readOnly -
sourceConnection
-
replicasConnection
-
-
Method Details
-
createProxyInstance
public static ReplicationConnection createProxyInstance(ConnectionUrl connectionUrl) throws java.sql.SQLExceptionStatic factory to createReplicationConnectioninstances.- Parameters:
connectionUrl- The connection URL containing the hosts in a replication setup.- Returns:
- A
ReplicationConnectionproxy. - Throws:
java.sql.SQLException- if an error occurs
-
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
protected boolean shouldExceptionTriggerConnectionSwitch(java.lang.Throwable t)Has no use in replication connections. Always returnfalse.- Specified by:
shouldExceptionTriggerConnectionSwitchin classMultiHostConnectionProxy- Parameters:
t- The Exception instance to check.- Returns:
- true if the given throwable should trigger a connection switch
-
isSourceConnection
public boolean isSourceConnection()Checks if current connection is the sources l/b connection.- Specified by:
isSourceConnectionin classMultiHostConnectionProxy- Returns:
- true if current connection is to a source host
-
isReplicasConnection
public boolean isReplicasConnection()Checks if current connection is the replicas l/b connection.- Returns:
- true if current connection is the replicas l/b connection
-
isSlavesConnection
@Deprecated public boolean isSlavesConnection()Deprecated.UseisReplicasConnection()instead.- Returns:
- true if it's a replicas connection
-
pickNewConnection
protected void pickNewConnection() throws java.sql.SQLExceptionDescription copied from class:MultiHostConnectionProxyPicks the "best" connection to use from now on. Each subclass needs to implement its connection switch strategy on it.- Specified by:
pickNewConnectionin classMultiHostConnectionProxy- Throws:
java.sql.SQLException- if an error occurs
-
syncSessionState
protected void syncSessionState(JdbcConnection source, JdbcConnection target, boolean readonly) throws java.sql.SQLExceptionDescription 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:
java.sql.SQLException- if an error occurs
-
doClose
protected void doClose() throws java.sql.SQLExceptionDescription copied from class:MultiHostConnectionProxyExecutes a close() invocation;- Specified by:
doClosein classMultiHostConnectionProxy- Throws:
java.sql.SQLException- if an error occurs
-
doAbortInternal
protected void doAbortInternal() throws java.sql.SQLExceptionDescription copied from class:MultiHostConnectionProxyExecutes a abortInternal() invocation;- Specified by:
doAbortInternalin classMultiHostConnectionProxy- Throws:
java.sql.SQLException- if an error occurs
-
doAbort
protected void doAbort(java.util.concurrent.Executor executor) throws java.sql.SQLExceptionDescription copied from class:MultiHostConnectionProxyExecutes a abort() invocation;- Specified by:
doAbortin classMultiHostConnectionProxy- Parameters:
executor- executor- Throws:
java.sql.SQLException- if an error occurs
-
invokeMore
protected java.lang.Object invokeMore(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. This is the continuation of MultiHostConnectionProxy#invoke(Object, Method, Object[]).- Specified by:
invokeMorein classMultiHostConnectionProxy- Parameters:
proxy- proxy objectmethod- method to invokeargs- method parameters- Returns:
- method result
- Throws:
java.lang.Throwable- if an error occurs
-
doPing
public void doPing() throws java.sql.SQLExceptionPings both l/b connections. Switch to another connection in case of failure.- Specified by:
doPingin interfacePingTarget- Throws:
java.sql.SQLException
-
getCurrentConnection
-
getConnectionGroupId
public long getConnectionGroupId() -
getSourceConnection
-
getMasterConnection
Deprecated.UsegetSourceConnection()instead.- Returns:
JdbcConnection
-
promoteReplicaToSource
public void promoteReplicaToSource(java.lang.String hostPortPair) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
promoteSlaveToMaster
@Deprecated public void promoteSlaveToMaster(java.lang.String hostPortPair) throws java.sql.SQLExceptionDeprecated.UsepromoteReplicaToSource(String)instead.- Parameters:
hostPortPair- host:port- Throws:
java.sql.SQLException
-
removeSourceHost
public void removeSourceHost(java.lang.String hostPortPair) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
removeMasterHost
@Deprecated public void removeMasterHost(java.lang.String hostPortPair) throws java.sql.SQLExceptionDeprecated.UseremoveSourceHost(String)instead.- Parameters:
hostPortPair- host:port- Throws:
java.sql.SQLException
-
removeSourceHost
public void removeSourceHost(java.lang.String hostPortPair, boolean waitUntilNotInUse) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
removeMasterHost
@Deprecated public void removeMasterHost(java.lang.String hostPortPair, boolean waitUntilNotInUse) throws java.sql.SQLExceptionDeprecated.UseremoveSourceHost(String, boolean)instead.- Parameters:
hostPortPair- host:portwaitUntilNotInUse- remove only when not in use- Throws:
java.sql.SQLException
-
removeSourceHost
public void removeSourceHost(java.lang.String hostPortPair, boolean waitUntilNotInUse, boolean isNowReplica) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
removeMasterHost
@Deprecated public void removeMasterHost(java.lang.String hostPortPair, boolean waitUntilNotInUse, boolean isNowReplica) throws java.sql.SQLExceptionDeprecated.UseremoveSourceHost(String, boolean, boolean)instead.- Parameters:
hostPortPair- host:portwaitUntilNotInUse- remove only when not in useisNowReplica- place to replicas- Throws:
java.sql.SQLException
-
isHostSource
public boolean isHostSource(java.lang.String hostPortPair) -
isHostMaster
@Deprecated public boolean isHostMaster(java.lang.String hostPortPair)Deprecated.UseisHostSource(String)instead.- Parameters:
hostPortPair- host:port- Returns:
- true if it's a source host
-
getReplicasConnection
-
getSlavesConnection
Deprecated.UsegetReplicasConnection()instead.- Returns:
JdbcConnection
-
addReplicaHost
public void addReplicaHost(java.lang.String hostPortPair) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
addSlaveHost
@Deprecated public void addSlaveHost(java.lang.String hostPortPair) throws java.sql.SQLExceptionDeprecated.UseaddReplicaHost(String)instead.- Parameters:
hostPortPair- host:port- Throws:
java.sql.SQLException
-
removeReplica
public void removeReplica(java.lang.String hostPortPair) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
removeSlave
@Deprecated public void removeSlave(java.lang.String hostPortPair) throws java.sql.SQLExceptionDeprecated.UseremoveReplica(String)instead.- Parameters:
hostPortPair- host:port- Throws:
java.sql.SQLException
-
removeReplica
public void removeReplica(java.lang.String hostPortPair, boolean closeGently) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
removeSlave
@Deprecated public void removeSlave(java.lang.String hostPortPair, boolean closeGently) throws java.sql.SQLExceptionDeprecated.UseremoveReplica(String, boolean)instead.- Parameters:
hostPortPair- host:portcloseGently- option- Throws:
java.sql.SQLException
-
isHostReplica
public boolean isHostReplica(java.lang.String hostPortPair) -
isHostSlave
@Deprecated public boolean isHostSlave(java.lang.String hostPortPair)Deprecated.UseisHostReplica(String)instead.- Parameters:
hostPortPair- host:port- Returns:
- true if it's a replica
-
setReadOnly
public void setReadOnly(boolean readOnly) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
isReadOnly
public boolean isReadOnly() throws java.sql.SQLException- Throws:
java.sql.SQLException
-