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 8329915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:57:08+00:00 2026-06-09T01:57:08+00:00

Currently I am using mongodb with java. I don’t want to reopen my connection

  • 0

Currently I am using mongodb with java. I don’t want to reopen my connection to the datastore every time, so I thought I should make a static variable:

package Config;

import java.net.UnknownHostException;

import com.google.code.morphia.*;

import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class Config {

    static String dbUrl = "url";
    static int dbPort = portnumber;
    static String username = "user";
    static String password = "pw";
    static String dbName = "dbname";
    public static Datastore ds;

    public static Datastore getDatastore() throws UnknownHostException,
            MongoException {
        if (ds == null) {
            ds = connect();
        }
        return ds;
    }

    public static Datastore connect() throws UnknownHostException,
            MongoException {
        Mongo m = new Mongo(dbUrl, dbPort);
        Datastore ds = new Morphia().createDatastore(m, dbName);
        boolean con = ds.getDB().authenticate(username, password.toCharArray());// todo
        return ds;
    }
}

I would use it like this:

Config.getDatastore().doSomthing();

Beside those un-handled exceptions, is this the correct way of using a static variable?

  • 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-06-09T01:57:09+00:00Added an answer on June 9, 2026 at 1:57 am

    The given Config seems to indicate that this class should be a singleton, and if so, then using static variables for all members is fine. Will you have multiple Configs, or only one?

    If Config isn’t intended to be a singleton, then static isn’t required in this case, or if so, only if it’s ok to share your Datastore with every instance of your Config class. You’ll achieve your goals with a simple – non-static – member variable, and you can still have, e.g. a getDataStore method. But each Config class will have it’s own Datastore

    Only use static if you want to also share that Datastore among multiple instances of your Config class.

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

Sidebar

Related Questions

I'm currently trying to make a register form using mongoDB and nodeJS - I've
I am using mongoDB currently, and I am wondering what measures we should take
I'm currently using MongoDB to record application logs, and while I'm quite happy with
I am currently working on a rails app where we are using mongoid/mongoDB on
I have setup hadoop on top of mongodb using hadoop-mongodb driver. Currently I can
I am developing a website using Codeigniter and MongoDB (Alex Bilbie library). Currently there
I'm designing an application that processes RSS feeds using MongoDB. Currently my collections are
I am currently using MongoDB's MapReduce to generate hourly ad view counts like this:
I'm currently trying to compile a PHP driver for MongoDB using Visual Studio 2008
I am currently working on a project with 3 friends using nodeJs, expressJs, MongoDB,

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.