Hi,
I am getting a little confused about the topic weather Tomcat 4.x ist having a
Connection Pooling implementet or not.
I was following the discussions the last few days and the answers have been a little
contradicting.
Therfore my question loud and clear ; )
Is there a Connection Pooling using JNDI with Tomcat 4.x or not?
Below some postings concerning this topic from the last few days...
Thanks Rainer
<past postings>
Tomcat: As far as I'm concerned, JNDI support is a "now and forever more"
feature of Tomcat 4 and later. It's the standard access mechanism for
J2EE app servers as well.
Recommendation: If you can, you should use JNDI based access to data
sources. This is both portable across containers, and portable across
Struts versus non-Struts applications. In addition, it can be used from
directly from within a JavaBean implementing your business logic, without
requiring a reference to ActionServlet or the servlet context (or the web
layer at all).
> > In the javabean.
> > ----------------
> > import javax.naming.NamingException;
> > import javax.naming.Context;
> > import javax.naming.InitialContext;
> > import javax.naming.NamingEnumeration;
> > import javax.naming.directory.InitialDirContext;
> >
> > class mybean() {
> > java.sql.Connection conn
> > ....
> > get getPooledDatabaseConnection() {
> > Context ctx = new InitialContext();
> > Context envCtx = (Context) ctx.lookup("java:/comp/env/");
> > DataSource ds = (DataSource) envCtx.lookup("jdbc/dbpool");
> > conn = ds.getConnection();
> > }
> > ...
> > }
Yep ... that is exactly the pattern you can use. Nice, isn't it?
> Soefara.
>
Craig
###################################################################################
Unfortunately by using the Tomcat (4.0.x) default JNDI datasource factory
(aka Tyrex) you not only do not have a pool manager interface but you don't
have a pool. The Tyrex factory only sets up one physical connection and one
pool instance AFAIK.
In order to create a pool you either can use the jakarta-commons DBCP
factory or write your own implementation. Craig McClanahan recently posted
this DBCP implementation which works with TC 4.0.x.
**************** Craig's post
> It should be possible to use the DBCP based pool even with Tomcat 4.0.x by
> overriding the default JNDI factory for javax.sql.DataSource objects (I
> haven't tested this, but in theory it should all work).
> This requires the following steps:
>
> * Download recent nightly builds of the Collections, Pool, and DBCP
> packages from Jakarta Commons
> (http://jakarta.apache.org/builds/jakarta-commons/nightly/) and put
> the JAR files into $CATALINA_HOME/common/lib along with your JDBC
> driver.
>
> * Configure the "factory" parameter in the <ResourceParams> element
> to set the JNDI ObjectFactory class for this resource:
>
> <ResourceParams name="jdbc/EmployeeDB">
> <parameter>
> <name>factory</name>
> <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
> </parameter>
> ... other configuration parameters ...
> </ResourceParams>
>
> * Configure the other parameter values of the <ResourceParams> element
> from the list of supported properties below:
> - driverClassName - Fully qualified Java class name of the JDBC driver
> - maxActive - Maximum number of active connections at one time
> - maxIdle - Maximum number of idle connections at one time
> (if more are returned to the pool, they will be
> closed to release database resources)
> - maxWait - Number of milliseconds the pool will wait when there
> are no available connections before causing
> getConnection() to throw an exception
> - password - Database password
> - url - Connection URL (also accepts "driverName" for
> backwards compatibility)
> - user - Database username
> - validationQuery - Optional SQL SELECT statement used to validate a
> connection before returning it to the application
> (must return at least one row). This is helpful
> in catching stale connections because of timeouts
> or recycling of the database without stopping
> Tomcat.
>
> Alternatively, you can use any other connection pool you like, if you
> create your own javax.naming.spi.ObjectFactory implementation class (the
> JNDI object factory interface) and registering its name with the "factory"
> parameter. Documentation is in the JNDI Specification and the
> corresponding Service Provider Interface document, at:
>
> http://java.sun.com/products/jndi/docs.html
As I said this implementation works fine for TC 4.0.x. and as Craig mentions
the DBCP BasicDataSourceFactory class is a good model for writing your own
implementation.
HTH
Steven
##############################################################################
Yes, I have been able to get pooling data sources to work with mySql using
the mm drivers. Here are excerpts from my setup:
Server.xml
<Resource name="jdbc/ds" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/ds">
<parameter><name>driverClassName</name><value>org.gjt.mm.mysql.Driver</value
></parameter>
<parameter><name>driverName</name><value>jdbc:mysql://localhost/test</value>
</parameter>
<parameter><name>user</name><value>youruser</value></parameter>
<parameter><name>password</name><value>yourpasswd</value></parameter>
</ResourceParams>
application's web.xml
<resource-ref>
<description>
Resource reference to a factory for
javax.sql.Datasource
instances that may be used for talking to a
particular
database that is configured in the server.xml file.
</description>
<res-ref-name>jdbc/ds</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Lastly, in my code. This will test for a pooled datasource.
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/2rc");
Connection conn;
if ( ds instanceof ConnectionPoolDataSource ) {
ConnectionPoolDataSource poolDataSrc =
(ConnectionPoolDataSource)ds;
PooledConnection pc = poolDataSrc.getPooledConnection();
conn = pc.getConnection();
out.println("Pooled Connection<br>");
} else {
conn = ds.getConnection();
}
Thanks,
Bill Stone
</past postings>