4

我得到了这个例外:

java.lang.ClassCastException: org.apache.tomcat.jdbc.pool.DataSource cannot be cast to javax.sql.ConnectionPoolDataSource

当我尝试运行使用与 Tomcat7 无缝协作的 tomcat jdbc 池的 web 应用程序(在 Tomcat6 中)时

我已经在 tomcat 6 lib 文件夹中包含了这些 jar:

tomcat-jdbc.jar
tomcat-juli.jar

可能是什么问题呢?

更新:

protected static Connection getConnection() throws NamingException, SQLException {
    InitialContext cxt = new InitialContext();
    String jndiName = "java:/comp/env/jdbc/MyDBHrd";
    ConnectionPoolDataSource dataSource = (ConnectionPoolDataSource) cxt.lookup(jndiName); // ClassCastException here....
    PooledConnection pooledConnection = dataSource.getPooledConnection();
    Connection conn = pooledConnection.getConnection();
    return conn; // Obtain connection from pool
} 

配置:

<Resource name="jdbc/MyDBHrd"
          auth="Container"
          type="javax.sql.DataSource"
          factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
          testWhileIdle="true"
          testOnBorrow="true"
          testOnReturn="false"
          validationQuery="SELECT 1"
          validationInterval="30000"
          timeBetweenEvictionRunsMillis="30000"
          maxActive="5000"
          minIdle="10"
          maxWait="10000"
          initialSize="20"
          removeAbandonedTimeout="120"
          removeAbandoned="true"
          logAbandoned="false"
          minEvictableIdleTimeMillis="30000"
          jmxEnabled="true"
          jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;
            org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"
          username="sa"
          password="password"
          driverClassName="net.sourceforge.jtds.jdbc.Driver"
          url="jdbc:jtds:sqlserver://192.168.114.130/MyDB"/>

当我更改“类型”时:

 type="javax.sql.ConnectionPoolDataSource"

我收到这个警告:

WARNING: javax.sql.ConnectionPoolDataSource is not a valid class name/type for this JNDI factory.

导致getConnection()返回NULL。

进口:

import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.ConnectionPoolDataSource;
import javax.sql.PooledConnection;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
4

1 回答 1

2

作为最终用户,您永远不必ConnectionPoolDataSource直接使用 a。它旨在作为物理连接的工厂 (the PooledConnection)。那PooledConnection保存在连接池中。当您执行 aDataSource.getConnection时,数据源将从池中签出 a PooledConnection,并将使用获得的逻辑连接返回给您作为最终用户,并在该逻辑关闭PooledConnection.getConnection()时将物理连接返回到池。Connection

所以构造是

User -- uses --> DataSource (with connectionpooling) -- uses --> ConnectionPoolDataSource

或者

ConnectionPoolDataSource --> creates PooledConnection --> DataSource --> returns Connection --> User

a 的使用与DataSource是否DataSource提供连接池无关(它应该对您透明)。

另请参阅我以前对另一个问题的回答:https ://stackoverflow.com/a/12651163/466862

换句话说,您的代码应更改为:

protected static Connection getConnection() throws NamingException, SQLException {
    InitialContext cxt = new InitialContext();
    String jndiName = "java:/comp/env/jdbc/MyDBHrd";
    DataSource dataSource = (DataSource) cxt.lookup(jndiName);
    Connection conn = dataSource.getConnection();
    return conn;
} 
于 2012-12-21T10:09:33.543 回答