Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7410725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:16:39+00:00 2026-05-29T06:16:39+00:00

I don’t have any prior experience with database, but I have to use it

  • 0

I don’t have any prior experience with database, but I have to use it for a app that I’m building, and I choose MongoDB.

Now everything is working, but I’m creating a new Mongo object every time I have to make an request to the database, and I understand that’s wrong.

So I want ask and learn what is the correct way to do this, I have some code like the following in a class called MongoDbConnnection:

public class MongoDbConnection {
    private static Mongo _mongo;
        public static Mongo getMongo()
        {
            if (_mongo == null) {
            try {
                    _mongo = new Mongo();
                } catch (UnknownHostException e) {
                    _mongo = null;
                    e.printStackTrace();
                } catch (MongoException e) {
                    _mongo = null;
                    e.printStackTrace();
                }
            }
        return _mongo;
    }
}

So that every time I need to do a query, I do Mongo m = MongoDbConnnection.getMongo(); and proceed forward.

Is this correct?

I also confuse about when to close the connection, as I have another class that extends HttpServlet and will respond GET, every time someone request that url I will have to query the database and return the information. If I close right after I respond to one request, what if other people request it again right after that. Then I have to create a new Mongo object again?

In other words, I know Mongo instance maintain a connection poll, so when I call m.close(), am I just replace the connection I used, or am I setting object m to null?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:16:40+00:00Added an answer on May 29, 2026 at 6:16 am

    Yes, you are mostly getting a connection from the pool of connection maintained for you with:

    Mongo m = new Mongo();
    DB db = m.getDB( "mydb" );
    

    You only need one Mongo object, and it is thread safe.

    I have not looked into the implementation in details, but you have to call

    db.close() from your code. 
    

    This will simply returns the connection to the pool and not close it.
    If you forget to do it, I suppose it is going to take time, before the Mongo object realize the connection is not used anymore.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't have any experience in updating a Rails app and when I google
DON'T ASK WHY but... I have a regex that needs to be case insensitive
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't be afraid to use any technical jargon or low-level explanations for things, please.
Don't ask why but I have the requirement to draw a border around certain
Don't ask why, but is there any way to suppress a failed linking error?
I don't like Jackson. I want to use ajax but with Google Gson. So
I don't have much PHP experience and I want to know how to best
Don't ask me why but i can't use this method because I need to
Don't yell at me! i have seen many threads claiming that HTML cannot be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.