使用Tomcat的MongoDB Java驱动程序数据库连接池 [英] MongoDB Java Driver database connection pooling with Tomcat

查看:699
本文介绍了使用Tomcat的MongoDB Java驱动程序数据库连接池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据MongoDB Java驱动程序文档,Mongo对象可以神奇地处理数据库连接池。

According to the MongoDB Java driver documentation, database connection pooling is magically handled by the Mongo object.

这是否意味着创建单例实例是安全的连接到将在Tomcat启动时运行的servlet中的MongoDB数据库的对象,而不用担心通过context.xml在Tomcat中配置数据库连接池?

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 /数据库连接池的一些基本概念?

Is this the right way to think about it? Am I misunderstanding some basic concept of Tomcat / database connection pooling in general?

推荐答案

我们一直在使用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.

希望有所帮助!

这篇关于使用Tomcat的MongoDB Java驱动程序数据库连接池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆