Class StandardClient
java.lang.Object
org.mariadb.jdbc.client.impl.StandardClient
- All Implemented Interfaces:
AutoCloseable, Client
- Direct Known Subclasses:
ReplayClient
Connection client
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate AuthenticationPluginprivate byte[]private booleanprivate final MutableByteprivate Configurationprivate final longprotected Contextconnection contextprivate final booleanprotected final ExceptionFactoryconnection exception factoryprivate HostAddressprivate final ClosableLockprivate static final Loggerprivate static final intmaximum number of authentication switch requests a server may send during connectionprivate Readerprivate static final Patternprivate final MutableByteprivate Socketconnection socket: the TLS socket once TLS is negotiated, the transport socket beforeprivate intprivate ClientMessageprivate Statementprivate booleanwhether the transport is a unix domain socket (a MitM-proof connection method)protected Writerpacket writer -
Constructor Summary
ConstructorsConstructorDescriptionStandardClient(Configuration conf, HostAddress hostAddress, ClosableLock lock, boolean skipPostCommands) Constructor -
Method Summary
Modifier and TypeMethodDescriptionvoidAbort current connectionprivate voidaddAutoCommitCommand(Context context, List<String> commands) private voidaddCharsetCommand(Context context, List<String> commands) private voidaddCustomSessionVariables(List<String> commands) private voidaddFixedOffsetTimeZone(ZoneId connectionZoneId, List<String> commands) private voidaddReadOnlyCommand(Context context, List<String> commands) private voidaddSessionTrackingCommand(Context context, List<String> commands) private voidaddTimeZoneCommand(Context context, List<String> commands) private voidaddTransactionIsolationCommand(Context context, List<String> commands) private voidaddTruncationCommand(List<String> commands) private voidassignStream(OutputStream out, InputStream in, Configuration conf, Long threadId) voidauthenticationHandler(Credential credential, HostAddress hostAddress) private StringbuildFinalQuery(List<String> commands) protected voidThrow an exception if client is closedvoidclose()Close clientvoidclosePrepare(Prepare prepare) Close prepare commandprivate voidprivate voidconfigureSslSocket(SSLSocket sslSocket, Configuration conf) private void(package private) static booleanconnectDeadlineReached(long deadlineNs, int connectTimeout) Indicate if the connection phase has exhausted the connectTimeout budget.createSessionVariableQuery(Context context) Creates a query string for setting session variables based on context and configuration.private SSLSocketcreateSslSocket(Configuration conf, TlsSocketPlugin socketPlugin, TrustManager[] trustManagers) protected voidClosing socket in case of Connection error after socket creation.private StringdetermineAuthType(InitialHandshakePacket handshake) private SslModeexecute(ClientMessage message, boolean canRedo) Send client message and read resultexecute(ClientMessage message, Statement stmt, boolean canRedo) Send client message and read resultexecute(ClientMessage message, Statement stmt, int fetchSize, long maxRows, int resultSetConcurrency, int resultSetType, boolean closeOnCompletion, boolean canRedo) Send client message and read resultexecutePipeline(ClientMessage[] messages, Statement stmt, int fetchSize, long maxRows, int resultSetConcurrency, int resultSetType, boolean closeOnCompletion, boolean canRedo) Send client messages pipelining and read resultGet connection contextGet connection exception factoryGet connection hostGet current socket IP or null (for Pipe / unix socket)intget socket timeoutprivate voidhandleAuthentication(InitialHandshakePacket handshake, long clientCapabilities) private voidprivate voidprivate InitialHandshakePacketprivate SSLSockethandleSSLConnection(InitialHandshakePacket handshake, long clientCapabilities) private voidhandleSslHandshake(SSLSocket sslSocket, TrustManager[] trustManagers) private voidprivate voidload server timezone and ensure this corresponds to client timezonestatic StringhidePassword(String redirectUrl) Mask the password of a redirection url (scheme://user:password@host...) so the credentials a server embeds inredirect_urlare not written to the logs.private voidinitializeContext(InitialHandshakePacket handshake, long clientCapabilities) private booleanisAutoCommitUpdateRequired(Context context, boolean canRelyOnConnectionFlag) booleanisClosed()Is client closedprivate booleanisDefaultCharsetSufficient(Context context) booleanis current client writer or read-onlyprivate booleanisReliableConnectionFlag(Context context) private voidreadPacket(ClientMessage message) Read a MySQL packet from socketreadPacket(Statement stmt, ClientMessage message, int fetchSize, long maxRows, int resultSetConcurrency, int resultSetType, boolean closeOnCompletion) Read server response packet.voidreadResponse(ClientMessage message) Read server responsereadResponse(Statement stmt, ClientMessage message, int fetchSize, long maxRows, int resultSetConcurrency, int resultSetType, boolean closeOnCompletion) Read server responses for a client messageprivate voidreadResults(Statement stmt, ClientMessage message, List<Completion> completions, int fetchSize, long maxRows, int resultSetConcurrency, int resultSetType, boolean closeOnCompletion) voidreadStreamingResults(List<Completion> completions, int fetchSize, long maxRows, int resultSetConcurrency, int resultSetType, boolean closeOnCompletion) Read resultsvoidprivate booleanWithout permitRedirect, redirection is followed only on a fully verified TLS connection.private booleanrequiresHostnameVerification(SslMode sslMode) voidreset()Reset connectionprivate voidsendHandshakeResponse(InitialHandshakePacket handshake, long clientCapabilities, Credential credential, String authType) intsendQuery(ClientMessage message) Send client message to serverprivate voidsendSslRequest(InitialHandshakePacket handshake, long clientCapabilities) voidsetReadOnly(boolean readOnly) Switch to a writer/read-only connection, no effet on mono-connectionvoidsetSocketTimeout(int milliseconds) Set socket timeoutprivate longsetupClientCapabilities(InitialHandshakePacket handshake) private voidsetupCompression(InputStream in, OutputStream out, long clientCapabilities, long threadId) private voidsetupConnection(boolean skipPostCommands) private voidprivate voidupdateThreadIds(InitialHandshakePacket handshake) private static booleanvalidateFingerPrint(AuthenticationPlugin authPlugin, byte[] validationHash, byte[] fingerPrint, Credential credential, byte[] seed) private voidprivate voidverifyHostname(SSLSocket sslSocket, TlsSocketPlugin socketPlugin)
-
Field Details
-
logger
-
MAX_AUTHENTICATION_SWITCH
private static final int MAX_AUTHENTICATION_SWITCHmaximum number of authentication switch requests a server may send during connection- See Also:
-
exceptionFactory
connection exception factory -
REDIRECT_PATTERN
-
socket
connection socket: the TLS socket once TLS is negotiated, the transport socket before -
unixSocketTransport
private boolean unixSocketTransportwhether the transport is a unix domain socket (a MitM-proof connection method) -
sequence
-
compressionSequence
-
lock
-
conf
-
authPlugin
-
hostAddress
-
disablePipeline
private final boolean disablePipeline -
context
connection context -
writer
packet writer -
closed
private boolean closed -
reader
-
certFingerprint
private byte[] certFingerprint -
streamStmt
-
streamMsg
-
socketTimeout
private int socketTimeout -
connectDeadlineNs
private final long connectDeadlineNs -
redirectConsumer
-
-
Constructor Details
-
StandardClient
public StandardClient(Configuration conf, HostAddress hostAddress, ClosableLock lock, boolean skipPostCommands) throws SQLException Constructor- Parameters:
conf- configurationhostAddress- hostlock- thread lockerskipPostCommands- must connection post command be skipped- Throws:
SQLException- if connection fails
-
-
Method Details
-
setupConnection
- Throws:
SQLExceptionIOException
-
setupCompression
private void setupCompression(InputStream in, OutputStream out, long clientCapabilities, long threadId) -
handleSSLConnection
private SSLSocket handleSSLConnection(InitialHandshakePacket handshake, long clientCapabilities) throws SQLException, IOException - Throws:
SQLExceptionIOException
-
updateThreadIds
-
determineSslMode
-
validateServerSslCapability
- Throws:
SQLException
-
sendSslRequest
private void sendSslRequest(InitialHandshakePacket handshake, long clientCapabilities) throws IOException - Throws:
IOException
-
createSslSocket
private SSLSocket createSslSocket(Configuration conf, TlsSocketPlugin socketPlugin, TrustManager[] trustManagers) throws SQLException, IOException - Throws:
SQLExceptionIOException
-
configureSslSocket
- Throws:
SQLException
-
handleSslHandshake
private void handleSslHandshake(SSLSocket sslSocket, TrustManager[] trustManagers) throws IOException - Throws:
IOException
-
requiresHostnameVerification
-
verifyHostname
- Throws:
SQLException
-
configureTimeout
- Throws:
SQLException
-
handleServerHandshake
- Throws:
SQLExceptionIOException
-
throwHandshakeError
- Throws:
SQLException
-
setupClientCapabilities
-
initializeContext
-
handleAuthentication
private void handleAuthentication(InitialHandshakePacket handshake, long clientCapabilities) throws IOException, SQLException - Throws:
IOExceptionSQLException
-
determineAuthType
-
handleConnectionError
- Throws:
SQLException
-
handleTimeoutError
- Throws:
SQLTimeoutException
-
handleIOError
- Throws:
SQLException
-
sendHandshakeResponse
private void sendHandshakeResponse(InitialHandshakePacket handshake, long clientCapabilities, Credential credential, String authType) throws IOException - Throws:
IOException
-
connectDeadlineReached
static boolean connectDeadlineReached(long deadlineNs, int connectTimeout) Indicate if the connection phase has exhausted the connectTimeout budget. connectTimeout is applied as a socket timeout during connection, so it bounds each individual read, but not their number: without this check a malicious server can keep a client in the authentication phase forever by sending an endless stream of authentication switch requests.- Parameters:
deadlineNs- monotonic deadline (System.nanoTime()based)connectTimeout- configured connect timeout, in milliseconds, zero meaning no timeout- Returns:
- true if connectTimeout is set and the deadline is reached
-
authenticationHandler
public void authenticationHandler(Credential credential, HostAddress hostAddress) throws IOException, SQLException - Parameters:
credential- credentialhostAddress- host address- Throws:
IOException- if any socket error occursSQLException- if any other kind of issue occurs
-
validateFingerPrint
private static boolean validateFingerPrint(AuthenticationPlugin authPlugin, byte[] validationHash, byte[] fingerPrint, Credential credential, byte[] seed) -
redirectByDefault
private boolean redirectByDefault()Without permitRedirect, redirection is followed only on a fully verified TLS connection. -
redirect
-
hidePassword
Mask the password of a redirection url (scheme://user:password@host...) so the credentials a server embeds inredirect_urlare not written to the logs.- Parameters:
redirectUrl- redirection url- Returns:
- url with the password component replaced by
***
-
assignStream
-
destroySocket
protected void destroySocket()Closing socket in case of Connection error after socket creation. -
handleTimezone
load server timezone and ensure this corresponds to client timezone- Throws:
SQLException- if any socket error.
-
postConnectionQueries
- Throws:
SQLException
-
createSessionVariableQuery
-
addAutoCommitCommand
-
isReliableConnectionFlag
-
isAutoCommitUpdateRequired
-
addTruncationCommand
-
addSessionTrackingCommand
-
addTimeZoneCommand
-
addFixedOffsetTimeZone
-
addTransactionIsolationCommand
-
addReadOnlyCommand
-
addCharsetCommand
-
isDefaultCharsetSufficient
-
addCustomSessionVariables
-
buildFinalQuery
-
setReadOnly
Description copied from interface:ClientSwitch to a writer/read-only connection, no effet on mono-connection- Specified by:
setReadOnlyin interfaceClient- Parameters:
readOnly- must use read-only connection- Throws:
SQLException- if any error occurs
-
sendQuery
Send client message to server- Parameters:
message- client message- Returns:
- number of command send
- Throws:
SQLException- if socket error occurs
-
execute
Description copied from interface:ClientSend client message and read result- Specified by:
executein interfaceClient- Parameters:
message- client messagecanRedo- can client message be redone in case of failover- Returns:
- results
- Throws:
SQLException- if execution fails
-
execute
public List<Completion> execute(ClientMessage message, Statement stmt, boolean canRedo) throws SQLException Description copied from interface:ClientSend client message and read result- Specified by:
executein interfaceClient- Parameters:
message- client messagestmt- statementcanRedo- can client message be redone in case of failover- Returns:
- results
- Throws:
SQLException- if execution fails
-
executePipeline
public List<Completion> executePipeline(ClientMessage[] messages, Statement stmt, int fetchSize, long maxRows, int resultSetConcurrency, int resultSetType, boolean closeOnCompletion, boolean canRedo) throws SQLException Description copied from interface:ClientSend client messages pipelining and read result- Specified by:
executePipelinein interfaceClient- Parameters:
messages- client messagestmt- statementfetchSize- fetch sizemaxRows- maximum number of rows. 0 = allresultSetConcurrency- concurrencyresultSetType- result-set typecloseOnCompletion- close statement on completioncanRedo- can client message be redone in case of failover- Returns:
- results
- Throws:
SQLException- if any error occurs
-
execute
public List<Completion> execute(ClientMessage message, Statement stmt, int fetchSize, long maxRows, int resultSetConcurrency, int resultSetType, boolean closeOnCompletion, boolean canRedo) throws SQLException Description copied from interface:ClientSend client message and read result- Specified by:
executein interfaceClient- Parameters:
message- client messagestmt- statementfetchSize- fetch sizemaxRows- maximum number of rows. 0 = allresultSetConcurrency- concurrencyresultSetType- result-set typecloseOnCompletion- close statement on completioncanRedo- can client message be redone in case of failover- Returns:
- results
- Throws:
SQLException- if any error occurs
-
readResponse
public List<Completion> readResponse(Statement stmt, ClientMessage message, int fetchSize, long maxRows, int resultSetConcurrency, int resultSetType, boolean closeOnCompletion) throws SQLException Read server responses for a client message- Parameters:
stmt- statement that issue the messagemessage- client message sentfetchSize- fetch sizemaxRows- maximum number of rowsresultSetConcurrency- concurrencyresultSetType- result-set typecloseOnCompletion- close statement on resultset completion- Returns:
- list of result
- Throws:
SQLException- if any error occurs
-
readResponse
Read server response- Parameters:
message- client message that was sent- Throws:
SQLException- if any error occurs
-
closePrepare
Description copied from interface:ClientClose prepare command- Specified by:
closePreparein interfaceClient- Parameters:
prepare- prepare command- Throws:
SQLException- if any error occurs
-
readStreamingResults
public void readStreamingResults(List<Completion> completions, int fetchSize, long maxRows, int resultSetConcurrency, int resultSetType, boolean closeOnCompletion) throws SQLException Description copied from interface:ClientRead results- Specified by:
readStreamingResultsin interfaceClient- Parameters:
completions- List that will have the new resultsfetchSize- fetch sizemaxRows- maximum number of rows. 0 = allresultSetConcurrency- concurrencyresultSetType- result-set typecloseOnCompletion- close statement on completion- Throws:
SQLException- if any error occurs
-
readResults
private void readResults(Statement stmt, ClientMessage message, List<Completion> completions, int fetchSize, long maxRows, int resultSetConcurrency, int resultSetType, boolean closeOnCompletion) throws SQLException - Throws:
SQLException
-
readPacket
Read a MySQL packet from socket- Parameters:
message- client message issuing the result- Returns:
- a mysql result
- Throws:
SQLException- if any error occurs
-
readPacket
public Completion readPacket(Statement stmt, ClientMessage message, int fetchSize, long maxRows, int resultSetConcurrency, int resultSetType, boolean closeOnCompletion) throws SQLException Read server response packet.- Parameters:
stmt- current statement (null if internal)message- current messagefetchSize- default fetch sizemaxRows- maximum row numberresultSetConcurrency- concurrencyresultSetType- typecloseOnCompletion- must resultset close statement on completion- Returns:
- Completion
- Throws:
SQLException- if any exception- See Also:
-
checkNotClosed
Throw an exception if client is closed- Throws:
SQLException- if closed
-
closeSocket
private void closeSocket() -
isClosed
-
getContext
Description copied from interface:ClientGet connection context- Specified by:
getContextin interfaceClient- Returns:
- connection context
-
abort
Description copied from interface:ClientAbort current connection- Specified by:
abortin interfaceClient- Parameters:
executor- executor- Throws:
SQLException- if any error occurs
-
getSocketTimeout
public int getSocketTimeout()Description copied from interface:Clientget socket timeout- Specified by:
getSocketTimeoutin interfaceClient- Returns:
- socket timeout
-
setSocketTimeout
Description copied from interface:ClientSet socket timeout- Specified by:
setSocketTimeoutin interfaceClient- Parameters:
milliseconds- timeout- Throws:
SQLException- if any error occurs
-
close
public void close()Description copied from interface:ClientClose client- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceClient
-
getSocketIp
Description copied from interface:ClientGet current socket IP or null (for Pipe / unix socket)- Specified by:
getSocketIpin interfaceClient- Returns:
- Socket current IP
-
isPrimary
-
getExceptionFactory
Description copied from interface:ClientGet connection exception factory- Specified by:
getExceptionFactoryin interfaceClient- Returns:
- connection exception factory
-
getHostAddress
Description copied from interface:ClientGet connection host- Specified by:
getHostAddressin interfaceClient- Returns:
- connection host
-
reset
-