You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by chris markiewicz <cm...@CollectiveIntelligence.com> on 2004/06/28 21:28:35 UTC

want BasicDataSource from javax.sql.Datasource...

Hello.  I want a BasicDataSource object so that I can monitor some pool
statistics.  I grab a javax.sql.DataSource object (through jndi) but I
get a ClassCastException when I try to cast it to BasicDataSource.  So
the question is, how do I get a BasicDataSource when using DBCP/JNDI?

Snippet of code:
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/ci_common");
BasicDataSource bds = (BasicDataSource) ds;

The error occurs when I attempt the cast, obviously.  An odd aside, if I
send ds to the console, it says that it is of type BasicDataSource.

Excerpt of server.xml:
<Resource name="jdbc/globaldb" type="javax.sql.DataSource" />
  <ResourceParams name="jdbc/globaldb">
      <parameter>
          <name>factory</name>
          <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
      </parameter>
      <parameter>
          <name>url</name>
          <value>omitted</value>
      </parameter>
      <parameter>
 
<name>driverClassName</name><value>com.mysql.jdbc.Driver</value>
      </parameter>
      <parameter><name>username</name><value>omitted</value></parameter>
      <parameter><name>password</name><value>omitted</value></parameter>
	pool params omitted..
  </ResourceParams>
...
<Context path="/xapp" docBase="xapp">
<ResourceLink name="jdbc/ci_common" global="jdbc/globaldb"
type="javax.sql.DataSource" />
</Context>


Note that I did find a similar thread in the archives, but it didn't
help me.  It suggested removing the factory parameter and adding a
resource-ref entry to my web.xml.  Neither of those produced any
results.

Thanks for your time.
chris


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: want BasicDataSource from javax.sql.Datasource...

Posted by Dirk Verbeeck <di...@pandora.be>.
So if you do ds.getClass().getName() it is a BasicDataSource but a 
cast fails?

In this case you have a classloader problem.
Remove the commons-dbcp jar from WEB-INF/lib.
In most cases you only need one copy of the dbcp jar and that one is 
in the tomcat common/lib directory.

-- Dirk

chris markiewicz wrote:

> Hello.  I want a BasicDataSource object so that I can monitor some pool
> statistics.  I grab a javax.sql.DataSource object (through jndi) but I
> get a ClassCastException when I try to cast it to BasicDataSource.  So
> the question is, how do I get a BasicDataSource when using DBCP/JNDI?
> 
> Snippet of code:
> Context ctx = new InitialContext();
> DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/ci_common");
> BasicDataSource bds = (BasicDataSource) ds;
> 
> The error occurs when I attempt the cast, obviously.  An odd aside, if I
> send ds to the console, it says that it is of type BasicDataSource.
> 
> Excerpt of server.xml:
> <Resource name="jdbc/globaldb" type="javax.sql.DataSource" />
>   <ResourceParams name="jdbc/globaldb">
>       <parameter>
>           <name>factory</name>
>           <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
>       </parameter>
>       <parameter>
>           <name>url</name>
>           <value>omitted</value>
>       </parameter>
>       <parameter>
>  
> <name>driverClassName</name><value>com.mysql.jdbc.Driver</value>
>       </parameter>
>       <parameter><name>username</name><value>omitted</value></parameter>
>       <parameter><name>password</name><value>omitted</value></parameter>
> 	pool params omitted..
>   </ResourceParams>
> ...
> <Context path="/xapp" docBase="xapp">
> <ResourceLink name="jdbc/ci_common" global="jdbc/globaldb"
> type="javax.sql.DataSource" />
> </Context>
> 
> 
> Note that I did find a similar thread in the archives, but it didn't
> help me.  It suggested removing the factory parameter and adding a
> resource-ref entry to my web.xml.  Neither of those produced any
> results.
> 
> Thanks for your time.
> chris
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
> 
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org