Re: Best practice for initializing JDBC connection in Servlets.

From: Kevin McMurtrie (mcmurtri_at_dslextreme.com)
Date: 06/19/04


Date: Sat, 19 Jun 2004 14:59:05 -0700

In article <20040619213437.GA2767@matijek.plusseven.com>,
 Alex Polite <m4@polite.se> wrote:

> I'm starting out with servlets.
>
> I'm wondering about best practices for initializing JDBC
> connections. I've tried doing it in the servlet constructor and in
> doGet. Intuitively the constructor feels like the right place, but I
> wonder if there could be negative consequences when multiple clients
> access the servlet simultaneously? Or maybe I should do it in the init
> method?
>
> Any recommended reading available online?
> alex

For a very simple and low traffic service, it may be acceptable to
initialize the JDBC driver in init() and manage connections in doGet().

For best performance you should manage the JDBC connections outside the
servlet. A JDBC pool can cache connections during frequent use to
eliminate the overhead of establishing and destroying connections.
Often there's one pre-loading Servlet that does nothing except
initialize the application context, like the JDBC pooling.

Eventually you'll want to move all JDBC code out of the servlets. The
software will be easier to scale if the servlets only have to deal the
interface between an HTTP client and the server's data, not management
of the data too.



Relevant Pages

  • Re: CLOS and C++
    ... so why is subclassing Servlet to make a new kind of servlet not ... > find methods which would be true for all lab instruments, ... All I'm saying is that Java programmers do reuse code ... But if you don't like Servlets, take JDBC drivers. ...
    (comp.lang.lisp)
  • Re: Jdbc connection pooling without app/J2ee server
    ... > If using Jdbc outside of an application/J2ee server with the Microsoft ... Some drivers will offer an internal pooling feature, ... useful for long-running applications that use lots of connections, ... then pooling in the driver won't be able to help. ...
    (microsoft.public.sqlserver.jdbcdriver)
  • Re: Shutdownhook not working
    ... so you shouldn't see connections in the database console ... ... I use a shutdownhook to close the connections, ... In the initpart of the servlet I put ... But for some reason, whenever I restart the ...
    (comp.lang.java.databases)
  • Re: servlets slow when disconnected
    ... You really should read the servlet spec. ... the servlet container is the software that provides the context ... If the network interface is actually shut down at the OS level then the ... You especially want that for wireless connections. ...
    (comp.lang.java.programmer)
  • Re: Microsoft JDBC driver support connection pooling out of the box
    ... > If using Jdbc outside of an application/J2ee server with the Microsoft ... useful for long-running applications that use lots of connections, ... pooling won't help. ...
    (microsoft.public.sqlserver.jdbcdriver)