You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Tuncay Baskan (İnternet Grubu)" <Tu...@intertech.com.tr> on 2002/12/14 20:20:46 UTC
ClassCastException when using BasicDataSource
I'm trying to use a JNDI name for a JDBC resource. Configuration is
as follows:
Tomcat 4.0.3
DBCP 1.0
In the server.xml, I have the following DefaultContext entry. (It must be
DefaultContext because there are 3 other webapps that use the same database)
<DefaultContext>
<Resource auth="Container" name="jdbc/eproject"
type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/eproject">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value></parameter>
<parameter>
<name>validationQuery</name>
<value>select * from groups</value></parameter>
<parameter>
<name>maxWait</name>
<value>10000</value></parameter>
<parameter>
<name>maxActive</name>
<value>100</value></parameter>
<parameter>
<name>username</name>
<value>username</value></parameter>
<parameter>
<name>password</name>
<value>password</value></parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://quantranet:3306/eproject?autoReconnet=true</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value></parameter>
<parameter>
<name>maxIdle</name>
<value>30</value></parameter>
</ResourceParams>
</DefaultContext>
And in the web.xml:
<resource-ref id="ResourceRef_1039888737098">
<description>none</description>
<res-ref-name>jdbc/eproject</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
I think there isn't a problem with JNDI. jndiContext.lookup() calls
successfully return an "object". It returns the correct object, which is
BasicDataSource (a class that implements javax.sql.DataSource).
I re-evaluated the code, so jndi.lookup() returns an object that has the
name "org.apache.commons.dbcp.BasicDataSource" which implements the
javax.sql.DataSource interface. But... I don't know how, I'm getting
a ClassCastException when I cast it to DataSource to get a connection!
(btw, returned object is not an "instanceof" javax.sql.DataSource,
but getClass().getInterfaces() has javax.sql.DataSource)
initContext = new InitialContext();
envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/eproject");
or,
initContext = new InitialContext();
envContext = (DataSource)initContext.lookup
("java:/comp/env/jdbc/eproject");
fails on the lines which I'm trying to cast DataSource.
So, how can it be? I really appreciate the one who can tell me why!
PS: commons-collections, commons-dbcp, commons-pool is in
%CATALINA_HOME%\common\lib along with mysql-connector and they are in .jar
format as described in JNDI-Resources HOWTO.
/tb.