Best place to look up JNDI resource, init db connection, in servlet.

Discussion in 'Java' started by jason.cipriani@gmail.com, Jul 6, 2008.

  1. Guest

    I'm new to Java servlet development. I'm accessing a database via a
    JNDI DataSource resource that I've defined in my servlet's context. I
    have gotten it working using some code like this:

    InitialContext ctx = new InitialContext();
    DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/
    mssql");
    Connection conn = ds.getConnection();
    PreparedStatement pstmt = conn.prepareStatement(stmt);
    ResultSet rs = pstmt.executeQuery();

    My question is, where is the most appropriate place to:

    1. Construct a new InitialContext,
    2. Look up the DataSource resource (call ctx.lookup),
    3. Initialize db connection (call ds.getConnection).

    And also I'm wondering about the most appropriate place to store the
    InitialContext, DataSource, and Connection. My options, for each of
    the above 3, are:

    A. Init in servlet class constructor. Store item as class member.
    B. Init when creating a new HTTP session. Store item as session
    attribute.
    C. Init as needed in servlet class methods. Store item as local
    variable.
    D. Maybe something else I'm not thinking of.

    Can somebody help me match up my 1,2,3's with my A,B,C's?

    Also when should I close the database connection? Do I even need to
    worry about it, or will it automagically happen for me? I am using
    connection pooling.

    Thanks a lot,
    Jason
    , Jul 6, 2008
    #1
    1. Advertising

  2. Guest

    Re: Best place to look up JNDI resource, init db connection, inservlet.

    Lew, thanks a lot for the detailed and informative explanation. That
    clears a lot of stuff up.

    On Jul 6, 2:23 pm, Lew <> wrote:
    > I would only store the DataSource.  Abandon the InitialContext after it's
    > yielded the DataSource to you.  Use the connection only in the moment of
    > connection; don't keep it around.


    All right, so, what if the web application also consists of a bunch of
    JSP pages? I can look up the DataSource in the servlet's init() (I
    forgot about init), for the servlet, but for the JSP pages should I
    just look it up in an InitialContext every time a page loads? Or does
    it make sense to "cache" the looked-up DataSource in a session
    attribute or something?

    > >   B. Init when creating a new HTTP session. Store item as session
    > > attribute.

    >
    > Is the DataSource going to differ between sessions?


    I don't think so...? I don't really know how it works. The DataSource
    will represent the same source of data for every session, if that's
    what you mean -- I'm using Tomcat 6 and this DataSource is a JNDI
    resource that I've defined in the web application's context.xml. Does
    InitialContext.lookup() return the same instance every time (does that
    even matter with DataSources)?

    Thanks again,
    Jason
    , Jul 6, 2008
    #2
    1. Advertising

  3. Guest

    Re: Best place to look up JNDI resource, init db connection, inservlet.

    Lew, thanks a lot for your insight below. You've cleared up all my
    issues with this.

    Jason

    On Jul 6, 4:21 pm, Lew <> wrote:
    > wrote:
    > > Lew, thanks a lot for the detailed and informative explanation. That
    > > clears a lot of stuff up.

    >
    > > On Jul 6, 2:23 pm, Lew <> wrote:
    > >> I would only store the DataSource.  Abandon the InitialContext after it's
    > >> yielded the DataSource to you.  Use the connection only in the moment of
    > >> connection; don't keep it around.

    >
    > > All right, so, what if the web application also consists of a bunch of
    > > JSP pages? I can look up the DataSource in the servlet's init() (I
    > > forgot about init), for the servlet, but for the JSP pages should I
    > > just look it up in an InitialContext every time a page loads? Or does
    > > it make sense to "cache" the looked-up DataSource in a session
    > > attribute or something?

    >
    > If the DataSource is stored in the application context, it's easy for the JSP
    > to retrieve it using EL (Expression Language) or <jsp:useBean>.
    >
    > You will not need the InitialContext to retrieve the DataSource if you keep
    > the DataSource in the context.
    >
    > >>>   B. Init when creating a new HTTP session. Store item as session
    > >>> attribute.
    > >> Is the DataSource going to differ between sessions?

    >
    > > I don't think so...? I don't really know how it works. The DataSource

    >
    > Then the session is too narrow a lifetime.  If the DataSource were to differ
    > between sessions, then session lifetime is appropriate.  I suggest that for
    > your need that you store the DataSource in the application context.
    >
    > > will represent the same source of data for every session, if that's
    > > what you mean -- I'm using Tomcat 6 and this DataSource is a JNDI

    >
    > That is what I meant.
    >
    > > resource that I've defined in the web application's context.xml. Does
    > > InitialContext.lookup() return the same instance every time (does that
    > > even matter with DataSources)?

    >
    > I don't know if it returns the same instance every time, but it doesn't matter.
    >
    > Just store the DataSource and you won't need to look it up each time.
    >
    > Not that it is especially harmful if you do.
    >
    > The principle I'm following is to figure out what the lifetime of the object
    > is.  The object that you need is the DataSource.  Its lifetime is for the life
    > of the application, thus I recommend to store it in the application context.
    > The InitialContext is only needed at the beginning to obtain the DataSource.
    > Its lifetime is brief, only long enough to get at the DataSource, thus I
    > recommend that it be a local variable.  Connection is needed only during a
    > query or transaction - local variable.
    >
    > This is not the only valid analysis.
    , Jul 6, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tony Cheng
    Replies:
    1
    Views:
    8,205
    Juan T. Llibre
    Feb 24, 2006
  2. Mike S
    Replies:
    0
    Views:
    23,000
    Mike S
    Sep 16, 2004
  3. Mick
    Replies:
    2
    Views:
    9,525
    Marcin Grunwald
    Feb 23, 2005
  4. Mitch
    Replies:
    2
    Views:
    551
    Mitch
    Mar 13, 2006
  5. Replies:
    1
    Views:
    656
    Jules
    Aug 18, 2005
Loading...

Share This Page