Java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be passed to org.apache.tomcat.jdbc.pool.DataSource

I am running Tomcat 7.0.22 and I wrote a simple servlet that connects to the SQL Anywhere 12.0 database. When I run the servlet, I get java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be passed to org.apache.tomcat.jdbc.pool.DataSource. The file My./META-INF/content.xml is as follows:

<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 is as follows:

 <?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 / Report 1
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 an 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 encode database access 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 occurs when I try to get a DataSource in this line: DataSource ds = (DataSource) envCtx.lookup ("jdbc / FUDB");

Thanks in advance for pulling my hair out.

+4
source share
3 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 everything works 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> 
+8
source

Today I spent half a day trying to deal with a similar problem. I have a tomcat server.xml file defining a context like this:

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

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

Just added a resource definition to my server.xml server definition (see above). And for the reason I defined resource-ref in my web.xml.

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

  • If I define the resource in the context of server.xml - tomcat does NOT select this.
  • If defined in a web archive, META-INF / context.xml is working fine.
  • If you define GlobalNamingResources in the server.xml tag, tomcat will NOT select this.
  • If you define the global context.xml file in tomcat, it works fine.

If you specify resource-ref in web.xml for bad cases 1,3 - tomcat will return org.apache.tomcat.dbcp.dbcp.BasicDataSource because I see that it is some kind of default. BUT using such a data source will lead to 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 you do not specify resource-ref in web.xml, you will get an exception indicating that a resource with this name cannot be found.

I also noticed that for good cases there is no need to specify the ref-resource in web.xml (works with and without the ref-resource).

Try some of the cases described. Hope something helps.

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

Good luck

Ps I am also running version 7.0.22.

+3
source

The solution is to import javax.sql.DataSource into your servlet since you define resouce in context.xml type = "javax.sql.DataSource"

-1
source

All Articles