简体   繁体   English

使用Tomcat的MongoDB Java驱动程序数据库连接池

[英]MongoDB Java Driver database connection pooling with Tomcat

According to the MongoDB Java driver documentation, database connection pooling is magically handled by the Mongo object. 根据MongoDB Java驱动程序文档,Mongo对象可以神奇地处理数据库连接池。

Does this mean it is safe to create an instance of a singleton object which connects to the MongoDB database in a servlet that will run when Tomcat starts and not worry about configuring database connection pooling in Tomcat via the context.xml? 这是否意味着创建单个对象的实例是安全的,该对象连接到将在Tomcat启动时运行的servlet中的MongoDB数据库,而不用担心通过context.xml在Tomcat中配置数据库连接池?

Is this the right way to think about it? 这是考虑它的正确方法吗? Am I misunderstanding some basic concept of Tomcat / database connection pooling in general? 我是否误解了Tomcat /数据库连接池的一些基本概念?

We've been using the Java drivers via the CFMongoDB project and we use it as you describe, but in a ColdFusion application rather then in Java. 我们一直在通过CFMongoDB项目使用Java驱动程序,我们按照您的描述使用它,但是在ColdFusion应用程序中而不是在Java中。 Same idea though: one object is created and we reuse it and that object maintains the one connection to the Mongo server. 同样的想法:创建了一个对象,我们重用它,该对象维护与Mongo服务器的一个连接。

You can create one Mongo Java instance and it will maintain an internal pool of connections (default size of 10) - to you it's hidden and you don't need to worry about it. 您可以创建一个Mongo Java实例,它将维护一个内部连接池(默认大小为10) - 对您来说它是隐藏的,您无需担心它。 The Mongo Java docs recommend this: Mongo Java文档推荐这个:

http://www.mongodb.org/display/DOCS/Java+Driver+Concurrency http://www.mongodb.org/display/DOCS/Java+Driver+Concurrency

We have it running in production now and there have been no issues. 我们现在已经在生产中运行,并且没有任何问题。 Multiple web request threads use the same Mongo instance and Mongo is quick enough to deal with this using it's internal pool (we're doing logging so it can write very fast!). 多个Web请求线程使用相同的Mongo实例,而Mongo使用它的内部池(我们正在进行日志记录以便它可以写得非常快!)来快速处理这个问题。

It is worth remembering to call close() on any instances that you are finished with - this will stop connections getting used up on the Mongo server over time: 值得记住在你完成的任何实例上调用close() - 这将阻止连接在Mongo服务器上随着时间的推移而消耗殆尽:

http://api.mongodb.org/java/2.5-pre-/com/mongodb/Mongo.html#close () http://api.mongodb.org/java/2.5-pre-/com/mongodb/Mongo.html#close ()

So in summary, don't worry about configuring Tomcat. 总而言之,不要担心配置Tomcat。

Hope that helps! 希望有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM