当运行太多使用我的本地测试数据库的JUnit测试时,我遇到了MongoDb的问题.当我一个接一个地运行所有测试时,一切正常,每次测试都通过.但是当我试图“运行所有测试”时,在第80次测试后的某个地方我失去了与Mongo的连接,...
当运行太多使用我的本地测试数据库的JUnit测试时,我遇到了MongoDb的问题.
当我一个接一个地运行所有测试时,一切正常,每次测试都通过.
但是当我试图“运行所有测试”时,在第80次测试后的某个地方我失去了与Mongo的连接,所有下一次测试都失败了.
这很奇怪,因为我理解并看到“运行所有测试”只是让所有测试连续运行.
我在每个测试的@After中删除我的数据库,所以我认为可能为每个测试创建自己的,唯一的数据库可以解决问题(在我使用单个数据库之前).但事实并非如此.经过80次测试后,我仍然失去了连接(每次都不同).
也许司机没有足够的时间在某个时刻得到Mongo的回应而导致失败?
我使用2.11.3 Mongo Java驱动程序.
需要帮忙.
谢谢.
这是错误堆栈:
Sep 13, 2013 5:32:07 PM com.mongodb.DBTCPConnector initDirectConnection
WARNING: Exception executing isMaster command on /127.0.0.1:27017
java.io.EOFException
at org.bson.io.Bits.readFully(Bits.java:48)
at org.bson.io.Bits.readFully(Bits.java:33)
at org.bson.io.Bits.readFully(Bits.java:28)
at com.mongodb.Response.<init>(Response.java:40)
at com.mongodb.DBPort.go(DBPort.java:142)
at com.mongodb.DBPort.go(DBPort.java:106)
at com.mongodb.DBPort.findOne(DBPort.java:162)
at com.mongodb.DBPort.runCommand(DBPort.java:170)
at com.mongodb.DBTCPConnector.initDirectConnection(DBTCPConnector.java:547)
at com.mongodb.DBTCPConnector.isMongosConnection(DBTCPConnector.java:334)
at com.mongodb.Mongo.isMongosConnection(Mongo.java:618)
at com.mongodb.DB.wrapCommand(DB.java:282)
at com.mongodb.DB.command(DB.java:260)
at com.mongodb.DB.command(DB.java:244)
at com.mongodb.DB.command(DB.java:301)
at com.mongodb.DB.command(DB.java:199)
at com.mongodb.DB.dropDatabase(DB.java:557)
at com.*******.dbconnection.mongodb.BaseMongodbTest.tearDown(BaseMongodbTest.java:102)
at com.*******.rpcserver.methods.BaseTestClient.tearDown(BaseTestClient.java:57)
at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:36)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:24)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
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:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
com.mongodb.MongoException$Network: Read operation to server /127.0.0.1:27017 failed on database test_mydb_fb19fd07-6e4f-4
at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:253)
at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:216)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:288)
at com.mongodb.DB.command(DB.java:262)
at com.mongodb.DB.command(DB.java:244)
at com.mongodb.DB.command(DB.java:301)
at com.mongodb.DB.command(DB.java:199)
at com.mongodb.DB.dropDatabase(DB.java:557)
at com.*******.dbconnection.mongodb.BaseMongodbTest.tearDown(BaseMongodbTest.java:102)
at com.*******.rpcserver.methods.BaseTestClient.tearDown(BaseTestClient.java:57)
at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:36)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:24)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.io.EOFException
at org.bson.io.Bits.readFully(Bits.java:48)
at org.bson.io.Bits.readFully(Bits.java:33)
at org.bson.io.Bits.readFully(Bits.java:28)
at com.mongodb.Response.<init>(Response.java:40)
at com.mongodb.DBPort.go(DBPort.java:142)
at com.mongodb.DBPort.call(DBPort.java:92)
at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:244)
... 42 more
解决方法:
似乎我发现了问题所在.
我们的测试客户端出现了一个愚蠢的错误:为每个测试用例创建了新的Mongo对象!
命令
db.serverStatus().connections
向我们显示可用连接数约为200,但每个新的Mongo实例创建新的10个(默认情况下)连接.所以我只是超出了日志中显示的限制(例如,您可以通过执行mongo –eval =“printjson(db.adminCommand({getLog:’global’}))来阅读它们.”)
解决方案是创建工厂类,它保留在每个测试用例中使用的Mongo对象的单个实例(我还在这里初始化DB和MongoClient实例,并建议您在需要时也这样做).
我强烈建议阅读这个主题,它解决了类似的问题(并帮助了我很多!):
Mongo opens too many connections
感谢@RobMoore和@interlude提供快速回复并愿意提供帮助!
本文标题为:java – 运行太多测试时丢失与mongoDB的连接
基础教程推荐
- java – 在Oracle ucp中找不到oracle.ucp.jdbc.PoolDataSourceFactory 2023-11-07
- Spring的同一个服务会加载多次的问题分析及解决方法 2023-06-16
- 一文带你了解Spring中@Enable开头注解的使用 2023-05-19
- 关于maven:pom文件的使用解析 2023-04-18
- 关于使用jpa聚合函数遇到的问题 2022-10-24
- java – 如何使用Vagrant连接两个VM(postgreSQL和tomee)? 2023-11-03
- java – SQLException:没有为url = jdbc:derby找到合适的驱动程序 2023-11-06
- Java List的get方法 2023-10-08
- 解决在微服务环境下远程调用feign和异步线程存在请求数据丢失问题 2022-11-11
- Java C++题解leetcode消失的两个数字实例 2023-06-02