我有一个使用 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/