6 Replies Latest reply: Jul 15, 2010 3:29 PM by forumKid2 RSS

    Synchronization and multi threading question

    forumKid2
      I have a singleton connection class that uses lazy initialization to create the instance. Here is a snippet:
           public static synchronized DataConnection getInstance() {
                if (instance == null) {
                     instance = new DataConnection();
                }
                return instance;
           }
      I submit multiple threads using ExecutorService and these submitted threads make calls to the database. Hence if I have 5 threads they all run at the same time grabbing a connection, running a query, then returning the connection back to the datasource pool.

      My question is this. Is there any synchronization issues that I have to be aware of when getting and returning a connection? Currently when I get a connection I do this: (Should I have public synchronized Connection getConnection() instead of just public Connection getConnection()
           public Connection getConnection() {
      
                try {
      
                     conn = dataSource.getConnection();
      
                     logger.info("Opened database connection " + conn);
      
                } catch (SQLException e) {
                     logger.error(DATAS_NAME + e.getMessage());
                }
      
                return conn;
           }
        • 1. Re: Synchronization and multi threading question
          796440
          1. You don't need lazy instantiation. Just eagerly instantiate it and save yourself some headaches.

          2. If you're going to lazily instantiate, then:

          2a. You can simply synchronize the getInstanceMethod. I can almost guarantee you the synchronization overhead will not hurt your perfrormance.

          OR

          2b. You can use a nested class to hold the instance. Google for java nested class singleton or something like that, but I'm not totally convinced it's guaranteed to lazy load.

          OR

          2c. You can use an enum for the singleton. Google for that if you're not sure how. I don't like this approach, as I consider it a misuse of enums.

          And finally,

          3, 4, 5, ... Infinity. You don't need lazy instantiation. Just eagerly instantiate and be done with it.
          • 2. Re: Synchronization and multi threading question
            gimbal2
            is it even necessary to synchronize access to a datasource I wonder? (at least I'm assuming there is a datasource in play here seeing the 'dataSource' variable)?

            I mean I don't see any reference to datasources needing to be thread safe in the javadocs, but it seems quite useless to me if a datasource is not thread safe by design.
            • 3. Re: Synchronization and multi threading question
              forumKid2
              Thank you for making it crystal clear. I really appreciate it and it makes sense now.
              • 4. Re: Synchronization and multi threading question
                796440
                Note that I may have misunderstood my question. I thought you were asking about thread-safety of lazy instantiation. However, it appears you may have been asking about thread safety of using that singleton after it's instantiated, or something like that. If that's the case, I don't really know what your question is, so my initial answer, while still valid advice, may not be relevant to your actual question.
                • 5. Re: Synchronization and multi threading question
                  forumKid2
                  Yes my question is about connection management and multi threading. I currently am using the lazy initialization of a singleton. The problem is that I have multithreads using Future and ExecutorService. The first two threads grab the same connection object, so I am having some sort of synchronization issue. Here is a stack trace of my log showing my 2nd thread grabbing the same connection as the first. The other threads grab a new connection object.

                  2010-07-15 04:08:36 INFO [pool-2-thread-1] - SProcedure.build -> Building stored procedures for output
                  2010-07-15 04:08:36 INFO [pool-2-thread-2] - SProcedure.build -> Building stored procedures for output
                  2010-07-15 04:08:36 INFO [pool-2-thread-1] - DataBConnection.getConnection -> Opened database connection weblogic.jdbc.wrapper.PoolConnection_oracle_jdbc_driver_T4CConnection@c
                  2010-07-15 04:08:36 INFO [pool-2-thread-2] - DataBConnection.getConnection -> Opened database connection weblogic.jdbc.wrapper.PoolConnection_oracle_jdbc_driver_T4CConnection@c
                  • 6. Re: Synchronization and multi threading question
                    forumKid2
                    I think I fixed the issue I was having. My getConnection() method must have had two threads in it and grabbed the same object. I put the synchronized keyword on my getConnection() method and it seems to have solved the issue.