Class StandardClient

java.lang.Object
org.mariadb.jdbc.client.impl.StandardClient
All Implemented Interfaces:
AutoCloseable, Client
Direct Known Subclasses:
ReplayClient

public class StandardClient extends Object implements Client, AutoCloseable
Connection client
  • Field Details

    • logger

      private static final Logger logger
    • exceptionFactory

      protected final ExceptionFactory exceptionFactory
      connection exception factory
    • REDIRECT_PATTERN

      private static final Pattern REDIRECT_PATTERN
    • socket

      private Socket socket
    • sequence

      private final MutableByte sequence
    • compressionSequence

      private final MutableByte compressionSequence
    • lock

      private final ClosableLock lock
    • conf

      private Configuration conf
    • authPlugin

      private AuthenticationPlugin authPlugin
    • hostAddress

      private HostAddress hostAddress
    • disablePipeline

      private final boolean disablePipeline
    • context

      protected Context context
      connection context
    • writer

      protected Writer writer
      packet writer
    • closed

      private boolean closed
    • reader

      private Reader reader
    • certFingerprint

      private byte[] certFingerprint
    • streamStmt

      private Statement streamStmt
    • streamMsg

      private ClientMessage streamMsg
    • socketTimeout

      private int socketTimeout
    • redirectConsumer

      private final Consumer<String> redirectConsumer
  • Constructor Details

    • StandardClient

      public StandardClient(Configuration conf, HostAddress hostAddress, ClosableLock lock, boolean skipPostCommands) throws SQLException
      Constructor
      Parameters:
      conf - configuration
      hostAddress - host
      lock - thread locker
      skipPostCommands - must connection post command be skipped
      Throws:
      SQLException - if connection fails
  • Method Details

    • setupConnection

      private void setupConnection(boolean skipPostCommands) throws SQLException, IOException
      Throws:
      SQLException
      IOException
    • setupCompression

      private void setupCompression(InputStream in, OutputStream out, long clientCapabilities, long threadId)
    • handleSSLConnection

      private SSLSocket handleSSLConnection(InitialHandshakePacket handshake, long clientCapabilities) throws SQLException, IOException
      Throws:
      SQLException
      IOException
    • updateThreadIds

      private void updateThreadIds(InitialHandshakePacket handshake)
    • determineSslMode

      private SslMode determineSslMode(Configuration conf)
    • validateServerSslCapability

      private void validateServerSslCapability() throws SQLException
      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:
      SQLException
      IOException
    • configureSslSocket

      private void configureSslSocket(SSLSocket sslSocket, Configuration conf) throws SQLException
      Throws:
      SQLException
    • handleSslHandshake

      private void handleSslHandshake(SSLSocket sslSocket, TrustManager[] trustManagers) throws IOException
      Throws:
      IOException
    • requiresHostnameVerification

      private boolean requiresHostnameVerification(SslMode sslMode)
    • verifyHostname

      private void verifyHostname(SSLSocket sslSocket, TlsSocketPlugin socketPlugin) throws SQLException
      Throws:
      SQLException
    • configureTimeout

      private void configureTimeout() throws SQLException
      Throws:
      SQLException
    • handleServerHandshake

      private InitialHandshakePacket handleServerHandshake() throws SQLException, IOException
      Throws:
      SQLException
      IOException
    • throwHandshakeError

      private void throwHandshakeError(ReadableByteBuf buf) throws SQLException
      Throws:
      SQLException
    • setupClientCapabilities

      private long setupClientCapabilities(InitialHandshakePacket handshake)
    • initializeContext

      private void initializeContext(InitialHandshakePacket handshake, long clientCapabilities)
    • handleAuthentication

      private void handleAuthentication(InitialHandshakePacket handshake, long clientCapabilities) throws IOException, SQLException
      Throws:
      IOException
      SQLException
    • determineAuthType

      private String determineAuthType(InitialHandshakePacket handshake)
    • handleConnectionError

      private void handleConnectionError(SQLException e) throws SQLException
      Throws:
      SQLException
    • handleTimeoutError

      private void handleTimeoutError(SocketTimeoutException e) throws SQLTimeoutException
      Throws:
      SQLTimeoutException
    • handleIOError

      private void handleIOError(IOException e) throws SQLException
      Throws:
      SQLException
    • sendHandshakeResponse

      private void sendHandshakeResponse(InitialHandshakePacket handshake, long clientCapabilities, Credential credential, String authType) throws IOException
      Throws:
      IOException
    • createAuthPlugin

      private void createAuthPlugin(InitialHandshakePacket handshake, Credential credential, String authType)
    • authenticationHandler

      public void authenticationHandler(Credential credential, HostAddress hostAddress) throws IOException, SQLException
      Parameters:
      credential - credential
      hostAddress - host address
      Throws:
      IOException - if any socket error occurs
      SQLException - if any other kind of issue occurs
    • validateFingerPrint

      private static boolean validateFingerPrint(AuthenticationPlugin authPlugin, byte[] validationHash, byte[] fingerPrint, Credential credential, byte[] seed)
    • redirect

      public void redirect(String redirectUrl)
    • assignStream

      private void assignStream(OutputStream out, InputStream in, Configuration conf, Long threadId)
    • destroySocket

      protected void destroySocket()
      Closing socket in case of Connection error after socket creation.
    • handleTimezone

      private void handleTimezone() throws SQLException
      load server timezone and ensure this corresponds to client timezone
      Throws:
      SQLException - if any socket error.
    • postConnectionQueries

      private void postConnectionQueries() throws SQLException
      Throws:
      SQLException
    • createSessionVariableQuery

      public String createSessionVariableQuery(Context context)
      Creates a query string for setting session variables based on context and configuration.
      Parameters:
      context - the connection context
      Returns:
      query string for setting session variables, or null if no variables need to be set
    • addAutoCommitCommand

      private void addAutoCommitCommand(Context context, List<String> commands)
    • isReliableConnectionFlag

      private boolean isReliableConnectionFlag(Context context)
    • isAutoCommitUpdateRequired

      private boolean isAutoCommitUpdateRequired(Context context, boolean canRelyOnConnectionFlag)
    • addTruncationCommand

      private void addTruncationCommand(List<String> commands)
    • addSessionTrackingCommand

      private void addSessionTrackingCommand(Context context, List<String> commands)
    • isSessionTrackingSupported

      private boolean isSessionTrackingSupported(Context context)
    • addTimeZoneCommand

      private void addTimeZoneCommand(Context context, List<String> commands)
    • addFixedOffsetTimeZone

      private void addFixedOffsetTimeZone(ZoneId connectionZoneId, List<String> commands)
    • addTransactionIsolationCommand

      private void addTransactionIsolationCommand(Context context, List<String> commands)
    • addReadOnlyCommand

      private void addReadOnlyCommand(Context context, List<String> commands)
    • addCharsetCommand

      private void addCharsetCommand(Context context, List<String> commands)
    • isDefaultCharsetSufficient

      private boolean isDefaultCharsetSufficient(Context context)
    • addCustomSessionVariables

      private void addCustomSessionVariables(List<String> commands)
    • buildFinalQuery

      private String buildFinalQuery(List<String> commands)
    • setReadOnly

      public void setReadOnly(boolean readOnly) throws SQLException
      Description copied from interface: Client
      Switch to a writer/read-only connection, no effet on mono-connection
      Specified by:
      setReadOnly in interface Client
      Parameters:
      readOnly - must use read-only connection
      Throws:
      SQLException - if any error occurs
    • sendQuery

      public int sendQuery(ClientMessage message) throws SQLException
      Send client message to server
      Parameters:
      message - client message
      Returns:
      number of command send
      Throws:
      SQLException - if socket error occurs
    • execute

      public List<Completion> execute(ClientMessage message, boolean canRedo) throws SQLException
      Description copied from interface: Client
      Send client message and read result
      Specified by:
      execute in interface Client
      Parameters:
      message - client message
      canRedo - 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: Client
      Send client message and read result
      Specified by:
      execute in interface Client
      Parameters:
      message - client message
      stmt - statement
      canRedo - 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: Client
      Send client messages pipelining and read result
      Specified by:
      executePipeline in interface Client
      Parameters:
      messages - client message
      stmt - statement
      fetchSize - fetch size
      maxRows - maximum number of rows. 0 = all
      resultSetConcurrency - concurrency
      resultSetType - result-set type
      closeOnCompletion - close statement on completion
      canRedo - 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: Client
      Send client message and read result
      Specified by:
      execute in interface Client
      Parameters:
      message - client message
      stmt - statement
      fetchSize - fetch size
      maxRows - maximum number of rows. 0 = all
      resultSetConcurrency - concurrency
      resultSetType - result-set type
      closeOnCompletion - close statement on completion
      canRedo - 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 message
      message - client message sent
      fetchSize - fetch size
      maxRows - maximum number of rows
      resultSetConcurrency - concurrency
      resultSetType - result-set type
      closeOnCompletion - close statement on resultset completion
      Returns:
      list of result
      Throws:
      SQLException - if any error occurs
    • readResponse

      public void readResponse(ClientMessage message) throws SQLException
      Read server response
      Parameters:
      message - client message that was sent
      Throws:
      SQLException - if any error occurs
    • closePrepare

      public void closePrepare(Prepare prepare) throws SQLException
      Description copied from interface: Client
      Close prepare command
      Specified by:
      closePrepare in interface Client
      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: Client
      Read results
      Specified by:
      readStreamingResults in interface Client
      Parameters:
      completions - List that will have the new results
      fetchSize - fetch size
      maxRows - maximum number of rows. 0 = all
      resultSetConcurrency - concurrency
      resultSetType - result-set type
      closeOnCompletion - 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

      public Completion readPacket(ClientMessage message) throws SQLException
      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 message
      fetchSize - default fetch size
      maxRows - maximum row number
      resultSetConcurrency - concurrency
      resultSetType - type
      closeOnCompletion - must resultset close statement on completion
      Returns:
      Completion
      Throws:
      SQLException - if any exception
      See Also:
    • checkNotClosed

      protected void checkNotClosed() throws SQLException
      Throw an exception if client is closed
      Throws:
      SQLException - if closed
    • closeSocket

      private void closeSocket()
    • isClosed

      public boolean isClosed()
      Description copied from interface: Client
      Is client closed
      Specified by:
      isClosed in interface Client
      Returns:
      close flag
    • getContext

      public Context getContext()
      Description copied from interface: Client
      Get connection context
      Specified by:
      getContext in interface Client
      Returns:
      connection context
    • abort

      public void abort(Executor executor) throws SQLException
      Description copied from interface: Client
      Abort current connection
      Specified by:
      abort in interface Client
      Parameters:
      executor - executor
      Throws:
      SQLException - if any error occurs
    • getSocketTimeout

      public int getSocketTimeout()
      Description copied from interface: Client
      get socket timeout
      Specified by:
      getSocketTimeout in interface Client
      Returns:
      socket timeout
    • setSocketTimeout

      public void setSocketTimeout(int milliseconds) throws SQLException
      Description copied from interface: Client
      Set socket timeout
      Specified by:
      setSocketTimeout in interface Client
      Parameters:
      milliseconds - timeout
      Throws:
      SQLException - if any error occurs
    • close

      public void close()
      Description copied from interface: Client
      Close client
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Client
    • getSocketIp

      public String getSocketIp()
      Description copied from interface: Client
      Get current socket IP or null (for Pipe / unix socket)
      Specified by:
      getSocketIp in interface Client
      Returns:
      Socket current IP
    • isPrimary

      public boolean isPrimary()
      Description copied from interface: Client
      is current client writer or read-only
      Specified by:
      isPrimary in interface Client
      Returns:
      is primary
    • getExceptionFactory

      public ExceptionFactory getExceptionFactory()
      Description copied from interface: Client
      Get connection exception factory
      Specified by:
      getExceptionFactory in interface Client
      Returns:
      connection exception factory
    • getHostAddress

      public HostAddress getHostAddress()
      Description copied from interface: Client
      Get connection host
      Specified by:
      getHostAddress in interface Client
      Returns:
      connection host
    • reset

      public void reset()
      Description copied from interface: Client
      Reset connection
      Specified by:
      reset in interface Client