java连接mongodb的一个奇葩问题及奇葩解决方式

标签: java mongodb 数据库 eclipse
13947人阅读 评论(6) 收藏 举报
分类:

昨天在eclipse中编写代码,本来连接mongodb进行各项操作都是正常的,但是有一会儿突然之间就没法连接了,还一直抱错,错误如下:

信息: Cluster created with settings {hosts=[192.168.0.4:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
十一月 06, 2015 9:23:42 上午 com.mongodb.diagnostics.logging.JULLogger log
信息: No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, all=[ServerDescription{address=192.168.0.4:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
十一月 06, 2015 9:24:02 上午 com.mongodb.diagnostics.logging.JULLogger log
信息: Exception in monitor thread while connecting to server 192.168.0.4:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
	at com.mongodb.connection.SocketStream.open(SocketStream.java:63)
	at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:114)
	at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127)
	at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketTimeoutException: connect timed out
	at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
	at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
	at java.net.Socket.connect(Socket.java:579)
	at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:50)
	at com.mongodb.connection.SocketStream.open(SocketStream.java:58)
	... 3 more

十一月 06, 2015 9:24:12 上午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [springMVC] in context with path [/birthdaySystem1] threw exception [Request processing failed; nested exception is 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=192.168.0.4:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}]] with root cause
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=192.168.0.4:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}]
	at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:370)
	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:166)
	at com.mongodb.operation.FindOperation.execute(FindOperation.java:394)
	at com.mongodb.operation.FindOperation.execute(FindOperation.java:57)
	at com.mongodb.Mongo.execute(Mongo.java:738)
	at com.mongodb.Mongo$2.execute(Mongo.java:725)
	at com.mongodb.DBCursor.initializeCursor(DBCursor.java:815)
	at com.mongodb.DBCursor.hasNext(DBCursor.java:149)
	at services.servicesImp.UserServiceImp.findUserName(UserServiceImp.java:60)
	at controllers.UserController.login(UserController.java:43)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:745)

对于这个问题,查了好多的资料硬是没能解决。后来偶然间关掉了QQ管家里边开启的“免费wifi”,没想到连接就一下子正常了。

于是,我有试了若干次,结果竟然都是一开这个wifi,mongodb立马就连不上,一关掉然后就一切正常。

问题解决了,但根本原因还是不曾明白。

查看评论

spring整合mongo报错:Timed Out While Waiting For A Server That Matches AnyServerSelector{} After 10000 Ms

今天在整合mina,让人头疼的是一直报超时错误,调了3个多小时的错误,终于找到原因了,下面是报的错 org.springframework.dao.DataAccessResourceFailureE...
  • maoyeqiu
  • maoyeqiu
  • 2015-07-31 20:17:25
  • 5809

centos7防火墙导致java程序访问mongodb3.0.1时报错的问题

环境描述: 数据库:mongodb3.0.1 数据库系统:centos7,(虚拟机,最小安装) 数据库驱动:mongo-java-driver-3.0.0.jar 问题描述:shell...
  • hkh_1012
  • hkh_1012
  • 2015-04-06 09:05:15
  • 14940

org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017

com.mongodb.MongoSocketOpenException: Exception opening socket     at com.mongodb.connection.Socket...
  • dotphoenix
  • dotphoenix
  • 2017-11-02 22:17:31
  • 2441

一次MongoDB的Socket Exception

今天重新部署了一个项目,该项目启动的时候会访问MongoDB获取一些数据,一个蛮简单的项目,从前发布都没问题,这次启动的时候直接就是Socekt Exception:nested exception ...
  • java_4_ever
  • java_4_ever
  • 2015-07-27 22:31:05
  • 10342

【已解决】Exception java.net.ConnectException: Error opening socket to server Connection timed out.

使用db2 data studio链接服务器的数据库的时候显示如下错误:SQL4499N Ensure that the data server has been started. Check th...
  • voilethht
  • voilethht
  • 2015-08-25 14:35:50
  • 3559

spring boot 启动时候报错mongodb

com.mongodb.MongoSocketOpenException: Exception opening socket at com.mongodb.connection.SocketS...
  • a714115852
  • a714115852
  • 2017-06-08 14:59:00
  • 4615

MongoDB: No server chosen by ReadPreferenceServerSelector

关于此问题做项纪录:解决问题方法来自:MongoDB: No server chosen by ReadPreferenceServerSelector首先出问题的代码块 FindIterable b...
  • oMrApollo
  • oMrApollo
  • 2017-03-16 18:08:10
  • 1896

mongo read preference读优先级设置

mongo的查询过程,默认是primary模式,也就是从主节点查询。当然也有其他模式。主模式的有点就是,你总是能读到最新的数据,缺点在于,读写位于同一个节点,负载比较高。 mongo的查询几种模式,...
  • u011321811
  • u011321811
  • 2014-07-29 18:27:52
  • 2077

遇到的问题-----mongodb.MongoException$Network: can't call something

java操作mongoDB的开发,实时插入mongoDB。 一开始是正常的,运行一段时间后开始报错。 长时间运行测试遇到问题,报错信息如下: at com.mongodb.DBTCP...
  • q383965374
  • q383965374
  • 2013-09-05 11:53:25
  • 8577

Exception: Timed out after 30000ms(selenium超时)

问题: 当运行selenium server参数设置为:sel.wait_for_page_to_load("30000")时,运行后报错:Exception: Timed out after 30...
  • wudiyi815
  • wudiyi815
  • 2012-12-25 11:02:15
  • 4383
    公告栏
    个人资料
    专栏达人 持之以恒
    等级:
    访问量: 89万+
    积分: 1万+
    排名: 1873