Oracle provides classes for connection pooling. There is a good description of
the classes and how to implement them.
You can read more at
http://technet.oracle.com/docs/products/oracle8i/doc_library/817_doc/java.817/a83724/connpoc2.htm


Santosh









Hans Bergsten <[EMAIL PROTECTED]> on 05/29/2001 10:30:57 PM

Please respond to A mailing list about Java Server Pages specification and
      reference <[EMAIL PROTECTED]>

To:   [EMAIL PROTECTED]
cc:    (bcc: Santosh Daryani/IT/Aon Consulting)

Subject:  Re: Database connection Pool in Tomcat



"Chawla, Mehar" wrote:
>
> Hi all,
>
>     Can anyone tell me how to implement JDBC connection pooling in Tomcat.
> Basically I want the following things :
>
>     1. Setup the connection pool in web.xml or server.xml with oracle user
> name and password set up in that file itself to avoid harcoding the username
> and password
>         in the servlets or jsp files.

You can use either servlet or context init parameters in the web.xml
file to specify this type of information. The values you specify can
be read with ServletConfig.getInitParameter() and
ServletContext.getInitParameter()
respectively. See also below.

>     2. Create a Servlet with methods GetConnection and ReleaseConnection
> (not close connection) to return the connection back to the pool and which
> should be loaded on startup of the web server (Tomcat)

First, I believe Tomcat 4.0 (currently in beta) implements the part of
the Servlet specification that allows you to specify a JDBC DataSource-based
connection pool as <resource-ref> in the web.xml (even though this is only
mandatory for a complete J2EE product, as opposed to a simple web container).
With this approach you get a reference to the pool using JNDI. Other web
containers may do the same, but since it's not mandated by the spec, it
may not be available in all simple web containers.

If you don't want to use this approach, what you have in mind is also
possible. I would, however, recommend that you use a servlet to create
an instance of a separate pool class (there are many available on the net)
and make it available to the rest of the application as a ServletContext
attribute.

>     3. And finally the jsp code to get the connection from that servlet.

I strongly recommend that you either put all the database code in a
servlet instead (acting as the Controller in the MVC model), or use a
set of custom actions in the JSP pages that know how to get hold of
connections from the pool. Otherwise you'll soon run into reliability
problems (for instance, making sure the connection is always returned to
the pool is not trivial if you use scriptlets in the JSP page) and
maintenance problems (the amount if Java code to do this right as way to
much to put directly in a JSP page; it's harder to debug the generated
servlet than it is to debug a standard Java class that you have full
control over).

Either way, if you have the pool available as a context attribute, all
you need to do to access it is something like this:

  DataSource pool = (DataSource) context.getAttribute("mypool");
  Connection conn = pool.getConnection();

>     I would appericiate if you could help me in setting this up by providing
> the sample code.

I describe using a servlet to create a pool and make it available as
a context attribute and a set of custom actions for accessing the
database in my JSP book, along with the source code for all of it:

  <http://TheJSPBook.com/>

There are also similar custom actions within the Jakarta Taglibs
project:

  <http://jakarta.apache.org/taglibs/>

Hans
--
Hans Bergsten           [EMAIL PROTECTED]
Gefion Software         http://www.gefionsoftware.com
Author of JavaServer Pages (O'Reilly), http://TheJSPBook.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to