You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by James Ward <ja...@firstlink.com> on 2002/06/09 01:59:53 UTC

A Connection Pool in JNDI that works!

Wow, this has been a fun one.  It should have been an easy little stroll
in the park, but due to my idioticness, it turned into a sour climb up
Everest.  So, I embarked on mission to get a connection pool working as
a JNDI Resource.  I found very little info on the subject, thus the
reason for me posting this info here.  Here goes...

(BTW: This is for Tomcat 4)

I started out using the standard JDBC Data Source documentation found in
the Tomcat 4 JNDI Resources HOW-TO.  It worked fine, but then I
discovered there was no built-in connection pooling.  Bummer.  So, I
ended up implementing the Apache commons dbcp found at:
http://jakarta.apache.org/builds/jakarta-commons/nightly/commons-dbcp/
This is for mySQL but it should be very easy to change the config for
any database.

Here is a snippet from my server.xml file:

<!-- Put this within a Context -->
          <Resource name="jdbc/MyDB" auth="Container"
                    type="javax.sql.DataSource" />
          <ResourceParams name="jdbc/MyDB">
            <parameter>
              <name>factory</name>
 
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
            </parameter>
            <parameter>
              <name>driverClassName</name>
              <value>org.gjt.mm.mysql.Driver</value>
            </parameter>
            <parameter>
              <name>maxActive</name>
              <value>2</value>
            </parameter>
            <parameter>
              <name>maxIdle</name>
              <value>2</value>
            </parameter>
            <parameter>
              <name>username</name>
              <value>myUsername</value>
            </parameter>
            <parameter>
              <name>password</name>
              <value>myPassword</value>
            </parameter>
            <parameter>
              <name>maxWait</name>
              <value>-1</value>
            </parameter>
            <parameter>
              <name>url</name>
              <value>jdbc:mysql://myhost:3306/mydatabase</value>
            </parameter>
          </ResourceParams>

I also discovered that I don't need to declare the resources in my
web.xml like the documentation stated.

Now you can just use this code snippet to get a DataSource and a
Connection:

    Context initCtx = new InitialContext();
    Context envCtx = (Context) initCtx.lookup ("java:comp/env");
    DataSource ds = (DataSource)envCtx.lookup("jdbc/MyDB");
    conn = (Connection)ds.getConnection();

Another thing to note is that I have the latest commons-dbcp,
commons-collection, commons-pool, and mysql jars in my common/lib dir.

Now why did that take me so long to figure out?  You really don't want
to know.  I am just an idiot.  But I hope that my toils will help
someone.

Thanks to Craig for this post:
http://www.geocrawler.com/archives/3/193/2002/4/0/8407853/
Which was about the only thing I could find with Google that was of
help.

-James

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: A Connection Pool in JNDI that works!

Posted by "anthony.dodd" <an...@ntlworld.com>.
Hi

    I've posted a solution see "Generic DataSource Resource Factory
Available. JDBC Data Source" on the tomcat user maillist.

Tony

----- Original Message -----
From: "James Ward" <ja...@firstlink.com>
To: <to...@jakarta.apache.org>; <to...@jakarta.apache.org>
Sent: Saturday, June 08, 2002 4:59 PM
Subject: A Connection Pool in JNDI that works!


Wow, this has been a fun one.  It should have been an easy little stroll
in the park, but due to my idioticness, it turned into a sour climb up
Everest.  So, I embarked on mission to get a connection pool working as
a JNDI Resource.  I found very little info on the subject, thus the
reason for me posting this info here.  Here goes...

(BTW: This is for Tomcat 4)

I started out using the standard JDBC Data Source documentation found in
the Tomcat 4 JNDI Resources HOW-TO.  It worked fine, but then I
discovered there was no built-in connection pooling.  Bummer.  So, I
ended up implementing the Apache commons dbcp found at:
http://jakarta.apache.org/builds/jakarta-commons/nightly/commons-dbcp/
This is for mySQL but it should be very easy to change the config for
any database.

Here is a snippet from my server.xml file:

<!-- Put this within a Context -->
          <Resource name="jdbc/MyDB" auth="Container"
                    type="javax.sql.DataSource" />
          <ResourceParams name="jdbc/MyDB">
            <parameter>
              <name>factory</name>

<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
            </parameter>
            <parameter>
              <name>driverClassName</name>
              <value>org.gjt.mm.mysql.Driver</value>
            </parameter>
            <parameter>
              <name>maxActive</name>
              <value>2</value>
            </parameter>
            <parameter>
              <name>maxIdle</name>
              <value>2</value>
            </parameter>
            <parameter>
              <name>username</name>
              <value>myUsername</value>
            </parameter>
            <parameter>
              <name>password</name>
              <value>myPassword</value>
            </parameter>
            <parameter>
              <name>maxWait</name>
              <value>-1</value>
            </parameter>
            <parameter>
              <name>url</name>
              <value>jdbc:mysql://myhost:3306/mydatabase</value>
            </parameter>
          </ResourceParams>

I also discovered that I don't need to declare the resources in my
web.xml like the documentation stated.

Now you can just use this code snippet to get a DataSource and a
Connection:

    Context initCtx = new InitialContext();
    Context envCtx = (Context) initCtx.lookup ("java:comp/env");
    DataSource ds = (DataSource)envCtx.lookup("jdbc/MyDB");
    conn = (Connection)ds.getConnection();

Another thing to note is that I have the latest commons-dbcp,
commons-collection, commons-pool, and mysql jars in my common/lib dir.

Now why did that take me so long to figure out?  You really don't want
to know.  I am just an idiot.  But I hope that my toils will help
someone.

Thanks to Craig for this post:
http://www.geocrawler.com/archives/3/193/2002/4/0/8407853/
Which was about the only thing I could find with Google that was of
help.

-James

--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>