# 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 AbstractMastersReplicasListener

`waitNewMasterProtocol, waitNewSecondaryProtocol`

### Fields inherited from class AbstractMastersListener

`currentConnectionAttempts, currentProtocol, currentReadOnlyAsked, explicitClosed, globalInfo, lastQueryNanos, lastRetry, proxy, urlParser`

## Constructor Summary

Constructors
Constructor
Description
`MastersReplicasListener(UrlParser urlParser, GlobalStateInfo globalInfo)`
Initialisation.

## Method Summary

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 AbstractMastersReplicasListener

`getFilterForFailedHost, getSecondaryHostFailNanos, handleFailover, hasHostFail, isMasterHostFailReconnect, isSecondaryHostFail, isSecondaryHostFailReconnect, resetMasterFailoverData, resetSecondaryFailoverData, setSecondaryHostFail`

### Methods inherited from class 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`

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### masterProtocol

protected Protocol masterProtocol

### secondaryProtocol

protected Protocol secondaryProtocol

### hostAddresses

protected List<HostAddress> hostAddresses

## Constructor Details

### MastersReplicasListener

public MastersReplicasListener(UrlParser urlParser, GlobalStateInfo globalInfo)
Initialisation.

Parameters:
  `urlParser` - connection string object.
  `globalInfo` - server global variables information

## Method Details

### removeListenerFromSchedulers

protected void removeListenerFromSchedulers()

Overrides:
  `removeListenerFromSchedulers` in class `AbstractMastersListener`

### initializeConnection

public void initializeConnection() throws SQLException
Initialize connections.

Specified by:
  `initializeConnection` in interface `Listener`
Overrides:
  `initializeConnection` in class `AbstractMastersListener`
Throws:
  `SQLException` - if a connection error append.

### isClosed

public boolean isClosed()

Specified by:
  `isClosed` in interface `Listener`
Overrides:
  `isClosed` in class `AbstractMastersListener`

### invoke

public Object invoke(Method method, Object[] args) throws Throwable

Specified by:
  `invoke` in interface `Listener`
Overrides:
  `invoke` in class `AbstractMastersListener`
Throws:
  `Throwable`

### versionGreaterOrEqual

public boolean versionGreaterOrEqual(int major, int minor, int patch)

Specified by:
  `versionGreaterOrEqual` in interface `Listener`
Overrides:
  `versionGreaterOrEqual` in class `AbstractMastersListener`

### isServerMariaDb

public boolean isServerMariaDb()

Specified by:
  `isServerMariaDb` in interface `Listener`
Overrides:
  `isServerMariaDb` in class `AbstractMastersListener`

### sessionStateAware

public boolean sessionStateAware()

Specified by:
  `sessionStateAware` in interface `Listener`
Overrides:
  `sessionStateAware` in class `AbstractMastersListener`

### getCatalog

public String getCatalog() throws SQLException

Specified by:
  `getCatalog` in interface `Listener`
Overrides:
  `getCatalog` in class `AbstractMastersListener`
Throws:
  `SQLException`

### getMajorServerVersion

public int getMajorServerVersion()

Specified by:
  `getMajorServerVersion` in interface `Listener`
Overrides:
  `getMajorServerVersion` in class `AbstractMastersListener`

### isMasterConnection

public boolean isMasterConnection()

Specified by:
  `isMasterConnection` in interface `Listener`
Overrides:
  `isMasterConnection` in class `AbstractMastersListener`

### getTimeout

public int getTimeout() throws SocketException
Get timeout (master connection possibly down).

Specified by:
  `getTimeout` in interface `Listener`
Overrides:
  `getTimeout` in class `AbstractMastersListener`
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 interface `Listener`
Overrides:
  `prolog` in class `AbstractMastersListener`
Throws:
  `SQLException`

### noBackslashEscapes

public boolean noBackslashEscapes()

Specified by:
  `noBackslashEscapes` in interface `Listener`
Overrides:
  `noBackslashEscapes` in class `AbstractMastersListener`

### 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 interface `Listener`
Specified by:
  `preClose` in class `AbstractMastersListener`

### preAbort

public void preAbort()

### preExecute

public void preExecute() throws SQLException

Specified by:
  `preExecute` in interface `Listener`
Specified by:
  `preExecute` in class `AbstractMastersListener`
Throws:
  `SQLException`

### isValid

public boolean isValid(int timeout) throws SQLException

Specified by:
  `isValid` in interface `Listener`
Overrides:
  `isValid` in class `AbstractMastersListener`
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 interface `Listener`
Specified by:
  `reconnectFailedConnection` in class `AbstractMastersListener`
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 class `AbstractMastersReplicasListener`
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 interface `Listener`
Specified by:
  `switchReadOnlyConnection` in class `AbstractMastersListener`
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 interface `Listener`
Specified by:
  `primaryFail` in class `AbstractMastersListener`
Parameters:
  `method` - the initial called method
  `args` - the initial args
  `killCmd` - 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 interface `Listener`
Specified by:
  `reconnect` in class `AbstractMastersListener`
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 class `AbstractMastersReplicasListener`
Parameters:
  `method` - the initial called method
  `args` - the initial args
  `killCmd` - 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 class `AbstractMastersListener`

### isMasterConnected

public boolean isMasterConnected()

### inTransaction

public boolean inTransaction()
Indicate if connection has an active transaction.

Specified by:
  `inTransaction` in interface `Listener`
Overrides:
  `inTransaction` in class `AbstractMastersListener`
Returns:
  boolean

### checkMasterStatus

public boolean checkMasterStatus(SearchFilter searchFilter)
Check master status.

Specified by:
  `checkMasterStatus` in interface `Listener`
Specified by:
  `checkMasterStatus` in class `AbstractMastersListener`
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.