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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:18:19+00:00 2026-05-23T13:18:19+00:00

I wrote a singleton class for obtaining a database connection. Now my question is

  • 0

I wrote a singleton class for obtaining a database connection.

Now my question is this: assume that there are 100 users accessing the application. If one user closes the connection, for the other 99 users will the connection be closed or not?

This is my sample program which uses a singleton class for getting a database connection:

public class GetConnection {

    private GetConnection() { }

    public Connection getConnection() {
        Context ctx = new InitialContext();
        DataSource ds = ctx.lookup("jndifordbconc");
        Connection con = ds.getConnection();
        return con;
    }

    public static  GetConnection   getInstancetoGetConnection () {
        // which gives GetConnection class instance to call getConnection() on this .
    }
}

Please guide me.

  • 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-23T13:18:20+00:00Added an answer on May 23, 2026 at 1:18 pm

    As long as you don’t return the same Connection instance on getConnection() call, then there’s nothing to worry about. Every caller will then get its own instance. As far now you’re creating a brand new connection on every getConnection() call and thus not returning some static or instance variable. So it’s safe.

    However, this approach is clumsy. It doesn’t need to be a singleton. A helper/utility class is also perfectly fine. Or if you want a bit more abstraction, a connection manager returned by an abstract factory. I’d only change it to obtain the datasource just once during class initialization instead of everytime in getConnection(). It’s the same instance everytime anyway. Keep it cheap. Here’s a basic kickoff example:

    public class Database {
    
        private static DataSource dataSource;
    
        static {
            try {
                dataSource = new InitialContext().lookup("jndifordbconc");
            }
            catch (NamingException e) { 
                throw new ExceptionInInitializerError("'jndifordbconc' not found in JNDI", e);
            }
        }
    
        public static Connection getConnection() {
            return dataSource.getConnection();
        }
    
    }
    

    which is to be used as follows according the normal JDBC idiom.

    public List<Entity> list() throws SQLException {
        List<Entity> entities = new ArrayList<Entity>();
    
        try (
            Connection connection = Database.getConnection();
            PreparedStatement statement = connection.prepareStatement("SELECT id, foo, bar FROM entity");
            ResultSet resultSet = statement.executeQuery();
        ) {
            while (resultSet.next()) {
                Entity entity = new Entity();
                entity.setId(resultSet.getLong("id"));
                entity.setFoo(resultSet.getString("foo"));
                entity.setBar(resultSet.getString("bar"));
                entities.add(entity);
            }
        }
    
        return entities;
    }
    

    See also:

    • Is it safe to use a static java.sql.Connection instance in a multithreaded system?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now I'm having an issue with a Singleton that I just wrote for
I created a Singleton class in c#, with a public property that I want
The classic of writing a singleton in java is like this: public class SingletonObject
For my project I wrote a small config class that loads its data from
I wrote a singleton class to keep track of some variables across my application.
I wrote a singleton class to keep track of some variables across my application.
I've got a singleton class like this. class UserInteraction { private CustomerInformation _loginDetails; private
I write a singleton c++ in the follow way: class A { private: static
I wrote a windows service using VB that read some legacy data from Visual
I wrote myself a little downloading application so that I could easily grab a

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.