tomcat-taglibs-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Hans Bergsten <>
Subject Re: using datasource
Date Mon, 05 Aug 2002 01:31:36 GMT
The Kelley's wrote:
> I have never used JNDI.    I read the Tomcat docs but until I see an example
> using JSTL...... What I would give for one spoon fed example.

Get my book when it's relased ;-) Seriously, configuration of an object
factory for JNDI in Tomcat 4 to date is harder than it should be, mainly
because the default DataSource factory bundled with Tomcat 4 seems to be
buggy and no longer supported. I believe Tomcat 4.1 bundles a JNDI
object factory and DataSource from Jakarta Commons instead, which should
be an improvement. But even with Tomcat 4 it's not that hard to and the
doc I referred to earlier is pretty good.

> Do I understand the JSTL in Action text correctly..
> The text
> However, the <sql:setDataSource> tag does not support connection pooling.
> It's not designed to do so. Instead, Java programmers who set up default
> databases
> for JSP pages have ample opportunity to set up their own pooling strategies.
> Doing
> so makes <sql:setDataSource> suitable only for applications where high
> performance
> isn't crucial. In particular, <sql:setDataSource> is great for small
> applications,
> That's why I bought the book to use with databases. Arg!!!
> Can I use JSTL with connection pooling? Doesn't sound like it.

Sure you can. You just have to make a DataSource that implements
connection pooling available to the JSTL actions. The only thing
the quoted text (and the JSTL spec) says is that the DataSource
created by the <sql:setDataSource> action does not provide pooling.

If you think JNDI is to messy, you can make a DataSource with pooling
capabilities available through other means, for instance with an
application lifecycle listener. This one creates an instance of Oracle's
DataSource and saves a reference to it as an application scope variable
that you can then use in the JNDI actions:

package com.ora.jsp.servlets;
import javax.servlet.*;
import javax.servlet.http.*;
import oracle.jdbc.pool.*;
public class ResourceManagerListener2 implements
   ServletContextListener {
   private OracleConnectionCacheImpl ds =null;

   public void contextInitialized(ServletContextEvent sce){
     ServletContext application = sce.getServletContext();
     String jdbcURL = application.getInitParameter("jdbcURL");
     String user = application.getInitParameter("user");
     String password = application.getInitParameter("password");
     String maxLimit = application.getInitParameter("maxLimit");
     try {
       ds = new OracleConnectionCacheImpl();
     catch (Exception e){
       application.log("Failed to create data source:" +
     application.setAttribute("appDataSource", ds);

You can use it like this:

   <sql:query dataSource="${appDataSource}" ... />

If you let the listener save the reference as the default DataSource,
you don't even have to use the "dataSource" attribute:

   public void contextInitialized(ServletContextEvent sce){
     Config.set(application, Config.SQL_DATASOURCE, ds);

   <sql:query ... />

I hope this helps. To learn more about this, I suggest you read the
JSTL spec until books and more articles about JSTL are available.

Hans Bergsten
Gefion Software
JavaServer Pages

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message