Thread synchronisation problem in static methods

Discussions

EJB programming & troubleshooting: Thread synchronisation problem in static methods

  1. Please see the static method below which is shared between multiple threads getting the connection. Does application may have synchronisation problems?

    <code>
    public static Connection getConnection() {
        try {
            Logger.print("method start");
            Properties parms = Properties();
            parms.setProperty(INITIAL_CONTEXT_FACTORY, "INITIAL_CONTEXT_FACTORY");
            Context ctx = new InitialContext(parms);
            DataSource ds = (DataSource) ctx.lookup("DATA_SOURCE_NAME");
            Connection conn = null;
            try {
                conn = ds.getConnection("USER", "PASSWORD");
            } catch (Exception e) {
                conn = ds.getConnection();
            }
            Logger.print("method end");
            return conn;
        } catch (Exception e) {
            Logger.print(e);
            throw e;
        }
    }
    </code>

    Thanks in advance
  2. Reply[ Go to top ]

    Mohsin,

    Looks like this method should work normally unless you start changing the values of the global variables inside it.

    What stumbles me, is the following place in your code:

    try {
                conn = ds.getConnection("USER", "PASSWORD");
            } catch (Exception e) {
                conn = ds.getConnection();
         }

    Why are you trying to get the connection twice?

    Regards,
    Alex
  3. Actually we are using IBM WebSphere 4.0 and the application must be compatible with WebSphere 3.02 or 3.5. In WebSphere 4.0 the db uid/pwd are supplied at the time of datasource configuration. So there is no need to supply the user id and password when geeting a connection through datasource.

    Anyway thanks I just need to know if there is any problem with thread synchronisation, If there are any chances that two thread may get the same instance of "conn" object. Also thanks to Brian :)
  4. For efficiency, you should look up the Datasource only once and use it to get a new connection for each of your thread. Use Singleton pattern like this and change your getConnection to non-static

    class ConnectionHandler {



    }
  5. For efficiency, you should look up the Datasource only once and use it to get a new connection for each of your thread. Use Singleton pattern like this and change your getConnection to non-static

    class ConnectionHandler {
      
      private ConnectionHandler=null;

      public static ConnectionHandler getInstance() {

      }


    }
  6. Sorry, previuos post was incomplete

    For efficiency, you should look up the Datasource only once and use it to get a new connection for each of your thread. Use Singleton pattern like this and change your getConnection to non-static

    class ConnectionHandler {
      
      private ConnectionHandler handler = null;

      public static ConnectionHandler getInstance() {
           if (handler == null)
              handler = new ConnectionHandler();
           return handler;
      }

      private ConnectionHandler() {
            Properties parms = Properties();
            parms.setProperty(INITIAL_CONTEXT_FACTORY, "INITIAL_CONTEXT_FACTORY");
            Context ctx = new InitialContext(parms);
            DataSource ds = (DataSource) ctx.lookup("DATA_SOURCE_NAME");
      }
      public Connection getConnection() {
        //Your codes after ds ......
        return conn;
      }

    }

    Your thread will use it like this:
    Connection con = connectionHandler.getInstance().getConnection();
  7. HERE's THE CORRECT CODES: ds should be member variable

    class ConnectionHandler {
       
      private ConnectionHandler handler = null;
      private DataSource ds;

      public static ConnectionHandler getInstance() {
           if (handler == null)
              handler = new ConnectionHandler();
           return handler;
      }

      private ConnectionHandler() {
            Properties parms = Properties();
            parms.setProperty(INITIAL_CONTEXT_FACTORY, "INITIAL_CONTEXT_FACTORY");
            Context ctx = new InitialContext(parms);
            ds = (DataSource) ctx.lookup("DATA_SOURCE_NAME");
      }
      public Connection getConnection() {
        //Your codes after ds ......
        conn = ds.getConnection(....);
        return conn;
      }

    }