MongoDB Java driver: autoConnectRetry (stackoverflow)
autoConnectRetry and maxAutoConnectRetryTime are deprecated in the current version最新版本中autoConnectRetry和maxAutoConnectRetryTime俩方法时被抑制的.
解释
There was a lot of confusion about the meaning of autoConnectRetry. Most people think it means that, if an operation failed due to an IOException, the driver would retry the operation until maxAutoConnectRetryTime elapsed. But that is not the case.
很多人对autoConnectRetry产生许多误解.人们错认为由于异常操作失败时,驱动会重试操作直到maxAutoConnectRetryTime的时间用尽.
All it means is that, on calls to Socket.connect(), the driver retries a failed attempt to connect until maxAutoConnectRetryTime elapsed. But this is exactly what connectTimeout is for. The only additional capability of autoConnectRetry is so that you can specify a longer connect timeout than is allowed by the underlying operating system (which typically enforces a max connect timeout that caps the value that the user specifies).
事实上它做的是socket连接,驱动会重试直到maxAutoConnectRetryTime的时间用尽.
但是这些是connectTimeout规定的内容.autoConnectRetry只是获得更长的 connect timeout,即连接超时.
Due to this confusion, the lack of value of the feature, and the fact that none of the other MongoDB drivers support this feature, we decided to deprecate it (and remove it in the next major release).
由于这些误解,并且事实上其他mongo驱动并未规定该类方法.我们决定抑制它(下版本将移除)
com.mongodb.DBPortPool gotError Warning: emptying DBPortPool to /IP:27017 b/c of error
MongoDB Driver can't remove dropped socket from connection from pool until your code try use it
原因
mongo驱动不能移除线程池中死掉的socket.
The MongoDB Java driver can only tell a connection is dead when you start to use it - it doesn't periodically check connections (for example) to check they're still alive because doing this automatically could have a performance impact. However, if you get an IOException in a single connection, all other connections will be closed and removed so new connections will be created afterwards.
java的 mngo驱动 只有当你用的时候,它才会告诉你连接已断开.驱动不会定期的检查连接是否正常.原因是这样会带来性能上的影响.另外,如果某一个连接抛了异常,剩余别的连接会关掉,移除连接.再操作时会建立新的连接.
It does mean that applications have a responsibility to catch Exceptions and perform a retry if appropriate. Really your application is the best place to decide what to do in exceptional circumstances like connections disappearing.
所以你的应用应该捕捉异常进行重试连接.
方案
add an interceptor method before every action in my application, and which will call a connection to check if it is closed.