关于java连接mongodb超时时间

uuuuy 发布于 2017/08/01 14:10
阅读 6K+
收藏 0

【Gopher China万字分享】华为云的Go语言云原生实战经验!>>>

当mongodb服务器宕机时,程序肯定连接不上。但是要等待很长时间才能抛出com.mongodb.MongoTimeoutException的异常。即使是设置了socketTimeout和connectTimeout的时间,仍然是要等待30000ms。想请教一下,如何能设置这个时间,或者能先判断是否能连接上mongodb。所用MongoDB Java Driver版本是3.2.2。

下面列出异常信息和连接mongodb时的一些参数。

com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=172.17.153.168:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException}}]
    at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:369)
    at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:101)
    at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:75)
    at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:71)
    at com.mongodb.binding.ClusterBinding.getReadConnectionSource(ClusterBinding.java:63)
    at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:210)
    at com.mongodb.operation.FindOperation.execute(FindOperation.java:482)
    at com.mongodb.operation.FindOperation.execute(FindOperation.java:79)
    at com.mongodb.Mongo.execute(Mongo.java:772)
    at com.mongodb.Mongo$2.execute(Mongo.java:759)
    at com.mongodb.FindIterableImpl$FindOperationIterable.first(FindIterableImpl.java:207)
    at com.mongodb.FindIterableImpl.first(FindIterableImpl.java:148)
    at com.ningxun.util.MongoUtil.findOne(MongoUtil.java:365)
    at com.ningxun.util.MongoUtil.main(MongoUtil.java:599)
Exception in thread "main" com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=172.17.153.168:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException}}]
    at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:369)
    at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:101)
    at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:75)
    at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:71)
    at com.mongodb.binding.ClusterBinding.getReadConnectionSource(ClusterBinding.java:63)
    at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:210)
    at com.mongodb.operation.FindOperation.execute(FindOperation.java:482)
    at com.mongodb.operation.FindOperation.execute(FindOperation.java:79)
    at com.mongodb.Mongo.execute(Mongo.java:772)
    at com.mongodb.Mongo$2.execute(Mongo.java:759)
    at com.mongodb.FindIterableImpl$FindOperationIterable.first(FindIterableImpl.java:207)
    at com.mongodb.FindIterableImpl.first(FindIterableImpl.java:148)
    at com.ningxun.util.MongoUtil.findOne(MongoUtil.java:365)
    at com.ningxun.util.MongoUtil.main(MongoUtil.java:599)

加载中
0
uuuuy
uuuuy

找到配置项了,在MongoClientOptions.Builder中的serverSelectionTimeout配置相应参数即可。

刘王林123
回复 @Kevin_cfb : 请教一下 怎么解决的cvc-complex-type.3.2.2: 元素 'mongo:client-options' 中不允许出现属性 'server-selection-timeout'。
K
Kevin_cfb
楼主是怎么解决的?请教下,谢谢
0
K
Kevin_cfb

请问是怎么解决的,我现在也遇到相同的问题。

0
ERR_CONNECTION
ERR_CONNECTION
server-selection-timeout="100"

spring-data-mongodb的xml文件里面加一个这个属性

返回顶部
顶部