private void connect ( String host , int port ) throws SQLException , IOException { try { socket = Utils . createSocket ( urlParser , host ) ; if ( options . socketTimeout != null ) { socket . setSoTimeout ( options . socketTimeout ) ; } initializeSocketOption ( ) ; // Bind the socket to a particular interface if the connection property // localSocketAddress has been defined. if ( options . localSocketAddress != null ) { InetSocketAddress localAddress = new InetSocketAddress ( options . localSocketAddress , 0 ) ; socket . bind ( localAddress ) ; } if ( ! socket . isConnected ( ) ) { InetSocketAddress sockAddr = urlParser . getOptions ( ) . pipe == null ? new InetSocketAddress ( host , port ) : null ; if ( options . connectTimeout != 0 ) { socket . connect ( sockAddr , options . connectTimeout ) ; } else { socket . connect ( sockAddr ) ; } } handleConnectionPhases ( host ) ; connected = true ; if ( options . useCompression ) { writer = new CompressPacketOutputStream ( writer . getOutputStream ( ) , options . maxQuerySizeToLog ) ; reader = new DecompressPacketInputStream ( ( ( StandardPacketInputStream ) reader ) . getInputStream ( ) , options . maxQuerySizeToLog ) ; if ( options . enablePacketDebug ) { writer . setTraceCache ( traceCache ) ; reader . setTraceCache ( traceCache ) ; } } boolean mustLoadAdditionalInfo = true ; if ( globalInfo != null ) { if ( globalInfo . isAutocommit ( ) == options . autocommit ) { mustLoadAdditionalInfo = false ; } } if ( mustLoadAdditionalInfo ) { Map < String , String > serverData = new TreeMap <> ( ) ; if ( options . usePipelineAuth && ! options . createDatabaseIfNotExist ) { try { sendPipelineAdditionalData ( ) ; readPipelineAdditionalData ( serverData ) ; } catch ( SQLException sqle ) { if ( "08" . equals ( sqle . getSQLState ( ) ) ) { throw sqle ; } //in case pipeline is not supported //(proxy flush socket after reading first packet) additionalData ( serverData ) ; } } else { additionalData ( serverData ) ; } writer . setMaxAllowedPacket ( Integer . parseInt ( serverData . get ( "max_allowed_packet" ) ) ) ; autoIncrementIncrement = Integer . parseInt ( serverData . get ( "auto_increment_increment" ) ) ; loadCalendar ( serverData . get ( "time_zone" ) , serverData . get ( "system_time_zone" ) ) ; } else { writer . setMaxAllowedPacket ( ( int ) globalInfo . getMaxAllowedPacket ( ) ) ; autoIncrementIncrement = globalInfo . getAutoIncrementIncrement ( ) ; loadCalendar ( globalInfo . getTimeZone ( ) , globalInfo . getSystemTimeZone ( ) ) ; } reader . setServerThreadId ( this . serverThreadId , isMasterConnection ( ) ) ; writer . setServerThreadId ( this . serverThreadId , isMasterConnection ( ) ) ; activeStreamingResult = null ; hostFailed = false ; } catch ( IOException | SQLException ioException ) { ensureClosingSocketOnException ( ) ; throw ioException ; } }
Connect the client and perform handshake .