java – 运行太多测试时丢失与mongoDB的连接

当运行太多使用我的本地测试数据库的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提供快速回复并愿意提供帮助!

点赞