java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to org.apache.tomcat.jdbc.pool.DataSource

user981052 picture user981052 · Oct 5, 2011 · Viewed 19.5k times · Source

I'm running Tomcat 7.0.22 and I wrote a simple servlet that connects to a SQL Anywhere 12.0 database. When I run the servlet I get java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to org.apache.tomcat.jdbc.pool.DataSource. My ./META-INF/content.xml file looks like the following:

<Context>
  <Resource name="jdbc/FUDB"
           auth="Container"
           type="javax.sql.DataSource"
           username="dba"
           password="sql"
           driverClassName="sybase.jdbc.sqlanywhere.IDriver"
           factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"

url="jdbc:sqlanywhere:uid=dba;pwd=sql;eng=BTH476331A_FedUtilization;" accessToUnderlyingConnectionAllowed="true" maxActive="8" maxIdle="4" />

My webapp web.xml looks like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">
    <display-name>FedUtilization</display-name>  
    <servlet>
  <servlet-name>Report1</servlet-name>
      <display-name>Report1</display-name>
      <servlet-class>com.sapgss.ps.servlet.Report1</servlet-class> 

Report1 /Report1
SQL Anywhere 12.0.1 server jdbc3 jdbc/FUDB javax.sql.DataSource Container

The servlet code is as follows:

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.*;
import org.apache.catalina.core.StandardContext.*;
import org.apache.tomcat.jdbc.pool.*;
import com.sapgss.ps.dbutil.*;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;

public class Report1 extends HttpServlet {

    public void doGet(HttpServletRequest request,

HttpServletResponse response) throws IOException, ServletException { try { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println("Hello Elaine!"); out.println(""); out.println(""); out.println("

Hello Elaine!

");
// This is how to code access to the database in Java Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); DataSource ds = (DataSource) envCtx.lookup("jdbc/FUDB"); Connection conn = ds.getConnection(); . . .
} }

The error happens when I try to get a DataSource at this line: DataSource ds = (DataSource) envCtx.lookup("jdbc/FUDB");

Thanks in advance I'm pulling my hair out.

Answer

ftfarias picture ftfarias · Jan 6, 2012

In my case I just forgot to put:

factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"

in my /tomcat7/conf/context.xml. Just added and all worked fine.

My context.xml:

<Context> 
    <Resource name="jdbc/gestrel" auth="Container"
    type="javax.sql.DataSource"
    driverClassName="org.postgresql.Driver"
    url="jdbc:postgresql://127.0.0.1:5432/g...."
    username="postgres"
    password="....." maxActive="20" maxIdle="10"
    factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
   maxWait="-1"/>
</Context>