Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Requesting explanation on process DBCP takes

Status
Not open for further replies.

javawanabe

Programmer
Joined
Dec 14, 2004
Messages
3
Location
CA

Hi all,
I am trying to implement DBCP to a DB2 database on Tomcat 4.1.X and am having a few problems.

I can successfully connect directly (no DBCP) from within my code using the supplied DB2 driver, so I don't think there are any issues with either my version of Tomcat or of the DBCP jar or the DB2 driver.

However, using DBCP is proving more difficult. I have configured my web.xml and server.xml files accordingly. I then created an application listener which retrieves the info via JNDI and places my DAO objects into the application context. This too appears to wok fine, as I do successfully retrieve a generic datasource object.

But, when I then try the getConnection() method, I receive a 'cannot load driver class null' error. Obviously, something is nsf with that stage.

So I guess my questions are:

1- Is the pool actually being created successfully? Does it not use the driver to actually create the pool?
2- Am I possibly running into a scope issue?

Any info appreciated.

JW
 
Sounds like DBCP cannot find the driver you wish to use - where/how have you specified the driver for DBCP to use ? Perhaps post your config relating to the DBCP config.

--------------------------------------------------
Free Database Connection Pooling Software
 
Hi again,
I finally have a bit of time to get back to this. To recap, I am trying to get DBCP connection pooling to work with Tomcat 4.1.24 and DB2 8.X. I can connect directly, but whenever I try to use DBCP, I receive a "Cannot load driver class 'null'" error. The DB2 and JNDI jar files are located in <tomcat-root>\common\lib.

This code snippet works:
...
String url = "jdbc:db2://myserver:50000/mydb";
String user = "test";
String pass = "password";
Class.forName("com.ibm.db2.jcc.DB2Driver");
Connection conn = DriverManager.getConnection(url,user,pass);
Statement stmt = conn.createStatement();
ResultSet myRs = stmt.executeQuery("select * from myTable");
...

This leads me to believe that:
a) My driver is fine and jar file location is ok
b) My login credentials are fine
I use the same driver and connection info in my DBCP config.

-------------------------------------------
Now when I try to use DBCP, this code snippet does NOT work.
...
Context jndiCtx = new InitialContext();
Context ctx = (Context) jndiCtx.lookup("java:comp/env");
DataSource ds = (DataSource) ctx.lookup("jdbc/mydb");
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
ResultSet myRs = stmt.executeQuery("select * from myTable");
...

Tomcat does not throw any errors at startup in any logs and when I execute the above code, the DataSource retrieved (ds) is not null and a getClassName() call reveals a generic commons DataSource object. However, once it gets to the getConnection() method, the evil "Cannot load driver class 'null'" error occurs.

I tried messing around with the JNDI entries to prove that was OK, and as expected I get a naming error if I put a wrong value. This tells me that the "lookup" portion works, yes?

Here are the relevant server.xml and the web.xml entries:

------------------------------------------------------
server.xml (note: I also tried it without the factory param)
------------------------------------------------------
...
<Context...>
<Resource name="jdbc/mydb" scope="Shareable" type="javax.sql.DataSource" auth="Container" />

<Resourceparams name="jdbc/mydb">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>username</name>
<value>test</value>
</parameter>
<parameter>
<name>password</name>
<value>password</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.ibm.db2.jcc.DB2Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:db2://myserver:50000/mydb</value>
</parameter>
</Resourceparams>
</Context>

------------------------------------------------------
web.xml
------------------------------------------------------
...
<resource-ref>
<resource-ref-name>jdbc/mydb<resource-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>


So, that is pretty much it. I have consulted 2 books, countless websites, and would appreciate any help.

Thanks

JW
 
I think i know what the problem is javawanabe. It worked for me but keep in mind im using tomcat 5.0.28 and mysql 4.1.

In this:

<Context...>
<Resource name="jdbc/mydb" scope="Shareable" type="javax.sql.DataSource" auth="Container" />

<Resourceparams name="jdbc/mydb">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>username</name>
<value>test</value>
</parameter>
<parameter>
<name>password</name>
<value>password</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.ibm.db2.jcc.DB2Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:db2://myserver:50000/mydb</value>
</parameter>
</Resourceparams>
</Context>

needs to be changed. It looks like tomcat doesn't read the ResourceParams tag. So eliminate it altogether. So you can do the following:

<Context...>
<Resource name="jdbc/mydb"
scope="Shareable"
type="javax.sql.DataSource"
auth="Container"
username="test"
password="password"
driverClassName="com.ibm.db2.jcc.DB2Driver"
url="jdbc:db2://myserver:50000/mydb"/>

</Context>

hopefully this solves it. Do keep us updated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top