Class AbstractQueryProtocol
java.lang.Object
org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol
org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol
- All Implemented Interfaces:
Protocol
- Direct Known Subclasses:
MasterProtocol
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate FutureTask
private static final String
private boolean
private InputStream
private static final Logger
private final LogQueryTool
private long
protected static ThreadPoolExecutor
private int
private int
Fields inherited from class org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol
activeStreamingResult, autoIncrementIncrement, connected, database, eofDeprecated, explicitClosed, hasWarnings, lock, options, proxy, reader, readOnly, serverCapabilities, serverPrepareStatementCache, serverStatus, serverThreadId, socket, socketTimeout, urlParser, writer
-
Constructor Summary
ConstructorsConstructorDescriptionAbstractQueryProtocol
(UrlParser urlParser, GlobalStateInfo globalInfo, ReentrantLock lock) Get a protocol instance. -
Method Summary
Modifier and TypeMethodDescriptionaddPrepareInCache
(String key, ServerPrepareResult serverPrepareResult) void
Cancels the current query - clones the current protocol and executes a query using the new connection.private void
void
private void
private void
executeBatch
(Results results, List<String> queries) Execute list of queries not rewritable.private void
executeBatchAggregateSemiColon
(Results results, List<String> queries) Execute list of queries.boolean
executeBatchClient
(boolean mustExecuteOnMaster, Results results, ClientPrepareResult prepareResult, List<ParameterHolder[]> parametersList, boolean hasLongData) Execute clientPrepareQuery batch.private void
executeBatchMulti
(Results results, ClientPrepareResult clientPrepareResult, List<ParameterHolder[]> parametersList) Execute clientPrepareQuery batch.private void
executeBatchRewrite
(Results results, ClientPrepareResult prepareResult, List<ParameterHolder[]> parameterList, boolean rewriteValues) Specific execution for batch rewrite that has specific query for memory.boolean
executeBatchServer
(boolean mustExecuteOnMaster, ServerPrepareResult serverPrepareResult, Results results, String sql, List<ParameterHolder[]> parametersList, boolean hasLongData) Execute Prepare if needed, and execute COM_STMT_EXECUTE queries in batch.void
executeBatchStmt
(boolean mustExecuteOnMaster, Results results, List<String> queries) Execute batch from Statement.executeBatch().private boolean
executeBulkBatch
(Results results, String sql, ServerPrepareResult serverPrepareResult, List<ParameterHolder[]> parametersList) Execute clientPrepareQuery batch.void
executePreparedQuery
(boolean mustExecuteOnMaster, ServerPrepareResult serverPrepareResult, Results results, ParameterHolder[] parameters) Execute a query that is already prepared.void
executeQuery
(boolean mustExecuteOnMaster, Results results, String sql) Execute query directly to outputStream.void
executeQuery
(boolean mustExecuteOnMaster, Results results, String sql, Charset charset) void
executeQuery
(boolean mustExecuteOnMaster, Results results, ClientPrepareResult clientPrepareResult, ParameterHolder[] parameters) Execute a unique clientPrepareQuery.void
executeQuery
(boolean mustExecuteOnMaster, Results results, ClientPrepareResult clientPrepareResult, ParameterHolder[] parameters, int queryTimeout) Execute a unique clientPrepareQuery.void
executeQuery
(String sql) Execute internal query.boolean
forceReleasePrepareStatement
(int statementId) Force release of prepare statement that are not used.void
Force release of prepare statement that are not used.boolean
Get current autocommit status.int
Get current auto increment increment.long
void
int
Returns the connection timeout in milliseconds.int
handleIoException
(Exception initialException) Handle IoException (reconnect if Exception is due to having send too much data, making server close the connection.private void
handleStateChange
(Buffer buf, Results results) private void
void
boolean
boolean
boolean
isValid
(int timeout) Check that connection is valid.boolean
ping()
Prepare query on server side.void
prolog
(long maxRows, boolean hasProxy, MariaDbConnection connection, MariaDbStatement statement) Preparation before command.void
prologProxy
(ServerPrepareResult serverPrepareResult, long maxRows, boolean hasProxy, MariaDbConnection connection, MariaDbStatement statement) private SQLException
readErrorPacket
(Buffer buffer, Results results) Read ERR_Packet.private void
readLocalInfilePacket
(Buffer buffer, Results results) Read Local_infile Packet.private void
readOkPacket
(Buffer buffer, Results results) Read OK_Packet.private void
readPacket
(Results results) Read server response packet.private void
readResultSet
(Buffer buffer, Results results) Read ResultSet Packet.void
releasePrepareStatement
(ServerPrepareResult serverPrepareResult) Deallocate prepare statement if not used anymore.void
reset()
Reset connection state.void
void
resetStateAfterFailover
(long maxRows, int transactionIsolationLevel, String database, boolean autocommit) Set current state after a failover.void
rollback()
Rollback transaction.void
setActiveFutureTask
(FutureTask activeFutureTask) void
setCatalog
(String database) void
setLocalInfileInputStream
(InputStream inputStream) void
setMaxRows
(long max) void
setTimeout
(int timeout) Sets the connection timeout.void
setTransactionIsolation
(int level) Set transaction isolation.void
Throw TimeoutException if timeout has been reached.Methods inherited from class org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol
abort, changeSocketSoTimeout, changeSocketTcpNoDelay, checkIfMaster, close, connect, connectWithoutProxy, getActiveStreamingResult, getDatabase, getHost, getHostAddress, getLock, getMajorServerVersion, getMinorServerVersion, getOptions, getPinGlobalTxToPhysicalConnection, getPort, getProxy, getReader, getReadonly, getServerThreadId, getServerVersion, getSocket, getTimeZone, getTraces, getUrlParser, getUsername, getWriter, hasMoreResults, hasWarnings, isClosed, isConnected, isEofDeprecated, isExplicitClosed, isMasterConnection, isServerMariaDb, mustBeMasterConnection, noBackslashEscapes, prepareStatementCache, readEofPacket, readPipelineCheckMaster, removeActiveStreamingResult, removeHasMoreResults, sessionStateAware, setActiveStreamingResult, setHasWarnings, setHostAddress, setHostFailedWithoutProxy, setProxy, setReadonly, setServerStatus, shouldReconnectWithoutProxy, skip, skipEofPacket, versionGreaterOrEqual
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.mariadb.jdbc.internal.protocol.Protocol
abort, changeSocketSoTimeout, changeSocketTcpNoDelay, checkIfMaster, close, connect, connectWithoutProxy, getActiveStreamingResult, getDatabase, getHost, getHostAddress, getLock, getMajorServerVersion, getMinorServerVersion, getOptions, getPinGlobalTxToPhysicalConnection, getPort, getProxy, getReader, getReadonly, getServerThreadId, getServerVersion, getSocket, getTimeZone, getTraces, getUrlParser, getUsername, getWriter, hasMoreResults, hasWarnings, isClosed, isConnected, isEofDeprecated, isExplicitClosed, isMasterConnection, isServerMariaDb, mustBeMasterConnection, noBackslashEscapes, prepareStatementCache, readEofPacket, removeActiveStreamingResult, removeHasMoreResults, sessionStateAware, setActiveStreamingResult, setHasWarnings, setHostAddress, setHostFailedWithoutProxy, setProxy, setReadonly, setServerStatus, shouldReconnectWithoutProxy, skip, skipEofPacket, versionGreaterOrEqual
-
Field Details
-
logger
-
CHECK_GALERA_STATE_QUERY
- See Also:
-
readScheduler
-
logQuery
-
transactionIsolationLevel
private int transactionIsolationLevel -
localInfileInputStream
-
maxRows
private long maxRows -
statementIdToRelease
private volatile int statementIdToRelease -
activeFutureTask
-
interrupted
private boolean interrupted -
galeraAllowedStates
-
-
Constructor Details
-
AbstractQueryProtocol
AbstractQueryProtocol(UrlParser urlParser, GlobalStateInfo globalInfo, ReentrantLock lock) Get a protocol instance.- Parameters:
urlParser
- connection URL information'slock
- the lock for thread synchronisation
-
-
Method Details
-
reset
Reset connection state.- Transaction will be rollback
- transaction isolation will be reset
- user variables will be removed
- sessions variables will be reset to global values
- Specified by:
reset
in interfaceProtocol
- Throws:
SQLException
- if command failed
-
executeQuery
Execute internal query.!! will not support multi values queries !!
- Specified by:
executeQuery
in interfaceProtocol
- Specified by:
executeQuery
in classAbstractConnectProtocol
- Parameters:
sql
- sql- Throws:
SQLException
- in any exception occur
-
executeQuery
public void executeQuery(boolean mustExecuteOnMaster, Results results, String sql) throws SQLException Execute query directly to outputStream.- Specified by:
executeQuery
in interfaceProtocol
- Parameters:
mustExecuteOnMaster
- was intended to be launched on master connectionresults
- resultsql
- the query to executeInternal- Throws:
SQLException
- exception
-
executeQuery
public void executeQuery(boolean mustExecuteOnMaster, Results results, String sql, Charset charset) throws SQLException - Specified by:
executeQuery
in interfaceProtocol
- Throws:
SQLException
-
executeQuery
public void executeQuery(boolean mustExecuteOnMaster, Results results, ClientPrepareResult clientPrepareResult, ParameterHolder[] parameters) throws SQLException Execute a unique clientPrepareQuery.- Specified by:
executeQuery
in interfaceProtocol
- Parameters:
mustExecuteOnMaster
- was intended to be launched on master connectionresults
- resultsclientPrepareResult
- clientPrepareResultparameters
- parameters- Throws:
SQLException
- exception
-
executeQuery
public void executeQuery(boolean mustExecuteOnMaster, Results results, ClientPrepareResult clientPrepareResult, ParameterHolder[] parameters, int queryTimeout) throws SQLException Execute a unique clientPrepareQuery.- Specified by:
executeQuery
in interfaceProtocol
- Parameters:
mustExecuteOnMaster
- was intended to be launched on master connectionresults
- resultsclientPrepareResult
- clientPrepareResultparameters
- parametersqueryTimeout
- if timeout is set and must use max_statement_time- Throws:
SQLException
- exception
-
executeBatchClient
public boolean executeBatchClient(boolean mustExecuteOnMaster, Results results, ClientPrepareResult prepareResult, List<ParameterHolder[]> parametersList, boolean hasLongData) throws SQLException Execute clientPrepareQuery batch.- Specified by:
executeBatchClient
in interfaceProtocol
- Parameters:
mustExecuteOnMaster
- was intended to be launched on master connectionresults
- resultsprepareResult
- ClientPrepareResultparametersList
- List of parametershasLongData
- has parameter with long data (stream)- Throws:
SQLException
- exception
-
executeBulkBatch
private boolean executeBulkBatch(Results results, String sql, ServerPrepareResult serverPrepareResult, List<ParameterHolder[]> parametersList) throws SQLException Execute clientPrepareQuery batch.- Parameters:
results
- resultssql
- sql commandserverPrepareResult
- prepare result if existparametersList
- List of parameters- Returns:
- if executed
- Throws:
SQLException
- exception
-
initializeBatchReader
private void initializeBatchReader() -
executeBatchMulti
private void executeBatchMulti(Results results, ClientPrepareResult clientPrepareResult, List<ParameterHolder[]> parametersList) throws SQLException Execute clientPrepareQuery batch.- Parameters:
results
- resultsclientPrepareResult
- ClientPrepareResultparametersList
- List of parameters- Throws:
SQLException
- exception
-
executeBatchStmt
public void executeBatchStmt(boolean mustExecuteOnMaster, Results results, List<String> queries) throws SQLException Execute batch from Statement.executeBatch().- Specified by:
executeBatchStmt
in interfaceProtocol
- Parameters:
mustExecuteOnMaster
- was intended to be launched on master connectionresults
- resultsqueries
- queries- Throws:
SQLException
- if any exception occur
-
executeBatch
Execute list of queries not rewritable.- Parameters:
results
- result objectqueries
- list of queries- Throws:
SQLException
- exception
-
prepare
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 slave, can be execute temporary on master, but we keep this flag, so when a slave is connected back to relaunch this query on slave)
- Specified by:
prepare
in interfaceProtocol
- Parameters:
sql
- the queryexecuteOnMaster
- state of current connection when creating this prepareStatement- Returns:
- a ServerPrepareResult object that contain prepare result information.
- Throws:
SQLException
- if any error occur on connection.
-
executeBatchAggregateSemiColon
private void executeBatchAggregateSemiColon(Results results, List<String> queries) throws 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 objectqueries
- list of queries- Throws:
SQLException
- exception
-
executeBatchRewrite
private void executeBatchRewrite(Results results, ClientPrepareResult prepareResult, List<ParameterHolder[]> parameterList, boolean rewriteValues) throws SQLException Specific execution for batch rewrite that has specific query for memory.- Parameters:
results
- resultprepareResult
- prepareResultparameterList
- parametersrewriteValues
- is rewritable flag- Throws:
SQLException
- exception
-
executeBatchServer
public boolean executeBatchServer(boolean mustExecuteOnMaster, ServerPrepareResult serverPrepareResult, Results results, String sql, List<ParameterHolder[]> parametersList, boolean hasLongData) throws SQLException Execute Prepare if needed, and execute COM_STMT_EXECUTE queries in batch.- Specified by:
executeBatchServer
in interfaceProtocol
- Parameters:
mustExecuteOnMaster
- must normally be executed on master connectionserverPrepareResult
- prepare result. can be null if not prepared.results
- execution resultssql
- sql query if needed to be preparedparametersList
- parameter listhasLongData
- has long data (stream)- Returns:
- executed
- Throws:
SQLException
- if parameter error or connection error occur.
-
executePreparedQuery
public void executePreparedQuery(boolean mustExecuteOnMaster, ServerPrepareResult serverPrepareResult, Results results, ParameterHolder[] parameters) throws SQLException Execute a query that is already prepared.- Specified by:
executePreparedQuery
in interfaceProtocol
- Parameters:
mustExecuteOnMaster
- must execute on masterserverPrepareResult
- prepare resultresults
- execution resultparameters
- parameters- Throws:
SQLException
- exception
-
rollback
Rollback transaction.- Specified by:
rollback
in interfaceProtocol
- Throws:
SQLException
-
forceReleasePrepareStatement
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 interfaceProtocol
- Parameters:
statementId
- prepared statement Id to remove.- Returns:
- true if successfully released
- Throws:
SQLException
- if connection exception.
-
forceReleaseWaitingPrepareStatement
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 interfaceProtocol
- Throws:
SQLException
- if connection occur
-
ping
- Specified by:
ping
in interfaceProtocol
- Throws:
SQLException
-
isValid
Check that connection is valid. !! careful, timeout is in milliseconds, connection.isValid(timeout) is in seconds !!- Specified by:
isValid
in interfaceProtocol
- Parameters:
timeout
- timeout in milliseconds- Returns:
- true is valid
- Throws:
SQLException
- if any error occur
-
getCatalog
- Specified by:
getCatalog
in interfaceProtocol
- Throws:
SQLException
-
setCatalog
- Specified by:
setCatalog
in interfaceProtocol
- Throws:
SQLException
-
resetDatabase
- Specified by:
resetDatabase
in interfaceProtocol
- Throws:
SQLException
-
cancelCurrentQuery
Cancels the current query - clones the current protocol and executes a query using the new connection.- Specified by:
cancelCurrentQuery
in interfaceProtocol
- Throws:
SQLException
- never thrown
-
getAutocommit
public boolean getAutocommit()Get current autocommit status.- Specified by:
getAutocommit
in interfaceProtocol
- Returns:
- autocommit status
-
inTransaction
public boolean inTransaction()- Specified by:
inTransaction
in interfaceProtocol
-
closeExplicit
public void closeExplicit()- Specified by:
closeExplicit
in interfaceProtocol
-
releasePrepareStatement
Deallocate prepare statement if not used anymore.- Specified by:
releasePrepareStatement
in interfaceProtocol
- Parameters:
serverPrepareResult
- allocation result- Throws:
SQLException
- if de-allocation failed.
-
getMaxRows
public long getMaxRows()- Specified by:
getMaxRows
in interfaceProtocol
-
setMaxRows
- Specified by:
setMaxRows
in interfaceProtocol
- Throws:
SQLException
-
setLocalInfileInputStream
- Specified by:
setLocalInfileInputStream
in interfaceProtocol
-
getTimeout
public int getTimeout()Returns the connection timeout in milliseconds.- Specified by:
getTimeout
in interfaceProtocol
- Returns:
- the connection timeout in milliseconds.
-
setTimeout
Sets the connection timeout.- Specified by:
setTimeout
in interfaceProtocol
- Parameters:
timeout
- the timeout, in milliseconds- Throws:
SocketException
- if there is an error in the underlying protocol, such as a TCP error.
-
setTransactionIsolation
Set transaction isolation.- Specified by:
setTransactionIsolation
in interfaceProtocol
- Parameters:
level
- transaction level.- Throws:
SQLException
- if transaction level is unknown
-
getTransactionIsolationLevel
public int getTransactionIsolationLevel()- Specified by:
getTransactionIsolationLevel
in interfaceProtocol
-
checkClose
- Throws:
SQLException
-
getResult
- Specified by:
getResult
in interfaceProtocol
- Throws:
SQLException
-
readPacket
Read server response packet.- Parameters:
results
- result object- Throws:
SQLException
- if sub-result connection fail- See Also:
-
readOkPacket
Read OK_Packet.- Parameters:
buffer
- current bufferresults
- result object- See Also:
-
handleStateChange
-
getAutoIncrementIncrement
Get current auto increment increment. *** no lock needed ****- Specified by:
getAutoIncrementIncrement
in interfaceProtocol
- Returns:
- auto increment increment.
- Throws:
SQLException
- if cannot retrieve auto increment value
-
readErrorPacket
Read ERR_Packet.- Parameters:
buffer
- current bufferresults
- result object- Returns:
- SQLException if sub-result connection fail
- See Also:
-
readLocalInfilePacket
Read Local_infile Packet.- Parameters:
buffer
- current bufferresults
- result object- Throws:
SQLException
- if sub-result connection fail- See Also:
-
readResultSet
Read ResultSet Packet.- Parameters:
buffer
- current bufferresults
- result object- Throws:
SQLException
- if sub-result connection fail- See Also:
-
prologProxy
public void prologProxy(ServerPrepareResult serverPrepareResult, long maxRows, boolean hasProxy, MariaDbConnection connection, MariaDbStatement statement) throws SQLException - Specified by:
prologProxy
in interfaceProtocol
- Throws:
SQLException
-
prolog
public void prolog(long maxRows, boolean hasProxy, MariaDbConnection connection, MariaDbStatement statement) throws SQLException Preparation before command.- Specified by:
prolog
in interfaceProtocol
- Parameters:
maxRows
- query max rowshasProxy
- has proxyconnection
- current connectionstatement
- current statement- Throws:
SQLException
- if any error occur.
-
addPrepareInCache
- Specified by:
addPrepareInCache
in interfaceProtocol
-
cmdPrologue
- Throws:
SQLException
-
resetStateAfterFailover
public void resetStateAfterFailover(long maxRows, int transactionIsolationLevel, String database, boolean autocommit) throws SQLException Set current state after a failover.- Specified by:
resetStateAfterFailover
in interfaceProtocol
- Parameters:
maxRows
- current Max rowstransactionIsolationLevel
- current transactionIsolationLeveldatabase
- current databaseautocommit
- current autocommit state- Throws:
SQLException
- if any error occur.
-
handleIoException
Handle IoException (reconnect if Exception is due to having send too much data, making server close the connection.There is 3 kind of IOException :
- 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
- packets size is greater than max_allowed_packet (can be checked with writer.isAllowedCmdLength()). Need to reconnect
- unknown IO error throw a CONNECTION_EXCEPTION
- Specified by:
handleIoException
in interfaceProtocol
- Parameters:
initialException
- initial Io error- Returns:
- the resulting error to return to client.
-
setActiveFutureTask
- Specified by:
setActiveFutureTask
in interfaceProtocol
-
interrupt
public void interrupt() -
isInterrupted
public boolean isInterrupted()- Specified by:
isInterrupted
in interfaceProtocol
-
stopIfInterrupted
Throw TimeoutException if timeout has been reached.- Specified by:
stopIfInterrupted
in interfaceProtocol
- Throws:
SQLTimeoutException
- to indicate timeout exception.
-