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 boolean
allowReplicaDownConnections
protected boolean
allowSourceDownConnections
protected boolean
enableJMX
protected boolean
readFromSourceWhenNoReplicas
protected boolean
readFromSourceWhenNoReplicasOriginal
protected boolean
readOnly
protected LoadBalancedConnection
replicasConnection
protected LoadBalancedConnection
sourceConnection
Fields 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 void
addReplicaHost(java.lang.String hostPortPair)
void
addSlaveHost(java.lang.String hostPortPair)
Deprecated.static ReplicationConnection
createProxyInstance(ConnectionUrl connectionUrl)
Static factory to createReplicationConnection
instances.protected void
doAbort(java.util.concurrent.Executor executor)
Executes a abort() invocation;protected void
doAbortInternal()
Executes a abortInternal() invocation;protected void
doClose()
Executes a close() invocation;void
doPing()
Pings both l/b connections.long
getConnectionGroupId()
JdbcConnection
getCurrentConnection()
JdbcConnection
getMasterConnection()
Deprecated.JdbcConnection
getReplicasConnection()
JdbcConnection
getSlavesConnection()
Deprecated.JdbcConnection
getSourceConnection()
protected java.lang.Object
invokeMore(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)
Proxies method invocation on the java.sql.Connection interface.boolean
isHostMaster(java.lang.String hostPortPair)
Deprecated.boolean
isHostReplica(java.lang.String hostPortPair)
boolean
isHostSlave(java.lang.String hostPortPair)
Deprecated.boolean
isHostSource(java.lang.String hostPortPair)
boolean
isReadOnly()
boolean
isReplicasConnection()
Checks if current connection is the replicas l/b connection.boolean
isSlavesConnection()
Deprecated.boolean
isSourceConnection()
Checks if current connection is the sources l/b connection.protected void
pickNewConnection()
Picks the "best" connection to use from now on.void
promoteReplicaToSource(java.lang.String hostPortPair)
void
promoteSlaveToMaster(java.lang.String hostPortPair)
Deprecated.protected void
propagateProxyDown(JdbcConnection proxyConn)
Propagates the connection proxy down through all live connections.void
removeMasterHost(java.lang.String hostPortPair)
Deprecated.void
removeMasterHost(java.lang.String hostPortPair, boolean waitUntilNotInUse)
Deprecated.void
removeMasterHost(java.lang.String hostPortPair, boolean waitUntilNotInUse, boolean isNowReplica)
Deprecated.void
removeReplica(java.lang.String hostPortPair)
void
removeReplica(java.lang.String hostPortPair, boolean closeGently)
void
removeSlave(java.lang.String hostPortPair)
Deprecated.void
removeSlave(java.lang.String hostPortPair, boolean closeGently)
Deprecated.void
removeSourceHost(java.lang.String hostPortPair)
void
removeSourceHost(java.lang.String hostPortPair, boolean waitUntilNotInUse)
void
removeSourceHost(java.lang.String hostPortPair, boolean waitUntilNotInUse, boolean isNowReplica)
void
setReadOnly(boolean readOnly)
protected boolean
shouldExceptionTriggerConnectionSwitch(java.lang.Throwable t)
Has no use in replication connections.protected void
syncSessionState(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 createReplicationConnection
instances.- Parameters:
connectionUrl
- The connection URL containing the hosts in a replication setup.- Returns:
- A
ReplicationConnection
proxy. - Throws:
java.sql.SQLException
- if an error occurs
-
propagateProxyDown
Propagates the connection proxy down through all live connections.- Overrides:
propagateProxyDown
in 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:
shouldExceptionTriggerConnectionSwitch
in 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:
isSourceConnection
in 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:MultiHostConnectionProxy
Picks the "best" connection to use from now on. Each subclass needs to implement its connection switch strategy on it.- Specified by:
pickNewConnection
in 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:MultiHostConnectionProxy
Synchronizes session state between two connections, allowing to override the read-only status.- Overrides:
syncSessionState
in 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:MultiHostConnectionProxy
Executes a close() invocation;- Specified by:
doClose
in classMultiHostConnectionProxy
- Throws:
java.sql.SQLException
- if an error occurs
-
doAbortInternal
protected void doAbortInternal() throws java.sql.SQLExceptionDescription copied from class:MultiHostConnectionProxy
Executes a abortInternal() invocation;- Specified by:
doAbortInternal
in 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:MultiHostConnectionProxy
Executes a abort() invocation;- Specified by:
doAbort
in 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:
invokeMore
in 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:
doPing
in 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
-