且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-09-21 16:06:16

我们一直在使用Java驱动程序 CFMongoDB 项目,我们按照您的描述使用它,但是在ColdFusion应用程序中而不是在Java中。同样的想法:创建了一个对象,我们重用它,该对象维护与Mongo服务器的一个连接。

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. Same idea though: one object is created and we reuse it and that object maintains the one connection to the Mongo server.

你可以创建一个Mongo Java实例,它将维护一个内部连接池(默认大小为10) - 对你来说它是隐藏的,你不需要担心关于它。 Mongo Java文档推荐这个:

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. The Mongo Java docs recommend this:

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

我们现在正在生产中运行并且没有任何问题。多个Web请求线程使用相同的Mongo实例,而Mongo使用它的内部池(我们正在进行日志记录以便它可以写得非常快!)来快速处理这个问题。

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!).

值得记住在你完成的任何实例上调用 close() - 这将停止连接随着时间的推移在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:

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

总而言之,不要担心配置Tomcat。

So in summary, don't worry about configuring Tomcat.

希望有所帮助!