gpt4 book ai didi

java - 如何访问Database类实例?

转载 作者:行者123 更新时间:2023-12-01 21:25:01 24 4
gpt4 key购买 nike

我有一个使用 MongoDB 的 JSP Web 应用程序,它在多个页面上执行不同的操作,例如搜索、更新、删除。那么我可以为所有页​​面创建一个 MongoClient 实例,还是创建一个实例并在必要时调用它们?就效率和速度而言,哪个更有效?

最佳答案

官方文档中http://mongodb.github.io/mongo-java-driver/3.2/driver/getting-started/quick-tour/

据说:

The MongoClient instance actually represents a pool of connections to the database; you will only need one instance of class MongoClient even with multiple threads.

值得注意的是:

Typically you only create one MongoClient instance for a given database cluster and use it across your application. When creating multiple instances:

  • All resource usage limits (max connections, etc) apply per MongoClient instance

  • To dispose of an instance, make sure you call MongoClient.close() to clean up resources

希望有帮助!

关于java - 如何访问Database类实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38309473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com