How do I set up DBCP for Tomcat 5.5 and Eclipse?

I'm experimenting with Struts at the moment and I need to set up a D connection pool.

I've tried to read around on what I need to do and I've found references to a context.xml file, or at least a Context element that sits in my server.xml.

As I'm keen to keep things compartmentalised, I understand that I can add a context.xml file under my Web-Inf for each web application. This is my preferred solution.

I'm a bit of a novice when it comes to configuring all this (give me a configured system and I'm happy as Larry)...

I've tried adding the context.xml under web-inf, I've tried renaming it to StrutsTest (the name of my app). Each time, I've included the following:

<?xml version="1.0" encoding="UTF-8"?>

<Context path="/StrutsTest" docBase="StrutsTest"

debug="5" reloadable="true" crossContext="true">

<!-- THIS IS NEW -->

<Resource name="jdbc/TestDB"

auth="Container"

type="javax.sql.DataSource"/>

<ResourceParams name="jdbc/TestDB">

<parameter>

<name>factory</name>

<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>

</parameter>

<parameter>

<name>maxActive</name>

<value>100</value>

</parameter>

<parameter>

<name>maxIdle</name>

<value>30</value>

</parameter>

<parameter>

<name>maxWait</name>

<value>10000</value>

</parameter>

<parameter>

<name>username</name>

<value>marklawford</value>

</parameter>

<parameter>

<name>password</name>

<value>mysql</value>

</parameter>

<parameter>

<name>driverClassName</name>

<value>com.mysql.jdbc.Driver</value>

</parameter>

<parameter>

<name>url</name>

<value>jdbc:mysql://localhost:3306/mysql_test?autoReconnect=true</value>

</parameter>

</ResourceParams>

<!-- END OF NEW -->

</Context>

Context initContext =new InitialContext();

Context envContext = (Context)initContext.lookup("java:/comp/env");

DataSource ds = (DataSource)envContext.lookup("jdbc/TestDB");

Connection conn =null;

try{

conn = ds.getConnection();

System.out.println(conn.getMetaData().getCatalogTerm());

}catch(Exception e){

e.printStackTrace();

}

I've also have the following in my web.xml:

<resource-ref>

<description>DB Connection</description>

<res-ref-name>jdbc/TestDB</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

</resource-ref>

I get an org.apache.tomcat.dbcp.dbcp.BasicDataSource instance, but I then get the message:

"Cannot create JDBC driver of class '' for connect URL 'null'".

Where is my disconnect? What is it I'm not doing right?

[3830 byte] By [marklawforda] at [2007-10-3 0:10:29]
# 1
I've already answered a similar question. Check out my reply in this thread: http://forum.java.sun.com/thread.jspa?threadID=741788&messageID=4252932#4252932
gimbal2a at 2007-7-14 16:59:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Yes! Thankyou for that. I had of course put the context.xml file in the web-inf directory.

I followed your example to the letter and it still wouldn't work. Or rather, I followed to the letter expect for the difference between "WEB" and "META".

Once I worked that out, it started working like a dream.

Note to anyone else who has a similar problem, make sure you REALLY look at the answer given. Make sure you REALLY do have all the files in the suggested places. It will save you time.

Thanks again.

marklawforda at 2007-7-14 16:59:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...