Skip to content

Commit cf64e3f

Browse files
committed
[JAVA-287]: set TCP_KEEPALIVE on the java driver
1 parent 8102fdd commit cf64e3f

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/main/com/mongodb/DBPort.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ boolean _open()
196196
_socket.connect( _addr , _options.connectTimeout );
197197

198198
_socket.setTcpNoDelay( ! USE_NAGLE );
199+
_socket.setKeepAlive( _options.socketKeepAlive );
199200
_socket.setSoTimeout( _options.socketTimeout );
200201
_in = new BufferedInputStream( _socket.getInputStream() );
201202
_out = _socket.getOutputStream();

src/main/com/mongodb/MongoOptions.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public void reset(){
3535
maxWaitTime = 1000 * 60 * 2;
3636
connectTimeout = 0;
3737
socketTimeout = 0;
38+
socketKeepAlive = false;
3839
autoConnectRetry = false;
3940
slaveOk = false;
4041
safe = false;
@@ -94,6 +95,14 @@ else if (safe)
9495
*/
9596
public int socketTimeout;
9697

98+
/**
99+
* This controls whether or not to have socket keep alive
100+
* turned on (SO_KEEPALIVE).
101+
*
102+
* defaults to false
103+
*/
104+
public boolean socketKeepAlive;
105+
97106
/**
98107
* This controls whether the system retries automatically
99108
* on connection errors.
@@ -155,6 +164,7 @@ public String toString(){
155164
buf.append( "maxWaitTime: " ).append( maxWaitTime ).append( " " );
156165
buf.append( "connectTimeout: " ).append( connectTimeout ).append( " " );
157166
buf.append( "socketTimeout: " ).append( socketTimeout ).append( " " );
167+
buf.append( "socketKeepAlive: " ).append( socketKeepAlive ).append( " " );
158168
buf.append( "autoConnectRetry: " ).append( autoConnectRetry ).append( " " );
159169
buf.append( "slaveOk: " ).append( slaveOk ).append( " " );
160170
buf.append( "safe: " ).append( safe ).append( " " );

0 commit comments

Comments
 (0)