MongoDB Java driver: autoConnectRetry (stackoverflow)
autoConnectRetry and maxAutoConnectRetryTime are deprecated in the current version最新版本中autoConnectRetry和maxAutoConnectRetryTime倆方法時(shí)被抑制的.
解釋
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.
很多人對(duì)autoConnectRetry產(chǎn)生許多誤解.人們錯(cuò)認(rèn)為由于異常操作失敗時(shí),驅(qū)動(dòng)會(huì)重試操作直到maxAutoConnectRetryTime的時(shí)間用盡.
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).
事實(shí)上它做的是socket連接,驅(qū)動(dòng)會(huì)重試直到maxAutoConnectRetryTime的時(shí)間用盡.
但是這些是connectTimeout規(guī)定的內(nèi)容.autoConnectRetry只是獲得更長(zhǎng)的 connect timeout,即連接超時(shí).
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).
由于這些誤解,并且事實(shí)上其他mongo驅(qū)動(dòng)并未規(guī)定該類方法.我們決定抑制它(下版本將移除)
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驅(qū)動(dòng)不能移除線程池中死掉的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驅(qū)動(dòng) 只有當(dāng)你用的時(shí)候,它才會(huì)告訴你連接已斷開(kāi).驅(qū)動(dòng)不會(huì)定期的檢查連接是否正常.原因是這樣會(huì)帶來(lái)性能上的影響.另外,如果某一個(gè)連接拋了異常,剩余別的連接會(huì)關(guān)掉,移除連接.再操作時(shí)會(huì)建立新的連接.
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.
所以你的應(yīng)用應(yīng)該捕捉異常進(jìn)行重試連接.
方案
add an interceptor method before every action in my application, and which will call a connection to check if it is closed.