2020
2121import java .io .IOException ;
2222import java .net .ConnectException ;
23- import java .net .Socket ;
24- import java .net .SocketAddress ;
25- import java .net .SocketTimeoutException ;
2623import java .nio .ByteBuffer ;
2724import java .nio .channels .ByteChannel ;
2825import java .util .Queue ;
2926
3027import org .neo4j .driver .internal .messaging .Message ;
3128import org .neo4j .driver .internal .messaging .MessageFormat ;
3229import org .neo4j .driver .internal .security .SecurityPlan ;
33- import org .neo4j .driver .internal .security .TLSSocketChannel ;
3430import org .neo4j .driver .internal .util .BytePrinter ;
35- import org .neo4j .driver .v1 .Config ;
3631import org .neo4j .driver .v1 .Logger ;
3732import org .neo4j .driver .v1 .exceptions .ClientException ;
3833import org .neo4j .driver .v1 .exceptions .ServiceUnavailableException ;
@@ -123,31 +118,22 @@ void blockingWrite( ByteBuffer buf ) throws IOException
123118
124119 public void start ()
125120 {
126- Socket socket = null ;
127- boolean connected = false ;
128121 try
129122 {
130123 logger .debug ( "Connecting to %s, secure: %s" , address , securityPlan .requiresEncryption () );
131124 if ( channel == null )
132125 {
133- socket = newSocket ( timeoutMillis );
134- setChannel ( ChannelFactory .create ( socket , address , securityPlan , timeoutMillis , logger ) );
126+ setChannel ( ChannelFactory .create ( address , securityPlan , timeoutMillis , logger ) );
135127 logger .debug ( "Connected to %s, secure: %s" , address , securityPlan .requiresEncryption () );
136128 }
137129
138130 logger .debug ( "Negotiating protocol with %s" , address );
139131 SocketProtocol protocol = negotiateProtocol ();
140132 setProtocol ( protocol );
141133 logger .debug ( "Selected protocol %s with %s" , protocol .getClass (), address );
142-
143- // reset read timeout (SO_TIMEOUT) to the original value of zero
144- // we do not want to permanently limit amount of time driver waits for database to execute query
145- socket .setSoTimeout ( 0 );
146- connected = true ;
147134 }
148- catch ( ConnectException | SocketTimeoutException e )
135+ catch ( ConnectException e )
149136 {
150- // unable to connect socket or TLS/Bolt handshake took too much time
151137 throw new ServiceUnavailableException ( format (
152138 "Unable to connect to %s, ensure the database is running and that there is a " +
153139 "working network connection to it." , address ), e );
@@ -156,19 +142,6 @@ public void start()
156142 {
157143 throw new ServiceUnavailableException ( "Unable to process request: " + e .getMessage (), e );
158144 }
159- finally
160- {
161- if ( !connected && socket != null )
162- {
163- try
164- {
165- socket .close ();
166- }
167- catch ( Throwable ignore )
168- {
169- }
170- }
171- }
172145 }
173146
174147 public void updateProtocol ( String serverVersion )
@@ -328,35 +301,4 @@ public BoltServerAddress address()
328301 {
329302 return address ;
330303 }
331-
332- /**
333- * Creates new {@link Socket} object with {@link Socket#setSoTimeout(int) read timeout} set to the given value.
334- * Connection to bolt server includes:
335- * <ol>
336- * <li>TCP connect via {@link Socket#connect(SocketAddress, int)}</li>
337- * <li>Optional TLS handshake using {@link TLSSocketChannel}</li>
338- * <li>Bolt handshake</li>
339- * </ol>
340- * We do not want any of these steps to hang infinitely if server does not respond and thus:
341- * <ol>
342- * <li>Use {@link Socket#connect(SocketAddress, int)} with timeout, as configured in
343- * {@link Config#connectionTimeoutMillis()}</li>
344- * <li>Initially set {@link Socket#setSoTimeout(int) read timeout} on the socket. Same connection-timeout value
345- * from {@link Config#connectionTimeoutMillis()} is used. This way blocking reads during TLS and Bolt handshakes
346- * have limited waiting time</li>
347- * </ol>
348- *
349- * @param configuredConnectTimeout user-defined connection timeout to be initially used as read timeout.
350- * @return new socket.
351- * @throws IOException when creation or configuration of the socket fails.
352- */
353- private static Socket newSocket ( int configuredConnectTimeout ) throws IOException
354- {
355- Socket socket = new Socket ();
356- socket .setReuseAddress ( true );
357- socket .setKeepAlive ( true );
358- // set read timeout initially
359- socket .setSoTimeout ( configuredConnectTimeout );
360- return socket ;
361- }
362304}
0 commit comments