Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Best place to look up JNDI resource, init db connection, in servlet.

Reply
Thread Tools

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

 
 
jason.cipriani@gmail.com
Guest
Posts: n/a
 
      07-06-2008
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
 
Reply With Quote
 
 
 
 
jason.cipriani@gmail.com
Guest
Posts: n/a
 
      07-06-2008
Lew, thanks a lot for the detailed and informative explanation. That
clears a lot of stuff up.

On Jul 6, 2:23*pm, Lew <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
jason.cipriani@gmail.com
Guest
Posts: n/a
 
      07-06-2008
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 <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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 <(E-Mail Removed)> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
questions about object initialization, default-init and value-init Jess C++ 4 05-04-2007 02:47 AM
Sequence Order between Page Init and User Control Init Tony Cheng ASP .Net 1 02-24-2006 01:56 PM
Compiler/Linker Error undefined reference to 'std::ios_base::Init::Init[in-charge]() clusardi2k@aol.com C++ 1 08-18-2005 07:11 PM
why use ENC JNDI (java:comp/env) instead of JNDI name ? Mick Java 2 02-23-2005 12:50 PM
Need Help with Hibernate - "Not binding factory to JNDI, no JNDI name configured" Mike S Java 0 09-16-2004 09:58 PM



Advertisments