20
20
21
21
import java .io .IOException ;
22
22
import java .net .ConnectException ;
23
- import java .net .Socket ;
24
- import java .net .SocketAddress ;
25
- import java .net .SocketTimeoutException ;
26
23
import java .nio .ByteBuffer ;
27
24
import java .nio .channels .ByteChannel ;
28
25
import java .util .Queue ;
29
26
30
27
import org .neo4j .driver .internal .messaging .Message ;
31
28
import org .neo4j .driver .internal .messaging .MessageFormat ;
32
29
import org .neo4j .driver .internal .security .SecurityPlan ;
33
- import org .neo4j .driver .internal .security .TLSSocketChannel ;
34
30
import org .neo4j .driver .internal .util .BytePrinter ;
35
- import org .neo4j .driver .v1 .Config ;
36
31
import org .neo4j .driver .v1 .Logger ;
37
32
import org .neo4j .driver .v1 .exceptions .ClientException ;
38
33
import org .neo4j .driver .v1 .exceptions .ServiceUnavailableException ;
@@ -123,31 +118,22 @@ void blockingWrite( ByteBuffer buf ) throws IOException
123
118
124
119
public void start ()
125
120
{
126
- Socket socket = null ;
127
- boolean connected = false ;
128
121
try
129
122
{
130
123
logger .debug ( "Connecting to %s, secure: %s" , address , securityPlan .requiresEncryption () );
131
124
if ( channel == null )
132
125
{
133
- socket = newSocket ( timeoutMillis );
134
- setChannel ( ChannelFactory .create ( socket , address , securityPlan , timeoutMillis , logger ) );
126
+ setChannel ( ChannelFactory .create ( address , securityPlan , timeoutMillis , logger ) );
135
127
logger .debug ( "Connected to %s, secure: %s" , address , securityPlan .requiresEncryption () );
136
128
}
137
129
138
130
logger .debug ( "Negotiating protocol with %s" , address );
139
131
SocketProtocol protocol = negotiateProtocol ();
140
132
setProtocol ( protocol );
141
133
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 ;
147
134
}
148
- catch ( ConnectException | SocketTimeoutException e )
135
+ catch ( ConnectException e )
149
136
{
150
- // unable to connect socket or TLS/Bolt handshake took too much time
151
137
throw new ServiceUnavailableException ( format (
152
138
"Unable to connect to %s, ensure the database is running and that there is a " +
153
139
"working network connection to it." , address ), e );
@@ -156,19 +142,6 @@ public void start()
156
142
{
157
143
throw new ServiceUnavailableException ( "Unable to process request: " + e .getMessage (), e );
158
144
}
159
- finally
160
- {
161
- if ( !connected && socket != null )
162
- {
163
- try
164
- {
165
- socket .close ();
166
- }
167
- catch ( Throwable ignore )
168
- {
169
- }
170
- }
171
- }
172
145
}
173
146
174
147
public void updateProtocol ( String serverVersion )
@@ -328,35 +301,4 @@ public BoltServerAddress address()
328
301
{
329
302
return address ;
330
303
}
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
- }
362
304
}
0 commit comments