Have a look at the attached context listener. Once the datasource is
initialized, you can acces it like that (but I guess you know that already)

Connection c = dataSource.getConnection();
try {
    Statement s = c.createStatement();
    String sql = "SELECT ...";
    ResultSet rs = s.executeQuery( sql );
    ....
    rs.close();
    s.close();
} catch( SQLException e ) {
    ...
} finally {
    try {
        c.close();
    } catch( SQLException e ) {
        ...
    }
}

Erik Price wrote:
> I found a promising-looking post in the archives about using DBCP in a
> Tomcat-based servlet environment:
>
> http://www.mail-archive.com/tomcat-user@jakarta.apache.org/msg67843.html
>
> Unfortunately, the link to the source code (the "meat" of the post) is
> broken.  Does anyone have any sample source code or other references
> suggesting how to go about adding DBCP into a webapp?  I have
> installed the DBCP JAR library into my webapp's WEB-INF/lib, and the
> MySQL driver JAR is in $CATALINA_HOME/common/lib, so I'm raring to go.
>
> (I've read the instructions in the DBCP API and I think I get the
> idea.   It's not using DBCP's classes that I'm unsure of /per se/,
> rather it's how to go about setting up DBCP in the context of a
> servlet container. My first idea was to write a simple servlet that
> sets up the connection pool in its init() method.  Then I thought
> that seems a little inelegant, and perhaps a ContextListener would do
> (it would listen for the startup of my webapp and then initialize the
> connection pool when the webapp starts).  But I'm not sure if this is
> the right solution either.)
>
> How do YOU implement DBCP?
>
> Thanks,
>
> Erik
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
package com.scope.rf.controller;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Locale;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.dbcp.BasicDataSource;

import com.scope.rf.model.Model;

/**
 *
 * @author hschmi
 */
public class ResourceManagerListener implements ServletContextListener {

    private Model model;
    BasicDataSource ds;
    
    public void contextInitialized( ServletContextEvent sce ) {

        ServletContext app  = sce.getServletContext();
        
        /* 
         * TODO:
         * This was necessary to make the application run on in a container with
         * a default locale set to en_US. WE NEED THE GERMAN LOCALE. The 
         * downside of setLocale() is that it changes the locale of ALL webapps 
         * in a container! This is not acceptable.
         */
        Locale.setDefault( Locale.GERMAN );
        
        // Load properties
        Properties properties = new Properties();
        InputStream is = app.getResourceAsStream( "/WEB-INF/properties" );
        if( is != null ) {
            try {
                properties.load( is );
            } catch( IOException e ) {
                throw new Error( "can't read from resource 'properties'" );
            }
        } else {
            throw new Error( "resource 'properties' not found" );
        }

        // Initialize data source
        ds = new BasicDataSource();
        ds.setDriverClassName( properties.getProperty( "jdbc.driver" ) );
        ds.setUrl( properties.getProperty( "jdbc.url" ) );
        
		try {
			model = new Model( ds, properties );
		} catch( SQLException e ) {
			// TODO: better exception handling
			throw new Error( e.getMessage() );
		}

        app.setAttribute( model.KEY, model );
    }

    public void contextDestroyed( ServletContextEvent sce ) {
        ServletContext app  = sce.getServletContext();
        model.close();
        app.setAttribute( Model.KEY, null );
        try {
            ds.close();
        } catch( SQLException e ) {
            e.printStackTrace();
        }
    }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to