简体   繁体   English

如何在Netbeans中安装和连接MongoDb?

[英]How to install and connect MongoDb in Netbeans?

I'm using the MongoDB Java driver in a Java web app. 我在Java Web应用程序中使用MongoDB Java驱动程序。 Whenever I'm doing a query, or can I keep it open and run other queries on it and close it when my application ends? 每当执行查询时,还是可以保持它打开并对其运行其他查询,并在应用程序结束时将其关闭?

When you create an instance of class MongoClient. 创建类MongoClient的实例时。 Your connection will keep by default. 默认情况下,您的连接将保持不变。 It only close when you use method close() to close it. 仅当使用方法close()将其关闭时,它才会关闭。 If you want to open and run other query on it (all query will reuse this connection), you can use the pattern singleton to create only one instance of MongoClient. 如果要在其上打开并运行其他查询(所有查询将重用此连接),则可以使用模式单例仅创建一个MongoClient实例。 This pattern will re-create your connection if it is null. 如果此模式为null,则它将重新创建您的连接。

You should create DAL object which will handle low level Database CRUD operation, And this object will be singleton as "Piu" suggested. 您应该创建DAL对象,该对象将处理低级数据库CRUD操作,并且该对象将单例显示为“ Piu”。 All the methods inside of this object will handle creating/opening/closing connection (on application Exit). 该对象内部的所有方法将处理创建/打开/关闭连接(在应用程序退出时)。 This way you will in future you will be free to change to other DB solution and change only the DAL class. 这样,将来您将可以自由地更改为其他数据库解决方案,而仅更改DAL类。

On your question, in my opinion you should leave the connection open if you will communicate with the DB a lot. 关于您的问题,我认为,如果您将与数据库进行大量交流,则应保持连接打开。 But if you will need to access to DB once a you start the application and on exit and the application execution duration is not short it is safe to close and reopen the connection. 但是,如果您需要一次访问数据库,则启动应用程序,然后退出,并且应用程序执行时间不短,可以安全地关闭并重新打开连接。

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

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