Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 316
user981052 Avatar asked Dec 21 '22 08:12

user981052


2 Answers

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>
like image 61
ftfarias Avatar answered Apr 16 '23 16:04

ftfarias


Today I've spent a half of the day trying to deal with the similar issue. I have tomcat server.xml file defining context like this:

<Context docBase="app" path="/my_context_path">
</Context>

Then I tried to add jdbc pool support using org.apache.tomcat.jdbc.pool.DataSource.

Just added resource definition to my server.xml context definition (see above). And of cause I defined resource-ref in my web.xml.

But there was always org.apache.tomcat.dbcp.dbcp.BasicDataSource returned. I spent time debugging tomcat and finally got to the following:

  1. If I define resource in server.xml context - tomcat does NOT pick that up.
  2. If define in web archive's META-INF/context.xml works ok.
  3. If define in server.xml GlobalNamingResources tag - tomcat does NOT pick that up.
  4. If define in tomcat global context.xml file works ok.

If you specify resource-ref in web.xml for bad cases 1,3 - tomcat will return org.apache.tomcat.dbcp.dbcp.BasicDataSource, cause as I can see it is some kind of default. BUT using such data source returned will cause something like this:

org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null' at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452) at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371) at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)

If not specify resource-ref in web.xml then you will get an exception telling that resource with such name could not be find.

Also I noticed that for good cases 2,4 specifying resource-ref in web.xml is not necessary (works with and without resource-ref).

Try some of the cases I described. I hope something will help.

I would try to define resource in tomcat global context.xml file.

Good luck!

P.s. I run 7.0.22 version as well.

like image 38
dpetruha Avatar answered Apr 16 '23 15:04

dpetruha