[JAVA-290] mongo driver should check whether socket still be available before use it? Created: 03/Mar/11  Updated: 19/Oct/16  Resolved: 14/Jun/11

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: 2.4
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: phoenix Assignee: Unassigned
Resolution: Duplicate Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

mongodb 1.8


Issue Links:
Depends
depends on JAVA-335 all connections to a server should ge... Closed

 Description   

After the driver open a socket to mongos,we shutdown mongos and restart it,then write two record into mongos,the first one will lost.

We had set autoConnectRetry to true.

The driver check if ( _socket == null ) only,should it check _socket can works fine also?

There are some exceptions:

Caused by: com.mongodb.MongoException$Network: can't say something
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:169) ~[mongo-java-driver-2.4-jar.jar:na]
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:141) ~[mongo-java-driver-2.4-jar.jar:na]
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:225) ~[mongo-java-driver-2.4-jar.jar:na]
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:180) ~[mongo-java-driver-2.4-jar.jar:na]
Caused by: java.net.SocketException: Broken pipe
at java.net.SocketOutputStream.socketWrite0(Native Method) ~[na:1.6.0_16]
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92) ~[na:1.6.0_16]
at java.net.SocketOutputStream.write(SocketOutputStream.java:136) ~[na:1.6.0_16]
at org.bson.io.PoolOutputBuffer.pipe(PoolOutputBuffer.java:113) ~[mongo-java-driver-2.4-jar.jar:na]
at com.mongodb.OutMessage.pipe(OutMessage.java:157) ~[mongo-java-driver-2.4-jar.jar:na]
at com.mongodb.DBPort.go(DBPort.java:92) ~[mongo-java-driver-2.4-jar.jar:na]
at com.mongodb.DBPort.go(DBPort.java:66) ~[mongo-java-driver-2.4-jar.jar:na]
at com.mongodb.DBPort.say(DBPort.java:61) ~[mongo-java-driver-2.4-jar.jar:na]
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:155) ~[mongo-java-driver-2.4-jar.jar:na]



 Comments   
Comment by Antoine Girbal [ 16/Aug/11 ]

if you get the SocketTimeoutException it means that the server is not responding at all on that connection (not even RST).
So most likely it would be impossible to test the connection state.
Also doing retries after a timeout is dangerous as it can lock up the driver for a while.
Your code needs to properly catch those exceptions.
thx

Comment by Dion C [ 16/Aug/11 ]

I'm seeing something similar in that the connection to the mongo server is closed (server log shows connection closed), but the java socket doesn't seem to realise it has been closed. Then attempting to do a find on a collection, results in "java.net.SocketTimeoutException: Read timed out"
Subsequent finds are fine, since a new connection is made after the SocketTimeoutException - until that new socket eventually closes again in a few hours.
I think this wouldn't be a problem if it were to retry since it would then have a new connection, but retries were specifically excluded for SocketTimeouts by JAVA-351

mongo 1.8.1 + java driver 2.6.3

Comment by Scott Hernandez (Inactive) [ 14/Jun/11 ]

If the socket is in a bad state it will throw an exception and we will create a new one. I believe we are doing a better job at tossing all those server's sockets in this case and this improvement has been included in 2.6.x: see JAVA-335

There are always going to be errors on writes after a server has been restarted, or the connections have been lost. You can do a read first to flush the sockets for example. Doing this in the driver is no more efficient and isn't appropriate for writes.

Comment by Paul D. Walker [ 13/Jun/11 ]

I'm seeing this problem too often. For some reason that I don't know why, the socket is getting closed with a "java.net.SocketException: Connection reset" error. (java driver 2.5.3 - server 1.8.1)

Comment by Andrew Louth [ 23/Mar/11 ]

I'm interested in seeing a solution here because I need to handle cases where the mongo server has been restarted.

In these cases, the server is available but saves will fail because the socket is no longer valid.

Generated at Thu Feb 08 08:51:56 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.