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

  • Home
  • SEARCH
  • 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 7861541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:46:33+00:00 2026-06-02T22:46:33+00:00

I am using JDBC connection pooling in Tomcat. To retrieve connections I have defined

  • 0

I am using JDBC connection pooling in Tomcat. To retrieve connections I have defined a connection factory as below:

public class ConnectionManager {

   // reference to the ConnectionManager
   private static ConnectionManager instance = null;
   // Connection to MySQL database
   private Connection connect = null;

   private static DataSource ds = null;
   // Logger
   public static final Logger logger = Logger
         .getLogger(ConnectionManager.class);

   static {

      try {
         Context initCtx = new InitialContext();
         Context envCtx = (Context) initCtx.lookup("java:comp/env");
         ds = (DataSource) envCtx.lookup("jdbc/ConnectionManager");
      } catch (NamingException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }

   /**
    * Private Constructor .. since its a singleton
    * 
    */
   private ConnectionManager() {

   }

   public static ConnectionManager getInstance() {
      if (instance == null) {
         instance = new ConnectionManager();
      }
      return instance;
   }

   public Connection getDbConnection() {
      Connection conn = null;

      try {
         synchronized (DataSource.class) {

            conn = ds.getConnection();
         }
      } catch (SQLException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }

      return conn;
   }

   public void closeDbConnection() throws SQLException {
      conn.close();
   }
}

Now I see that my code always gets stuck at conn = ds.getConnection(); line. Please let me know what I am doing wrong. From my DAO methods I am using the following to get connection: conn = ds.getConnection();

Clearly its a multi-threading issue. What should I do?

  • 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-02T22:46:35+00:00Added an answer on June 2, 2026 at 10:46 pm

    Most of your class seems geared around retrieving the JNDI datasource and using it to create connections. Not necessarily a bad idea but in this case you have introduced some bugs into your program with the additional complexity.

    First off, your singleton is not a singleton. Your are not synchronizing the getInstance method so its possible to multiple threads to invoke this method at the same time. The best method in Java (unfortunately) for implementing singletons is via an enum:

    public enum ConnectionManager {
        INSTANCE;
    }
    

    Your second significant issue is that you are synchronizing on a class that you don’t explicitly control. There is nothing preventing third party JARs or even other classes in your own application from synchronizing on the DataSource class, making it a rife target for deadlocking issues. I would take out all the superfluous methods from the class and remove the synchronize block:

    public enum ConnectionManager {
        INSTANCE;
    
        private DataSource ds = null;
    
        ConnectionManager() {
          try {
             final Context initCtx = new InitialContext();
             final Context envCtx = (Context) initCtx.lookup("java:comp/env");
             ds = (DataSource) envCtx.lookup("jdbc/ConnectionManager");
          } catch (NamingException e) {
             e.printStackTrace();
          }
        }
    
       public Connection getConnection() throws SQLException {
          if(ds == null) return null;
    
          return ds.getConnection();
       }
    }
    

    Now, most datasource implementations are thread safe in my experience, so the above code should work most of the time. But, we shouldn’t rely on implementations we cannot control, so lets add a safe synchronization to the code, like so:

    public enum ConnectionManager {
        INSTANCE;
    
        private DataSource ds = null;
        private Lock connectionLock = new ReentrantLock();
    
        ConnectionManager() {
          try {
             final Context initCtx = new InitialContext();
             final Context envCtx = (Context) initCtx.lookup("java:comp/env");
             ds = (DataSource) envCtx.lookup("jdbc/ConnectionManager");
          } catch (NamingException e) {
             e.printStackTrace();
          }
        }
    
       public Connection getConnection() throws SQLException {
          if(ds == null) return null;
    
          Connection conn = null;
          connectionLock.lock();
          try {
              conn = ds.getConnection();
          } finally {
              connectionLock.unlock();
          }
    
          return conn;
       }
    }
    

    You don’t have to add wrapper methods to close the connection, that is the responsibility of the calling code. Good luck.

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

Sidebar

Related Questions

I'm using dbcp connection pooling in tomcat (version 7) and I have a connection
I have a web app on Tomcat, which handles DB connection pooling, and using
I have a main Servlet that processes post/get requests. I am using connection pooling
I am trying to make connections to my database using connection pooling library: DBPool
Currently I'm using a separate DBConnectionManager class to handle my connection pooling, but I
The JDBC 3.0 spec talks about Connection (and Prepared Statement) pooling. We have several
I'm using JDBC for very simple database connectivity. I have created my connection/statement and
I'm using JDBC Connection pooling with mysql. This is my code private static DataSource
I am using JDBC to connect to a MySQL server (no connection pooling I
I'm using a vendor API to obtain a JDBC connection to the application's database.

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.