# Class MastersFailoverListener

java.lang.Object
org.mariadb.jdbc.internal.failover.AbstractMastersListener
org.mariadb.jdbc.internal.failover.impl.MastersFailoverListener

All Implemented Interfaces:
  `Listener`

public class MastersFailoverListener extends AbstractMastersListener

## Field Summary

### Fields inherited from class AbstractMastersListener

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

## Constructor Summary

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

## Method Summary

Modifier and Type
Method
Description
`boolean`
`checkMasterStatus(SearchFilter searchFilter)`
Check master status.
`void`
`foundActiveMaster(Protocol protocol)`
method called when a new Master connection is found after a fallback.
`long`
`getServerThreadId()`
`void`
`handleFailLoop()`
Add listener to FailoverLoop if master connection is not active, so a reconnection will be done.
`void`
`initializeConnection()`
Connect to database.
`boolean`
`isMasterConnected()`
`void`
`preAbort()`
`void`
`preClose()`
`void`
`preExecute()`
Before executing query, reconnect if connection is closed, and autoReconnect option is set.
`HandleErrorResult`
`primaryFail(Method method, Object[] args, boolean killCmd, boolean alreadyClosed)`
`void`
`reconnect()`
Try to reconnect connection.
`void`
`reconnectFailedConnection(SearchFilter searchFilter)`
Loop to connect failed hosts.
`void`
`rePrepareOnReplica(ServerPrepareResult oldServerPrepareResult, boolean mustExecuteOnReplica)`
`void`
`reset()`
Reset state of master connection.
`void`
`switchReadOnlyConnection(Boolean mustBeReadOnly)`
Force session to read-only according to options.

### Methods inherited from class AbstractMastersListener

`abortConnection, addToBlacklist, canRetryFailLoop, clearBlacklist, closeConnection, getBlacklistKeys, getCatalog, getCurrentProtocol, getFilterForFailedHost, getLastQueryNanos, getMajorServerVersion, getMasterHostFailNanos, getProxy, getRetriesAllDown, getTimeout, getUrlParser, handleFailover, hasHostFail, inTransaction, invoke, invoke, isAutoReconnect, isClosed, isExplicitClosed, isMasterConnection, isMasterHostFail, isQueryRelaunchable, isReadOnly, isServerMariaDb, isValid, noBackslashEscapes, pingMasterProtocol, preAutoReconnect, prolog, relaunchOperation, removeFromBlacklist, removeListenerFromSchedulers, resetMasterFailoverData, resetOldsBlackListHosts, sessionStateAware, setMasterHostFail, setProxy, setSessionReadOnly, syncConnection, throwFailoverMessage, versionGreaterOrEqual`

### Methods inherited from class Object

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

## Constructor Details

### MastersFailoverListener

public MastersFailoverListener(UrlParser urlParser, GlobalStateInfo globalInfo)
Initialisation.

Parameters:
  `urlParser` - url options.
  `globalInfo` - server global variables information

## Method Details

### initializeConnection

public void initializeConnection() throws SQLException
Connect to database.

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

### preExecute

public void preExecute() throws SQLException
Before executing query, reconnect if connection is closed, and autoReconnect option is set.

Specified by:
  `preExecute` in interface `Listener`
Specified by:
  `preExecute` in class `AbstractMastersListener`
Throws:
  `SQLException` - if connection has been explicitly closed.

### preClose

public void preClose()

Specified by:
  `preClose` in interface `Listener`
Specified by:
  `preClose` in class `AbstractMastersListener`

### getServerThreadId

public long getServerThreadId()

### preAbort

public void preAbort()

### primaryFail

public HandleErrorResult primaryFail(Method method, Object[] args, boolean killCmd, boolean alreadyClosed)

Specified by:
  `primaryFail` in interface `Listener`
Specified by:
  `primaryFail` in class `AbstractMastersListener`

### reconnectFailedConnection

public void reconnectFailedConnection(SearchFilter searchFilter) throws SQLException
Loop to connect failed hosts.

Specified by:
  `reconnectFailedConnection` in interface `Listener`
Specified by:
  `reconnectFailedConnection` in class `AbstractMastersListener`
Parameters:
  `searchFilter` - search parameters.
Throws:
  `SQLException` - if there is any error during reconnection

### switchReadOnlyConnection

public void switchReadOnlyConnection(Boolean mustBeReadOnly) throws SQLException
Force session to read-only according to options.

Specified by:
  `switchReadOnlyConnection` in interface `Listener`
Specified by:
  `switchReadOnlyConnection` in class `AbstractMastersListener`
Parameters:
  `mustBeReadOnly` - is read-only flag
Throws:
  `SQLException` - if a connection error occur

### foundActiveMaster

public void foundActiveMaster(Protocol protocol) throws SQLException
method called when a new Master connection is found after a fallback.

Parameters:
  `protocol` - the new active connection
Throws:
  `SQLException`

### reconnect

public void reconnect() throws SQLException
Try to reconnect connection.

Specified by:
  `reconnect` in interface `Listener`
Specified by:
  `reconnect` in class `AbstractMastersListener`
Throws:
  `SQLException` - if reconnect a new connection but there was an active transaction.

### handleFailLoop

public void handleFailLoop()
Add listener to FailoverLoop if master connection is not active, so a reconnection will be done. (the reconnection will be done by failover or if append before by the next query/method that will use the failed connection) Remove listener from FailoverLoop is master connection is active.

Specified by:
  `handleFailLoop` in class `AbstractMastersListener`

### isMasterConnected

public boolean isMasterConnected()

### 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 mustExecuteOnReplica)

### reset

public void reset() throws SQLException
Reset state of master connection.

Throws:
  `SQLException` - if command fail.