Class AbstractMastersListener

    • Field Detail

      • blacklist

        private static final java.util.concurrent.ConcurrentMap<HostAddress,​java.lang.Long> blacklist
        List the recent failedConnection.
      • logger

        private static final Logger logger
      • urlParser

        public final UrlParser urlParser
      • currentConnectionAttempts

        protected final java.util.concurrent.atomic.AtomicInteger currentConnectionAttempts
      • explicitClosed

        protected final java.util.concurrent.atomic.AtomicBoolean explicitClosed
      • masterHostFail

        private final java.util.concurrent.atomic.AtomicBoolean masterHostFail
      • currentReadOnlyAsked

        protected volatile boolean currentReadOnlyAsked
      • currentProtocol

        protected Protocol currentProtocol
      • lastRetry

        protected long lastRetry
      • lastQueryNanos

        protected long lastQueryNanos
      • masterHostFailNanos

        private volatile long masterHostFailNanos
    • Constructor Detail

    • Method Detail

      • clearBlacklist

        public static void clearBlacklist()
        Clear blacklist data.
      • initializeConnection

        public void initializeConnection()
                                  throws java.sql.SQLException
        Initialize Listener. This listener will be added to the connection validation loop according to option value so the connection will be verified periodically. (Important for aurora, for other, connection pool often have this functionality)
        Specified by:
        initializeConnection in interface Listener
        Throws:
        java.sql.SQLException - if any exception occur.
      • removeListenerFromSchedulers

        protected void removeListenerFromSchedulers()
      • preAutoReconnect

        protected void preAutoReconnect()
                                 throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • handleFailover

        public HandleErrorResult handleFailover​(java.sql.SQLException qe,
                                                java.lang.reflect.Method method,
                                                java.lang.Object[] args,
                                                Protocol protocol,
                                                boolean isClosed)
                                         throws java.sql.SQLException
        Call when a failover is detected on master connection. Will :
        1. set fail variable
        2. try to reconnect
        3. relaunch query if possible
        Specified by:
        handleFailover in interface Listener
        Parameters:
        method - called method
        args - methods parameters
        protocol - current protocol
        Returns:
        a HandleErrorResult object to indicate if query has been relaunched, and the exception if not
        Throws:
        java.sql.SQLException - when method and parameters does not exist.
      • addToBlacklist

        public void addToBlacklist​(HostAddress hostAddress)
        After a failover, put the hostAddress in a static list so the other connection will not take this host in account for a time.
        Specified by:
        addToBlacklist in interface Listener
        Parameters:
        hostAddress - the HostAddress to add to blacklist
      • removeFromBlacklist

        public void removeFromBlacklist​(HostAddress hostAddress)
        After a successfull connection, permit to remove a hostAddress from blacklist.
        Specified by:
        removeFromBlacklist in interface Listener
        Parameters:
        hostAddress - the host address tho be remove of blacklist
      • resetOldsBlackListHosts

        public void resetOldsBlackListHosts()
        Permit to remove Host to blacklist after loadBalanceBlacklistTimeout seconds.
      • resetMasterFailoverData

        protected void resetMasterFailoverData()
      • setSessionReadOnly

        protected void setSessionReadOnly​(boolean readOnly,
                                          Protocol protocol)
                                   throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • handleFailLoop

        public abstract void handleFailLoop()
      • getMasterHostFailNanos

        public long getMasterHostFailNanos()
      • setMasterHostFail

        public boolean setMasterHostFail()
        Set master fail variables.
        Specified by:
        setMasterHostFail in interface Listener
        Returns:
        true if was already failed
      • relaunchOperation

        public HandleErrorResult relaunchOperation​(java.lang.reflect.Method method,
                                                   java.lang.Object[] args)
                                            throws java.sql.SQLException
        After a failover that has bean done, relaunch the operation that was in progress. In case of special operation that crash server, doesn't relaunched it;
        Parameters:
        method - the method accessed
        args - the parameters
        Returns:
        An object that indicate the result or that the exception as to be thrown
        Throws:
        java.sql.SQLException - if there is any error relaunching initial method
      • isQueryRelaunchable

        public boolean isQueryRelaunchable​(java.lang.reflect.Method method,
                                           java.lang.Object[] args)
        Check if query can be re-executed.
        Parameters:
        method - invoke method
        args - invoke arguments
        Returns:
        true if can be re-executed
      • invoke

        public java.lang.Object invoke​(java.lang.reflect.Method method,
                                       java.lang.Object[] args,
                                       Protocol specificProtocol)
                                throws java.lang.Throwable
        Specified by:
        invoke in interface Listener
        Throws:
        java.lang.Throwable
      • invoke

        public java.lang.Object invoke​(java.lang.reflect.Method method,
                                       java.lang.Object[] args)
                                throws java.lang.Throwable
        Specified by:
        invoke in interface Listener
        Throws:
        java.lang.Throwable
      • syncConnection

        public void syncConnection​(Protocol from,
                                   Protocol to)
                            throws java.sql.SQLException
        When switching between 2 connections, report existing connection parameter to the new used connection.
        Specified by:
        syncConnection in interface Listener
        Parameters:
        from - used connection
        to - will-be-current connection
        Throws:
        java.sql.SQLException - if catalog cannot be set
      • versionGreaterOrEqual

        public boolean versionGreaterOrEqual​(int major,
                                             int minor,
                                             int patch)
        Specified by:
        versionGreaterOrEqual in interface Listener
      • isClosed

        public boolean isClosed()
        Specified by:
        isClosed in interface Listener
      • isValid

        public boolean isValid​(int timeout)
                        throws java.sql.SQLException
        Specified by:
        isValid in interface Listener
        Throws:
        java.sql.SQLException
      • isReadOnly

        public boolean isReadOnly()
        Specified by:
        isReadOnly in interface Listener
      • preExecute

        public abstract void preExecute()
                                 throws java.sql.SQLException
        Specified by:
        preExecute in interface Listener
        Throws:
        java.sql.SQLException
      • preClose

        public abstract void preClose()
        Specified by:
        preClose in interface Listener
      • switchReadOnlyConnection

        public abstract void switchReadOnlyConnection​(java.lang.Boolean readonly)
                                               throws java.sql.SQLException
        Specified by:
        switchReadOnlyConnection in interface Listener
        Throws:
        java.sql.SQLException
      • primaryFail

        public abstract HandleErrorResult primaryFail​(java.lang.reflect.Method method,
                                                      java.lang.Object[] args,
                                                      boolean killCmd,
                                                      boolean isClosed)
                                               throws java.sql.SQLException
        Specified by:
        primaryFail in interface Listener
        Throws:
        java.sql.SQLException
      • throwFailoverMessage

        public void throwFailoverMessage​(HostAddress failHostAddress,
                                         boolean wasMaster,
                                         java.sql.SQLException queryException,
                                         boolean reconnected)
                                  throws java.sql.SQLException
        Throw a human readable message after a failoverException.
        Specified by:
        throwFailoverMessage in interface Listener
        Parameters:
        failHostAddress - failedHostAddress
        wasMaster - was failed connection master
        queryException - internal error
        reconnected - connection status
        Throws:
        java.sql.SQLException - error with failover information
      • getCatalog

        public java.lang.String getCatalog()
                                    throws java.sql.SQLException
        Specified by:
        getCatalog in interface Listener
        Throws:
        java.sql.SQLException
      • getTimeout

        public int getTimeout()
                       throws java.net.SocketException
        Specified by:
        getTimeout in interface Listener
        Throws:
        java.net.SocketException
      • reconnect

        public abstract void reconnect()
                                throws java.sql.SQLException
        Specified by:
        reconnect in interface Listener
        Throws:
        java.sql.SQLException
      • pingMasterProtocol

        protected boolean pingMasterProtocol​(Protocol protocol)
      • closeConnection

        public void closeConnection​(Protocol protocol)
        Utility to close existing connection.
        Parameters:
        protocol - connection to close.
      • abortConnection

        public void abortConnection​(Protocol protocol)
        Utility to force close existing connection.
        Parameters:
        protocol - connection to close.