Performance and scalability: InitialContext and DataSource creating

  1. InitialContext and DataSource creating (3 messages)

    Hi everyone -

    I'm running weblogic6.1 sp1. We're creating an InitialContext, looking up a DataSource, and then getting a connection from the data source - pretty standard stuff.

    Now, I was always under the impression that creating an initial context and doing a JNDI lookup were pretty expensive operations. However, in doing some timings, it looks like weblogic takes less than .10s to instantiate and InitialContext and return a DataSource, and more than 1s to return a connection.

    Doesn't this seem a little strange?

    Note: When I instantiate an InitialContext - I'm just calling "new InitialContext()".

  2. Hi Scott,

    According to my experience with WebLogic, the "new InitialContext()" operation should not take longer than 60 to 100 milliseconds - and about the same for looking up the DataSource.

    Usually the DataSource object takes about .7 second (650 to 750 milliseconds to be exact) to return connection for the first time (this includes new InitialContext(), lookup() and ds.getConnection()). That's a awfully large amount of time. After experiencing the above, I did the following trick (might not be a good programming practice, because you can easily be a victim of lost connection if you do not close() and refresh it properly):
    - I created a DataSource instance as the private static member of the entity bean and a method to return a connection on this DataSource. Only the first time the DataSource is looked up (new InitialContext() and lookup()) and then it is assigned to the private static member of the bean. The next time (if ds != null) the method directly accesses the DataSource already lookedup as for example: (UserBean.staticDataSource).getConnection().
    This reduced the total timing between the clients connect() call and returning the connection to about 250 to 300 milliseconds.

    - KK
  3. Is is advisable to cache the "new IniialConext()" object and then using this one cached object for looking up Home Objects and DataSource Objects across the entire application.
  4. Which part of using DataSource takes up to 0.7 seconds? My understanding of your finding is:

    1. Context ctx = new InitContext();
    2. DataSource ds = (DataSource) ctx.lookup("MyDataSource");
    3. Connection conn = ds.getConnection();

    (omitted the try/catch stuff for clarity)

    Step 1 takes 60 - 100 millisec
    Step 2 also takes 60 - 100 millisec
    Step 3 takes 700 millisec?
    Or Steps 1+2+3 take 700 millisec?

    Regardless, the ds.getConnection() step seems to be the most time-consuming part. I am curious how can you reduce the total time to 250 to 300 millisec by using a static reference to the DataSource object?

    Anyway, thanks very much for the infomation.