Class MastersSlavesListener
- java.lang.Object
-
- org.mariadb.jdbc.internal.failover.AbstractMastersListener
-
- org.mariadb.jdbc.internal.failover.AbstractMastersSlavesListener
-
- org.mariadb.jdbc.internal.failover.impl.MastersSlavesListener
-
- All Implemented Interfaces:
Listener
- Direct Known Subclasses:
AuroraListener
public class MastersSlavesListener extends AbstractMastersSlavesListener
this class handle the operation when multiple hosts.
-
-
Field Summary
Fields Modifier and Type Field Description protected List<HostAddress>hostAddressesprotected ProtocolmasterProtocolprotected ProtocolsecondaryProtocol-
Fields inherited from class org.mariadb.jdbc.internal.failover.AbstractMastersSlavesListener
waitNewMasterProtocol, waitNewSecondaryProtocol
-
Fields inherited from class org.mariadb.jdbc.internal.failover.AbstractMastersListener
currentConnectionAttempts, currentProtocol, currentReadOnlyAsked, explicitClosed, globalInfo, lastQueryNanos, lastRetry, proxy, urlParser
-
-
Constructor Summary
Constructors Constructor Description MastersSlavesListener(UrlParser urlParser, GlobalStateInfo globalInfo)Initialisation.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidcheckInitialConnection(SQLException queryException)booleancheckMasterStatus(SearchFilter searchFilter)Check master status.voidcheckWaitingConnection()Verify that there is waiting connection that have to replace failing one.List<HostAddress>connectedHosts()List current connected HostAddress.voidfoundActiveMaster(Protocol newMasterProtocol)Method called when a new Master connection is found after a fallback.voidfoundActiveSecondary(Protocol newSecondaryProtocol)Method called when a new secondary connection is found after a fallback.StringgetCatalog()intgetMajorServerVersion()longgetServerThreadId()Get current connection server id.intgetTimeout()Get timeout (master connection possibly down).voidhandleFailLoop()voidinitializeConnection()Initialize connections.booleaninTransaction()Indicate if connection has an active transaction.Objectinvoke(Method method, Object[] args)booleanisClosed()booleanisMasterConnected()booleanisMasterConnection()booleanisServerMariaDb()booleanisValid(int timeout)voidlockAndSwitchMaster(Protocol newMasterProtocol)Use the parameter newMasterProtocol as new current master connection.voidlockAndSwitchSecondary(Protocol newSecondaryProtocol)Use the parameter newSecondaryProtocol as new current secondary connection.booleannoBackslashEscapes()voidpreAbort()voidpreClose()Called after a call on Connection.close().voidpreExecute()HandleErrorResultprimaryFail(Method method, Object[] args, boolean killCmd, boolean isClosed)To handle the newly detected failover on the master connection.voidprolog(long maxRows, MariaDbConnection connection, MariaDbStatement statement)voidreconnect()Reconnect failed connection.voidreconnectFailedConnection(SearchFilter searchFilter)Loop to connect.protected voidremoveListenerFromSchedulers()voidrePrepareOnSlave(ServerPrepareResult oldServerPrepareResult, boolean mustBeOnMaster)voidreset()Reset state of master and slave connection.HandleErrorResultsecondaryFail(Method method, Object[] args, boolean killCmd)To handle the newly detected failover on the secondary connection.booleansessionStateAware()voidswitchReadOnlyConnection(Boolean mustBeReadOnly)Switch to a read-only(secondary) or read and write connection(master).booleanversionGreaterOrEqual(int major, int minor, int patch)-
Methods inherited from class org.mariadb.jdbc.internal.failover.AbstractMastersSlavesListener
getFilterForFailedHost, getSecondaryHostFailNanos, handleFailover, hasHostFail, isMasterHostFailReconnect, isSecondaryHostFail, isSecondaryHostFailReconnect, resetMasterFailoverData, resetSecondaryFailoverData, setSecondaryHostFail
-
Methods inherited from class org.mariadb.jdbc.internal.failover.AbstractMastersListener
abortConnection, addToBlacklist, canRetryFailLoop, clearBlacklist, closeConnection, getBlacklistKeys, getCurrentProtocol, getLastQueryNanos, getMasterHostFailNanos, getProxy, getRetriesAllDown, getUrlParser, invoke, isAutoReconnect, isExplicitClosed, isMasterHostFail, isQueryRelaunchable, isReadOnly, pingMasterProtocol, preAutoReconnect, relaunchOperation, removeFromBlacklist, resetOldsBlackListHosts, setMasterHostFail, setProxy, setSessionReadOnly, syncConnection, throwFailoverMessage
-
-
-
-
Field Detail
-
masterProtocol
protected Protocol masterProtocol
-
secondaryProtocol
protected Protocol secondaryProtocol
-
hostAddresses
protected List<HostAddress> hostAddresses
-
-
Constructor Detail
-
MastersSlavesListener
public MastersSlavesListener(UrlParser urlParser, GlobalStateInfo globalInfo)
Initialisation.- Parameters:
urlParser- connection string object.globalInfo- server global variables information
-
-
Method Detail
-
removeListenerFromSchedulers
protected void removeListenerFromSchedulers()
- Overrides:
removeListenerFromSchedulersin classAbstractMastersListener
-
initializeConnection
public void initializeConnection() throws SQLExceptionInitialize connections.- Specified by:
initializeConnectionin interfaceListener- Overrides:
initializeConnectionin classAbstractMastersListener- Throws:
SQLException- if a connection error append.
-
isClosed
public boolean isClosed()
- Specified by:
isClosedin interfaceListener- Overrides:
isClosedin classAbstractMastersListener
-
invoke
public Object invoke(Method method, Object[] args) throws Throwable
- Specified by:
invokein interfaceListener- Overrides:
invokein classAbstractMastersListener- Throws:
Throwable
-
versionGreaterOrEqual
public boolean versionGreaterOrEqual(int major, int minor, int patch)- Specified by:
versionGreaterOrEqualin interfaceListener- Overrides:
versionGreaterOrEqualin classAbstractMastersListener
-
isServerMariaDb
public boolean isServerMariaDb()
- Specified by:
isServerMariaDbin interfaceListener- Overrides:
isServerMariaDbin classAbstractMastersListener
-
sessionStateAware
public boolean sessionStateAware()
- Specified by:
sessionStateAwarein interfaceListener- Overrides:
sessionStateAwarein classAbstractMastersListener
-
getCatalog
public String getCatalog() throws SQLException
- Specified by:
getCatalogin interfaceListener- Overrides:
getCatalogin classAbstractMastersListener- Throws:
SQLException
-
getMajorServerVersion
public int getMajorServerVersion()
- Specified by:
getMajorServerVersionin interfaceListener- Overrides:
getMajorServerVersionin classAbstractMastersListener
-
isMasterConnection
public boolean isMasterConnection()
- Specified by:
isMasterConnectionin interfaceListener- Overrides:
isMasterConnectionin classAbstractMastersListener
-
getTimeout
public int getTimeout() throws SocketExceptionGet timeout (master connection possibly down).- Specified by:
getTimeoutin interfaceListener- Overrides:
getTimeoutin classAbstractMastersListener- Returns:
- socket timeout in ms
- Throws:
SocketException- if socket exception
-
prolog
public void prolog(long maxRows, MariaDbConnection connection, MariaDbStatement statement) throws SQLException- Specified by:
prologin interfaceListener- Overrides:
prologin classAbstractMastersListener- Throws:
SQLException
-
noBackslashEscapes
public boolean noBackslashEscapes()
- Specified by:
noBackslashEscapesin interfaceListener- Overrides:
noBackslashEscapesin classAbstractMastersListener
-
getServerThreadId
public long getServerThreadId()
Get current connection server id.- Returns:
- server id.
-
checkInitialConnection
protected void checkInitialConnection(SQLException queryException) throws SQLException
- Throws:
SQLException
-
preClose
public void preClose()
Called after a call on Connection.close(). Will explicitly closed all connections.- Specified by:
preClosein interfaceListener- Specified by:
preClosein classAbstractMastersListener
-
preAbort
public void preAbort()
-
preExecute
public void preExecute() throws SQLException- Specified by:
preExecutein interfaceListener- Specified by:
preExecutein classAbstractMastersListener- Throws:
SQLException
-
isValid
public boolean isValid(int timeout) throws SQLException- Specified by:
isValidin interfaceListener- Overrides:
isValidin classAbstractMastersListener- Throws:
SQLException
-
checkWaitingConnection
public void checkWaitingConnection() throws SQLExceptionVerify that there is waiting connection that have to replace failing one. If there is replace failed connection with new one.- Throws:
SQLException- if error occur
-
reconnectFailedConnection
public void reconnectFailedConnection(SearchFilter searchFilter) throws SQLException
Loop to connect.- Specified by:
reconnectFailedConnectionin interfaceListener- Specified by:
reconnectFailedConnectionin classAbstractMastersListener- Throws:
SQLException- if there is any error during reconnection
-
foundActiveMaster
public void foundActiveMaster(Protocol newMasterProtocol)
Method called when a new Master connection is found after a fallback.- Parameters:
newMasterProtocol- the new active connection
-
lockAndSwitchMaster
public void lockAndSwitchMaster(Protocol newMasterProtocol) throws ReconnectDuringTransactionException
Use the parameter newMasterProtocol as new current master connection.Lock must be set
- Parameters:
newMasterProtocol- new master connection- Throws:
ReconnectDuringTransactionException- if there was an active transaction.
-
foundActiveSecondary
public void foundActiveSecondary(Protocol newSecondaryProtocol) throws SQLException
Method called when a new secondary connection is found after a fallback.- Specified by:
foundActiveSecondaryin classAbstractMastersSlavesListener- Parameters:
newSecondaryProtocol- the new active connection- Throws:
SQLException- if switch failed
-
lockAndSwitchSecondary
public void lockAndSwitchSecondary(Protocol newSecondaryProtocol) throws SQLException
Use the parameter newSecondaryProtocol as new current secondary connection.- Parameters:
newSecondaryProtocol- new secondary connection- Throws:
SQLException- if an error occur during setting session read-only
-
switchReadOnlyConnection
public void switchReadOnlyConnection(Boolean mustBeReadOnly) throws SQLException
Switch to a read-only(secondary) or read and write connection(master).- Specified by:
switchReadOnlyConnectionin interfaceListener- Specified by:
switchReadOnlyConnectionin classAbstractMastersListener- Parameters:
mustBeReadOnly- the read-only status asked- Throws:
SQLException- if operation hasn't change protocol
-
primaryFail
public HandleErrorResult primaryFail(Method method, Object[] args, boolean killCmd, boolean isClosed)
To handle the newly detected failover on the master connection.- Specified by:
primaryFailin interfaceListener- Specified by:
primaryFailin classAbstractMastersListener- Parameters:
method- the initial called methodargs- the initial argskillCmd- is the fail due to a KILL cmd- Returns:
- an object to indicate if the previous Exception must be thrown, or the object resulting if a failover worked
-
reconnect
public void reconnect() throws SQLExceptionReconnect failed connection.- Specified by:
reconnectin interfaceListener- Specified by:
reconnectin classAbstractMastersListener- Throws:
SQLException- if reconnection has failed
-
secondaryFail
public HandleErrorResult secondaryFail(Method method, Object[] args, boolean killCmd) throws SQLException
To handle the newly detected failover on the secondary connection.- Specified by:
secondaryFailin classAbstractMastersSlavesListener- Parameters:
method- the initial called methodargs- the initial argskillCmd- is fail due to a KILL command- Returns:
- an object to indicate if the previous Exception must be thrown, or the object resulting if a failover worked
- Throws:
SQLException- if relaunch operation fails
-
handleFailLoop
public void handleFailLoop()
- Specified by:
handleFailLoopin classAbstractMastersListener
-
isMasterConnected
public boolean isMasterConnected()
-
inTransaction
public boolean inTransaction()
Indicate if connection has an active transaction.- Specified by:
inTransactionin interfaceListener- Overrides:
inTransactionin classAbstractMastersListener- Returns:
- boolean
-
checkMasterStatus
public boolean checkMasterStatus(SearchFilter searchFilter)
Check master status.- Specified by:
checkMasterStatusin interfaceListener- Specified by:
checkMasterStatusin classAbstractMastersListener- Parameters:
searchFilter- search filter- Returns:
- has some status changed
-
rePrepareOnSlave
public void rePrepareOnSlave(ServerPrepareResult oldServerPrepareResult, boolean mustBeOnMaster) throws SQLException
- Throws:
SQLException
-
connectedHosts
public List<HostAddress> connectedHosts()
List current connected HostAddress.- Returns:
- hostAddress List.
-
reset
public void reset() throws SQLExceptionReset state of master and slave connection.- Throws:
SQLException- if command fail.
-
-