Class FailoverProxy

  • All Implemented Interfaces:
    java.lang.reflect.InvocationHandler

    public class FailoverProxy
    extends java.lang.Object
    implements java.lang.reflect.InvocationHandler
    • Field Detail

      • METHOD_IS_EXPLICIT_CLOSED

        private static final java.lang.String METHOD_IS_EXPLICIT_CLOSED
        See Also:
        Constant Field Values
      • METHOD_GET_OPTIONS

        private static final java.lang.String METHOD_GET_OPTIONS
        See Also:
        Constant Field Values
      • METHOD_GET_URLPARSER

        private static final java.lang.String METHOD_GET_URLPARSER
        See Also:
        Constant Field Values
      • METHOD_GET_PROXY

        private static final java.lang.String METHOD_GET_PROXY
        See Also:
        Constant Field Values
      • METHOD_EXECUTE_QUERY

        private static final java.lang.String METHOD_EXECUTE_QUERY
        See Also:
        Constant Field Values
      • METHOD_SET_READ_ONLY

        private static final java.lang.String METHOD_SET_READ_ONLY
        See Also:
        Constant Field Values
      • METHOD_GET_READ_ONLY

        private static final java.lang.String METHOD_GET_READ_ONLY
        See Also:
        Constant Field Values
      • METHOD_IS_MASTER_CONNECTION

        private static final java.lang.String METHOD_IS_MASTER_CONNECTION
        See Also:
        Constant Field Values
      • METHOD_VERSION_GREATER_OR_EQUAL

        private static final java.lang.String METHOD_VERSION_GREATER_OR_EQUAL
        See Also:
        Constant Field Values
      • METHOD_SESSION_STATE_AWARE

        private static final java.lang.String METHOD_SESSION_STATE_AWARE
        See Also:
        Constant Field Values
      • METHOD_CLOSED_EXPLICIT

        private static final java.lang.String METHOD_CLOSED_EXPLICIT
        See Also:
        Constant Field Values
      • METHOD_IS_CLOSED

        private static final java.lang.String METHOD_IS_CLOSED
        See Also:
        Constant Field Values
      • METHOD_EXECUTE_PREPARED_QUERY

        private static final java.lang.String METHOD_EXECUTE_PREPARED_QUERY
        See Also:
        Constant Field Values
      • METHOD_COM_MULTI_PREPARE_EXECUTES

        private static final java.lang.String METHOD_COM_MULTI_PREPARE_EXECUTES
        See Also:
        Constant Field Values
      • METHOD_PROLOG_PROXY

        private static final java.lang.String METHOD_PROLOG_PROXY
        See Also:
        Constant Field Values
      • METHOD_GET_NO_BACKSLASH

        private static final java.lang.String METHOD_GET_NO_BACKSLASH
        See Also:
        Constant Field Values
      • METHOD_GET_SERVER_THREAD_ID

        private static final java.lang.String METHOD_GET_SERVER_THREAD_ID
        See Also:
        Constant Field Values
      • METHOD_GET_CATALOG

        private static final java.lang.String METHOD_GET_CATALOG
        See Also:
        Constant Field Values
      • METHOD_GET_TIMEOUT

        private static final java.lang.String METHOD_GET_TIMEOUT
        See Also:
        Constant Field Values
      • METHOD_GET_MAJOR_VERSION

        private static final java.lang.String METHOD_GET_MAJOR_VERSION
        See Also:
        Constant Field Values
      • METHOD_IN_TRANSACTION

        private static final java.lang.String METHOD_IN_TRANSACTION
        See Also:
        Constant Field Values
      • METHOD_IS_MARIADB

        private static final java.lang.String METHOD_IS_MARIADB
        See Also:
        Constant Field Values
      • logger

        private static final Logger logger
      • lock

        public final java.util.concurrent.locks.ReentrantLock lock
      • listener

        private final Listener listener
    • Constructor Detail

      • FailoverProxy

        public FailoverProxy​(Listener listener,
                             java.util.concurrent.locks.ReentrantLock lock,
                             LruTraceCache traceCache)
                      throws java.sql.SQLException
        Proxy constructor.
        Parameters:
        listener - failover implementation.
        lock - synchronisation lock
        traceCache - trace cache
        Throws:
        java.sql.SQLException - if connection error occur
    • Method Detail

      • addHostInformationToException

        private static java.sql.SQLException addHostInformationToException​(java.sql.SQLException exception,
                                                                           Protocol protocol)
        Add Host information ("on HostAddress...") to exception.

        example : java.sql.SQLException: (conn=603) Cannot execute statement in a READ ONLY transaction.
        Query is: INSERT INTO TableX VALUES (21)
        on HostAddress{host='mydb.example.com', port=3306},master=true

        Parameters:
        exception - current exception
        protocol - protocol to have hostname
      • invoke

        public java.lang.Object invoke​(java.lang.Object proxy,
                                       java.lang.reflect.Method method,
                                       java.lang.Object[] args)
                                throws java.lang.Throwable
        Proxy that catch Protocol call, to permit to catch errors and handle failover when multiple hosts.
        Specified by:
        invoke in interface java.lang.reflect.InvocationHandler
        Parameters:
        proxy - the current protocol
        method - the called method on the protocol
        args - methods parameters
        Returns:
        protocol method result
        Throws:
        java.lang.Throwable - the method throwed error if not catch by failover
      • executeInvocation

        private java.lang.Object executeInvocation​(java.lang.reflect.Method method,
                                                   java.lang.Object[] args,
                                                   boolean isSecondExecution)
                                            throws java.lang.Throwable
        Throws:
        java.lang.Throwable
      • handleFailOver

        private java.lang.Object handleFailOver​(java.sql.SQLException qe,
                                                java.lang.reflect.Method method,
                                                java.lang.Object[] args,
                                                Protocol protocol,
                                                boolean isClosed)
                                         throws java.lang.Throwable
        After a connection exception, launch failover.
        Parameters:
        qe - the exception thrown
        method - the method to call if failover works well
        args - the arguments of the method
        Returns:
        the object return from the method
        Throws:
        java.lang.Throwable - throwable
      • hasToHandleFailover

        public boolean hasToHandleFailover​(java.sql.SQLException exception)
        Check if this Sqlerror is a connection exception. if that's the case, must be handle by failover

        error codes : 08000 : connection exception 08001 : SQL client unable to establish SQL connection 08002 : connection name in use 08003 : connection does not exist 08004 : SQL server rejected SQL connection 08006 : connection failure 08007 : transaction resolution unknown 70100 : connection was killed if error code is "1927"

        Parameters:
        exception - the Exception
        Returns:
        true if there has been a connection error that must be handled by failover
      • reconnect

        public void reconnect()
                       throws java.sql.SQLException
        Launch reconnect implementation.
        Throws:
        java.sql.SQLException - exception
      • getListener

        public Listener getListener()