简体   繁体   中英

java.net.SocketException: No buffer space available (maximum connections reached?): connect MongoDB JAVA API Driver

There's MongoDB replica set which is connected regularly by threads writing data to it. After a while, i'm getting this error.

WARNING: Server seen down: mongoServer:port - java.io.IOException - message: couldn't connect to [mongoServer:port] bc:java.net.SocketException: No buffer space available (maximum connections reached?): connect
ינו 26, 2014 10:29:57 PM com.mongodb.DBPort _open
INFO: connect fail to : mongoServer:port
java.net.SocketException: No buffer space available (maximum connections reached?): connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at com.mongodb.DBPort._open(DBPort.java:204)
    at com.mongodb.DBPort.go(DBPort.java:107)
    at com.mongodb.DBPort.go(DBPort.java:88)
    at com.mongodb.DBPort.findOne(DBPort.java:143)
    at com.mongodb.DBPort.runCommand(DBPort.java:148)
    at com.mongodb.ConnectionStatus$UpdatableNode.update(ConnectionStatus.java:184)
    at com.mongodb.ReplicaSetStatus$UpdatableReplicaSetNode.update(ReplicaSetStatus.java:612)
    at com.mongodb.ReplicaSetStatus$Updater.updateAll(ReplicaSetStatus.java:764)
    at com.mongodb.ReplicaSetStatus$Updater.run(ReplicaSetStatus.java:734)

I would be happy if someone can tell why does it happen and how to prevent it. I was thinking that the reason linked to fact that each opened connection to mongoDB encapsulated in MongoClient object should be closed.

I suspect that the problem is that your application is "leaking" connections; ie it is opening lots of connections to the MongoDB server and neglecting to close them. This is causing something (the OS kernel?) to run out of buffer space.

The fix is to modify your code so that all connections are closed once they are finished with. The recommend way to do this is:

  • Java 7 and later - use "try with resource"

  • Java 6 and earlier - use "try ... finally" and explicitly close the resource in the finally block.


It is also possible that your application is trying to send too many requests to MongoDB simultaneously; ie with a swarm of request threads. If you are doing that ... don't.

Connection send data will use direct memory. If the connection you created not too much, maybe you can use -XX:MaxDirectMemorySize= to set max direct memory size. If it does not work, you can check and release the count of connection and close them after use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM