Class AbstractQueryProtocol

    • Field Detail

      • logger

        private static final Logger logger
      • LOCK_DEADLOCK_ERROR_CODES

        private static final java.util.Set<java.lang.Integer> LOCK_DEADLOCK_ERROR_CODES
      • readScheduler

        private java.util.concurrent.ThreadPoolExecutor readScheduler
      • transactionIsolationLevel

        private int transactionIsolationLevel
      • localInfileInputStream

        private java.io.InputStream localInfileInputStream
      • maxRows

        private long maxRows
      • statementIdToRelease

        private volatile int statementIdToRelease
      • activeFutureTask

        private java.util.concurrent.FutureTask activeFutureTask
      • interrupted

        private boolean interrupted
    • Constructor Detail

      • AbstractQueryProtocol

        AbstractQueryProtocol​(UrlParser urlParser,
                              GlobalStateInfo globalInfo,
                              java.util.concurrent.locks.ReentrantLock lock,
                              LruTraceCache traceCache)
        Get a protocol instance.
        Parameters:
        urlParser - connection URL information's
        lock - the lock for thread synchronisation
        traceCache - trace cache
    • Method Detail

      • reset

        public void reset()
                   throws java.sql.SQLException
        Reset connection state.
        1. Transaction will be rollback
        2. transaction isolation will be reset
        3. user variables will be removed
        4. sessions variables will be reset to global values
        Specified by:
        reset in interface Protocol
        Throws:
        java.sql.SQLException - if command failed
      • exceptionWithQuery

        private MariaDbSqlException exceptionWithQuery​(java.lang.String sql,
                                                       java.sql.SQLException sqlException,
                                                       boolean explicitClosed)
      • executeQuery

        public void executeQuery​(java.lang.String sql)
                          throws java.sql.SQLException
        Execute internal query.

        !! will not support multi values queries !!

        Specified by:
        executeQuery in interface Protocol
        Specified by:
        executeQuery in class AbstractConnectProtocol
        Parameters:
        sql - sql
        Throws:
        java.sql.SQLException - in any exception occur
      • executeQuery

        public void executeQuery​(boolean mustExecuteOnMaster,
                                 Results results,
                                 java.lang.String sql)
                          throws java.sql.SQLException
        Execute query directly to outputStream.
        Specified by:
        executeQuery in interface Protocol
        Parameters:
        mustExecuteOnMaster - was intended to be launched on master connection
        results - result
        sql - the query to executeInternal
        Throws:
        java.sql.SQLException - exception
      • executeQuery

        public void executeQuery​(boolean mustExecuteOnMaster,
                                 Results results,
                                 java.lang.String sql,
                                 java.nio.charset.Charset charset)
                          throws java.sql.SQLException
        Specified by:
        executeQuery in interface Protocol
        Throws:
        java.sql.SQLException
      • executeQuery

        public void executeQuery​(boolean mustExecuteOnMaster,
                                 Results results,
                                 ClientPrepareResult clientPrepareResult,
                                 ParameterHolder[] parameters)
                          throws java.sql.SQLException
        Execute a unique clientPrepareQuery.
        Specified by:
        executeQuery in interface Protocol
        Parameters:
        mustExecuteOnMaster - was intended to be launched on master connection
        results - results
        clientPrepareResult - clientPrepareResult
        parameters - parameters
        Throws:
        java.sql.SQLException - exception
      • executeQuery

        public void executeQuery​(boolean mustExecuteOnMaster,
                                 Results results,
                                 ClientPrepareResult clientPrepareResult,
                                 ParameterHolder[] parameters,
                                 int queryTimeout)
                          throws java.sql.SQLException
        Execute a unique clientPrepareQuery.
        Specified by:
        executeQuery in interface Protocol
        Parameters:
        mustExecuteOnMaster - was intended to be launched on master connection
        results - results
        clientPrepareResult - clientPrepareResult
        parameters - parameters
        queryTimeout - if timeout is set and must use max_statement_time
        Throws:
        java.sql.SQLException - exception
      • executeBatchClient

        public boolean executeBatchClient​(boolean mustExecuteOnMaster,
                                          Results results,
                                          ClientPrepareResult prepareResult,
                                          java.util.List<ParameterHolder[]> parametersList)
                                   throws java.sql.SQLException
        Execute clientPrepareQuery batch.
        Specified by:
        executeBatchClient in interface Protocol
        Parameters:
        mustExecuteOnMaster - was intended to be launched on master connection
        results - results
        prepareResult - ClientPrepareResult
        parametersList - List of parameters
        Throws:
        java.sql.SQLException - exception
      • executeBulkBatch

        private boolean executeBulkBatch​(Results results,
                                         java.lang.String sql,
                                         ServerPrepareResult serverPrepareResult,
                                         java.util.List<ParameterHolder[]> parametersList)
                                  throws java.sql.SQLException
        Execute clientPrepareQuery batch.
        Parameters:
        results - results
        sql - sql command
        serverPrepareResult - prepare result if exist
        parametersList - List of parameters
        Returns:
        if executed
        Throws:
        java.sql.SQLException - exception
      • initializeBatchReader

        private void initializeBatchReader()
      • executeBatchMulti

        private void executeBatchMulti​(Results results,
                                       ClientPrepareResult clientPrepareResult,
                                       java.util.List<ParameterHolder[]> parametersList)
                                throws java.sql.SQLException
        Execute clientPrepareQuery batch.
        Parameters:
        results - results
        clientPrepareResult - ClientPrepareResult
        parametersList - List of parameters
        Throws:
        java.sql.SQLException - exception
      • executeBatchStmt

        public void executeBatchStmt​(boolean mustExecuteOnMaster,
                                     Results results,
                                     java.util.List<java.lang.String> queries)
                              throws java.sql.SQLException
        Execute batch from Statement.executeBatch().
        Specified by:
        executeBatchStmt in interface Protocol
        Parameters:
        mustExecuteOnMaster - was intended to be launched on master connection
        results - results
        queries - queries
        Throws:
        java.sql.SQLException - if any exception occur
      • executeBatch

        private void executeBatch​(Results results,
                                  java.util.List<java.lang.String> queries)
                           throws java.sql.SQLException
        Execute list of queries not rewritable.
        Parameters:
        results - result object
        queries - list of queries
        Throws:
        java.sql.SQLException - exception
      • prepare

        public ServerPrepareResult prepare​(java.lang.String sql,
                                           boolean executeOnMaster)
                                    throws java.sql.SQLException
        Prepare query on server side. Will permit to know the parameter number of the query, and permit to send only the data on next results.

        For failover, two additional information are in the result-set object : - current connection : Since server maintain a state of this prepare statement, all query will be executed on this particular connection. - executeOnMaster : state of current connection when creating this prepareStatement (if was on master, will only be executed on master. If was on a replica, can be execute temporary on master, but we keep this flag, so when a replica is connected back to relaunch this query on replica)

        Specified by:
        prepare in interface Protocol
        Parameters:
        sql - the query
        executeOnMaster - state of current connection when creating this prepareStatement
        Returns:
        a ServerPrepareResult object that contain prepare result information.
        Throws:
        java.sql.SQLException - if any error occur on connection.
      • executeBatchAggregateSemiColon

        private void executeBatchAggregateSemiColon​(Results results,
                                                    java.util.List<java.lang.String> queries)
                                             throws java.sql.SQLException
        Execute list of queries. This method is used when using text batch statement and using rewriting (allowMultiQueries || rewriteBatchedStatements). queries will be send to server according to max_allowed_packet size.
        Parameters:
        results - result object
        queries - list of queries
        Throws:
        java.sql.SQLException - exception
      • executeBatchRewrite

        private void executeBatchRewrite​(Results results,
                                         ClientPrepareResult prepareResult,
                                         java.util.List<ParameterHolder[]> parameterList,
                                         boolean rewriteValues)
                                  throws java.sql.SQLException
        Specific execution for batch rewrite that has specific query for memory.
        Parameters:
        results - result
        prepareResult - prepareResult
        parameterList - parameters
        rewriteValues - is rewritable flag
        Throws:
        java.sql.SQLException - exception
      • executeBatchServer

        public boolean executeBatchServer​(boolean mustExecuteOnMaster,
                                          ServerPrepareResult serverPrepareResult,
                                          Results results,
                                          java.lang.String sql,
                                          java.util.List<ParameterHolder[]> parametersList)
                                   throws java.sql.SQLException
        Execute Prepare if needed, and execute COM_STMT_EXECUTE queries in batch.
        Specified by:
        executeBatchServer in interface Protocol
        Parameters:
        mustExecuteOnMaster - must normally be executed on master connection
        serverPrepareResult - prepare result. can be null if not prepared.
        results - execution results
        sql - sql query if needed to be prepared
        parametersList - parameter list
        Returns:
        executed
        Throws:
        java.sql.SQLException - if parameter error or connection error occur.
      • executePreparedQuery

        public void executePreparedQuery​(boolean mustExecuteOnMaster,
                                         ServerPrepareResult serverPrepareResult,
                                         Results results,
                                         ParameterHolder[] parameters)
                                  throws java.sql.SQLException
        Execute a query that is already prepared.
        Specified by:
        executePreparedQuery in interface Protocol
        Parameters:
        mustExecuteOnMaster - must execute on master
        serverPrepareResult - prepare result
        results - execution result
        parameters - parameters
        Throws:
        java.sql.SQLException - exception
      • rollback

        public void rollback()
                      throws java.sql.SQLException
        Rollback transaction.
        Specified by:
        rollback in interface Protocol
        Throws:
        java.sql.SQLException
      • forceReleasePrepareStatement

        public boolean forceReleasePrepareStatement​(int statementId)
                                             throws java.sql.SQLException
        Force release of prepare statement that are not used. This method will be call when adding a new prepare statement in cache, so the packet can be send to server without problem.
        Specified by:
        forceReleasePrepareStatement in interface Protocol
        Parameters:
        statementId - prepared statement Id to remove.
        Returns:
        true if successfully released
        Throws:
        java.sql.SQLException - if connection exception.
      • forceReleaseWaitingPrepareStatement

        public void forceReleaseWaitingPrepareStatement()
                                                 throws java.sql.SQLException
        Force release of prepare statement that are not used. This permit to deallocate a statement that cannot be release due to multi-thread use.
        Specified by:
        forceReleaseWaitingPrepareStatement in interface Protocol
        Throws:
        java.sql.SQLException - if connection occur
      • ping

        public boolean ping()
                     throws java.sql.SQLException
        Specified by:
        ping in interface Protocol
        Throws:
        java.sql.SQLException
      • isValid

        public boolean isValid​(int timeout)
                        throws java.sql.SQLException
        Check that connection is valid. !! careful, timeout is in milliseconds, connection.isValid(timeout) is in seconds !!
        Specified by:
        isValid in interface Protocol
        Parameters:
        timeout - timeout in milliseconds
        Returns:
        true is valid
        Throws:
        java.sql.SQLException - if any error occur
      • getCatalog

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

        public void setCatalog​(java.lang.String database)
                        throws java.sql.SQLException
        Specified by:
        setCatalog in interface Protocol
        Throws:
        java.sql.SQLException
      • resetDatabase

        public void resetDatabase()
                           throws java.sql.SQLException
        Specified by:
        resetDatabase in interface Protocol
        Throws:
        java.sql.SQLException
      • cancelCurrentQuery

        public void cancelCurrentQuery()
                                throws java.sql.SQLException
        Cancels the current query - clones the current protocol and executes a query using the new connection.
        Specified by:
        cancelCurrentQuery in interface Protocol
        Throws:
        java.sql.SQLException - never thrown
      • getAutocommit

        public boolean getAutocommit()
        Get current autocommit status.
        Specified by:
        getAutocommit in interface Protocol
        Returns:
        autocommit status
      • releasePrepareStatement

        public void releasePrepareStatement​(ServerPrepareResult serverPrepareResult)
                                     throws java.sql.SQLException
        Deallocate prepare statement if not used anymore.
        Specified by:
        releasePrepareStatement in interface Protocol
        Parameters:
        serverPrepareResult - allocation result
        Throws:
        java.sql.SQLException - if de-allocation failed.
      • setMaxRows

        public void setMaxRows​(long max)
                        throws java.sql.SQLException
        Specified by:
        setMaxRows in interface Protocol
        Throws:
        java.sql.SQLException
      • setLocalInfileInputStream

        public void setLocalInfileInputStream​(java.io.InputStream inputStream)
        Specified by:
        setLocalInfileInputStream in interface Protocol
      • getTimeout

        public int getTimeout()
        Returns the connection timeout in milliseconds.
        Specified by:
        getTimeout in interface Protocol
        Returns:
        the connection timeout in milliseconds.
      • setTimeout

        public void setTimeout​(int timeout)
                        throws java.net.SocketException
        Sets the connection timeout.
        Specified by:
        setTimeout in interface Protocol
        Parameters:
        timeout - the timeout, in milliseconds
        Throws:
        java.net.SocketException - if there is an error in the underlying protocol, such as a TCP error.
      • setReadonly

        public void setReadonly​(boolean readOnly)
                         throws java.sql.SQLException
        Puts this connection in read-only / read-write mode
        Specified by:
        setReadonly in interface Protocol
        Parameters:
        readOnly - true enables read-only mode; false disables it
        Throws:
        java.sql.SQLException - If socket error.
      • setTransactionIsolation

        public void setTransactionIsolation​(int level)
                                     throws java.sql.SQLException
        Set transaction isolation.
        Specified by:
        setTransactionIsolation in interface Protocol
        Parameters:
        level - transaction level.
        Throws:
        java.sql.SQLException - if transaction level is unknown
      • checkClose

        private void checkClose()
                         throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • getResult

        public void getResult​(Results results)
                       throws java.sql.SQLException
        Specified by:
        getResult in interface Protocol
        Throws:
        java.sql.SQLException
      • readPacket

        private void readPacket​(Results results)
                         throws java.sql.SQLException
        Read server response packet.
        Parameters:
        results - result object
        Throws:
        java.sql.SQLException - if sub-result connection fail
        See Also:
        server response packets
      • readOkPacket

        private void readOkPacket​(Buffer buffer,
                                  Results results)
        Read OK_Packet.
        Parameters:
        buffer - current buffer
        results - result object
        See Also:
        OK_Packet
      • handleStateChange

        private void handleStateChange​(Buffer buf,
                                       Results results)
      • getAutoIncrementIncrement

        public int getAutoIncrementIncrement()
                                      throws java.sql.SQLException
        Get current auto increment increment. *** no lock needed ****
        Specified by:
        getAutoIncrementIncrement in interface Protocol
        Returns:
        auto increment increment.
        Throws:
        java.sql.SQLException - if cannot retrieve auto increment value
      • readErrorPacket

        private java.sql.SQLException readErrorPacket​(Buffer buffer,
                                                      Results results)
        Read ERR_Packet.
        Parameters:
        buffer - current buffer
        results - result object
        Returns:
        SQLException if sub-result connection fail
        See Also:
        ERR_Packet
      • readLocalInfilePacket

        private void readLocalInfilePacket​(Buffer buffer,
                                           Results results)
                                    throws java.sql.SQLException
        Read Local_infile Packet.
        Parameters:
        buffer - current buffer
        results - result object
        Throws:
        java.sql.SQLException - if sub-result connection fail
        See Also:
        local_infile packet
      • readResultSet

        private void readResultSet​(Buffer buffer,
                                   Results results)
                            throws java.sql.SQLException
        Read ResultSet Packet.
        Parameters:
        buffer - current buffer
        results - result object
        Throws:
        java.sql.SQLException - if sub-result connection fail
        See Also:
        resultSet packets
      • prolog

        public void prolog​(long maxRows,
                           boolean hasProxy,
                           MariaDbConnection connection,
                           MariaDbStatement statement)
                    throws java.sql.SQLException
        Preparation before command.
        Specified by:
        prolog in interface Protocol
        Parameters:
        maxRows - query max rows
        hasProxy - has proxy
        connection - current connection
        statement - current statement
        Throws:
        java.sql.SQLException - if any error occur.
      • cmdPrologue

        private void cmdPrologue()
                          throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • resetStateAfterFailover

        public void resetStateAfterFailover​(long maxRows,
                                            int transactionIsolationLevel,
                                            java.lang.String database,
                                            boolean autocommit)
                                     throws java.sql.SQLException
        Set current state after a failover.
        Specified by:
        resetStateAfterFailover in interface Protocol
        Parameters:
        maxRows - current Max rows
        transactionIsolationLevel - current transactionIsolationLevel
        database - current database
        autocommit - current autocommit state
        Throws:
        java.sql.SQLException - if any error occur.
      • handleIoException

        public java.sql.SQLException handleIoException​(java.lang.Exception initialException)
        Handle IoException (reconnect if Exception is due to having send too much data, making server close the connection.

        There is 3 kind of IOException :

        1. MaxAllowedPacketException : without need of reconnect : thrown when driver don't send packet that would have been too big then error is not a CONNECTION_EXCEPTION
        2. packets size is greater than max_allowed_packet (can be checked with writer.isAllowedCmdLength()). Need to reconnect
        3. unknown IO error throw a CONNECTION_EXCEPTION
        Specified by:
        handleIoException in interface Protocol
        Parameters:
        initialException - initial Io error
        Returns:
        the resulting error to return to client.
      • setActiveFutureTask

        public void setActiveFutureTask​(java.util.concurrent.FutureTask activeFutureTask)
        Specified by:
        setActiveFutureTask in interface Protocol
      • interrupt

        public void interrupt()
        Specified by:
        interrupt in interface Protocol
      • stopIfInterrupted

        public void stopIfInterrupted()
                               throws java.sql.SQLTimeoutException
        Throw TimeoutException if timeout has been reached.
        Specified by:
        stopIfInterrupted in interface Protocol
        Throws:
        java.sql.SQLTimeoutException - to indicate timeout exception.