4

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.

4

3 回答 3

9

就我而言,我只是忘了放:

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

在我的/tomcat7/conf/context.xml. 刚刚添加,一切正常。

我的 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>
于 2012-01-06T18:47:11.337 回答
3

今天我花了半天的时间试图处理类似的问题。我有 tomcat server.xml 文件定义这样的上下文:

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

然后我尝试使用 org.apache.tomcat.jdbc.pool.DataSource 添加 jdbc 池支持。

刚刚将资源定义添加到我的 server.xml 上下文定义中(见上文)。当然,我在 web.xml 中定义了资源引用。

但总是返回 org.apache.tomcat.dbcp.dbcp.BasicDataSource。我花了一些时间调试tomcat,最终得到以下结果:

  1. 如果我在 server.xml 上下文中定义资源-tomcat 不会选择它。
  2. 如果在 web 存档的 META-INF/context.xml 中定义可以正常工作。
  3. 如果在 server.xml GlobalNamingResources 标记中定义 - Tomcat 不会选择它。
  4. 如果在 tomcat 全局 context.xml 文件中定义可以正常工作。

如果您在 web.xml 中为不良情况 1,3 指定 resource-ref - tomcat 将返回 org.apache.tomcat.dbcp.dbcp.BasicDataSource,因为我可以看到它是某种默认值。但是使用返回的此类数据源将导致如下情况:

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)

如果未在 web.xml 中指定资源引用,那么您将收到一个异常,告诉您找不到具有此类名称的资源。

我还注意到,对于好的情况 2,4,没有必要在 web.xml 中指定资源引用(使用和不使用资源引用)。

试试我描述的一些案例。我希望能有所帮助。

我会尝试在 tomcat 全局 context.xml 文件中定义资源。

祝你好运!

Ps 我也运行 7.0.22 版本。

于 2011-11-16T16:08:28.160 回答
-1

解决方案是在您的 servlet 中导入 javax.sql.DataSource,因为您在 type="javax.sql.DataSource" 的 context.xml 中定义资源

于 2015-01-16T07:46:31.480 回答