Class MastersReplicasListener
- java.lang.Object
-
- org.mariadb.jdbc.internal.failover.AbstractMastersListener
-
- org.mariadb.jdbc.internal.failover.AbstractMastersReplicasListener
-
- org.mariadb.jdbc.internal.failover.impl.MastersReplicasListener
-
- All Implemented Interfaces:
Listener
- Direct Known Subclasses:
AuroraListener
public class MastersReplicasListener extends AbstractMastersReplicasListener
this class handle the operation when multiple hosts.
-
-
Field Summary
Fields Modifier and Type Field Description protected List<HostAddress>
hostAddresses
protected Protocol
masterProtocol
protected Protocol
secondaryProtocol
-
Fields inherited from class org.mariadb.jdbc.internal.failover.AbstractMastersReplicasListener
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 MastersReplicasListener(UrlParser urlParser, GlobalStateInfo globalInfo)
Initialisation.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
checkInitialConnection(SQLException queryException)
boolean
checkMasterStatus(SearchFilter searchFilter)
Check master status.void
checkWaitingConnection()
Verify that there is waiting connection that have to replace failing one.List<HostAddress>
connectedHosts()
List current connected HostAddress.void
foundActiveMaster(Protocol newMasterProtocol)
Method called when a new Master connection is found after a fallback.void
foundActiveSecondary(Protocol newSecondaryProtocol)
Method called when a new secondary connection is found after a fallback.String
getCatalog()
int
getMajorServerVersion()
long
getServerThreadId()
Get current connection server id.int
getTimeout()
Get timeout (master connection possibly down).void
handleFailLoop()
void
initializeConnection()
Initialize connections.boolean
inTransaction()
Indicate if connection has an active transaction.Object
invoke(Method method, Object[] args)
boolean
isClosed()
boolean
isMasterConnected()
boolean
isMasterConnection()
boolean
isServerMariaDb()
boolean
isValid(int timeout)
void
lockAndSwitchMaster(Protocol newMasterProtocol)
Use the parameter newMasterProtocol as new current master connection.void
lockAndSwitchSecondary(Protocol newSecondaryProtocol)
Use the parameter newSecondaryProtocol as new current secondary connection.boolean
noBackslashEscapes()
void
preAbort()
void
preClose()
Called after a call on Connection.close().void
preExecute()
HandleErrorResult
primaryFail(Method method, Object[] args, boolean killCmd, boolean isClosed)
To handle the newly detected failover on the master connection.void
prolog(long maxRows, MariaDbConnection connection, MariaDbStatement statement)
void
reconnect()
Reconnect failed connection.void
reconnectFailedConnection(SearchFilter searchFilter)
Loop to connect.protected void
removeListenerFromSchedulers()
void
rePrepareOnReplica(ServerPrepareResult oldServerPrepareResult, boolean mustBeOnMaster)
void
reset()
Reset state of master and replica connection.HandleErrorResult
secondaryFail(Method method, Object[] args, boolean killCmd)
To handle the newly detected failover on the secondary connection.boolean
sessionStateAware()
void
switchReadOnlyConnection(Boolean mustBeReadOnly)
Switch to a read-only(secondary) or read and write connection(master).boolean
versionGreaterOrEqual(int major, int minor, int patch)
-
Methods inherited from class org.mariadb.jdbc.internal.failover.AbstractMastersReplicasListener
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
-
MastersReplicasListener
public MastersReplicasListener(UrlParser urlParser, GlobalStateInfo globalInfo)
Initialisation.- Parameters:
urlParser
- connection string object.globalInfo
- server global variables information
-
-
Method Detail
-
removeListenerFromSchedulers
protected void removeListenerFromSchedulers()
- Overrides:
removeListenerFromSchedulers
in classAbstractMastersListener
-
initializeConnection
public void initializeConnection() throws SQLException
Initialize connections.- Specified by:
initializeConnection
in interfaceListener
- Overrides:
initializeConnection
in classAbstractMastersListener
- Throws:
SQLException
- if a connection error append.
-
isClosed
public boolean isClosed()
- Specified by:
isClosed
in interfaceListener
- Overrides:
isClosed
in classAbstractMastersListener
-
invoke
public Object invoke(Method method, Object[] args) throws Throwable
- Specified by:
invoke
in interfaceListener
- Overrides:
invoke
in classAbstractMastersListener
- Throws:
Throwable
-
versionGreaterOrEqual
public boolean versionGreaterOrEqual(int major, int minor, int patch)
- Specified by:
versionGreaterOrEqual
in interfaceListener
- Overrides:
versionGreaterOrEqual
in classAbstractMastersListener
-
isServerMariaDb
public boolean isServerMariaDb()
- Specified by:
isServerMariaDb
in interfaceListener
- Overrides:
isServerMariaDb
in classAbstractMastersListener
-
sessionStateAware
public boolean sessionStateAware()
- Specified by:
sessionStateAware
in interfaceListener
- Overrides:
sessionStateAware
in classAbstractMastersListener
-
getCatalog
public String getCatalog() throws SQLException
- Specified by:
getCatalog
in interfaceListener
- Overrides:
getCatalog
in classAbstractMastersListener
- Throws:
SQLException
-
getMajorServerVersion
public int getMajorServerVersion()
- Specified by:
getMajorServerVersion
in interfaceListener
- Overrides:
getMajorServerVersion
in classAbstractMastersListener
-
isMasterConnection
public boolean isMasterConnection()
- Specified by:
isMasterConnection
in interfaceListener
- Overrides:
isMasterConnection
in classAbstractMastersListener
-
getTimeout
public int getTimeout() throws SocketException
Get timeout (master connection possibly down).- Specified by:
getTimeout
in interfaceListener
- Overrides:
getTimeout
in 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:
prolog
in interfaceListener
- Overrides:
prolog
in classAbstractMastersListener
- Throws:
SQLException
-
noBackslashEscapes
public boolean noBackslashEscapes()
- Specified by:
noBackslashEscapes
in interfaceListener
- Overrides:
noBackslashEscapes
in 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:
preClose
in interfaceListener
- Specified by:
preClose
in classAbstractMastersListener
-
preAbort
public void preAbort()
-
preExecute
public void preExecute() throws SQLException
- Specified by:
preExecute
in interfaceListener
- Specified by:
preExecute
in classAbstractMastersListener
- Throws:
SQLException
-
isValid
public boolean isValid(int timeout) throws SQLException
- Specified by:
isValid
in interfaceListener
- Overrides:
isValid
in classAbstractMastersListener
- Throws:
SQLException
-
checkWaitingConnection
public void checkWaitingConnection() throws SQLException
Verify 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:
reconnectFailedConnection
in interfaceListener
- Specified by:
reconnectFailedConnection
in 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:
foundActiveSecondary
in classAbstractMastersReplicasListener
- 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:
switchReadOnlyConnection
in interfaceListener
- Specified by:
switchReadOnlyConnection
in 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:
primaryFail
in interfaceListener
- Specified by:
primaryFail
in 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 SQLException
Reconnect failed connection.- Specified by:
reconnect
in interfaceListener
- Specified by:
reconnect
in 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:
secondaryFail
in classAbstractMastersReplicasListener
- 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:
handleFailLoop
in classAbstractMastersListener
-
isMasterConnected
public boolean isMasterConnected()
-
inTransaction
public boolean inTransaction()
Indicate if connection has an active transaction.- Specified by:
inTransaction
in interfaceListener
- Overrides:
inTransaction
in classAbstractMastersListener
- Returns:
- boolean
-
checkMasterStatus
public boolean checkMasterStatus(SearchFilter searchFilter)
Check master status.- Specified by:
checkMasterStatus
in interfaceListener
- Specified by:
checkMasterStatus
in classAbstractMastersListener
- Parameters:
searchFilter
- search filter- Returns:
- has some status changed
-
rePrepareOnReplica
public void rePrepareOnReplica(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 SQLException
Reset state of master and replica connection.- Throws:
SQLException
- if command fail.
-
-