ClassCastException无法将数据源强制转换为javax.sql.ConnectionPoolDataSource [英] ClassCastException DataSource cannot be cast to javax.sql.ConnectionPoolDataSource
问题描述
我收到此异常:
java.lang.ClassCastException: org.apache.tomcat.jdbc.pool.DataSource cannot be cast to javax.sql.ConnectionPoolDataSource
当我尝试运行我的webapp时(在Tomcat6中)使用与Tomcat7无缝协作的tomcat jdbc池
When I try to run my webapp (in Tomcat6) that use tomcat jdbc pool that seamlessly work with Tomcat7
我已将这些jar包含在tomcat 6 lib文件夹中:
I have included these jars already in tomcat 6 lib folder:
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"
I得到此警告:
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;
推荐答案
作为最终用户,你永远不应该直接使用 ConnectionPoolDataSource
。它旨在作为物理连接的工厂( PooledConnection
)。那个 PooledConnection
保存在连接池中。当您执行 DataSource.getConnection
时,数据源将从池中检出 PooledConnection
,并返回逻辑连接使用 PooledConnection.getConnection()
作为最终用户获得,并在逻辑连接$ c时将物理连接返回到池$ c>已关闭。
As an end-user you should never ever have to use a ConnectionPoolDataSource
directly. It is intended as a factory for physical connections (the PooledConnection
). That PooledConnection
is kept in the connectionpool. When you do a DataSource.getConnection
, the datasource will check out a PooledConnection
from the pool, and return the logical connection obtained using PooledConnection.getConnection()
to you as the end-user, and returning the physical connection to the pool when that logical Connection
is closed.
所以构造是
User -- uses --> DataSource (with connectionpooling) -- uses --> ConnectionPoolDataSource
或
ConnectionPoolDataSource --> creates PooledConnection --> DataSource --> returns Connection --> User
使用 DataSource
是独立的事实上,如果 DataSource
提供或不提供连接池(它应该对你透明)。
The use of a DataSource
is independent of the fact if that DataSource
provides connectionpooling or not (it should be transparent to you).
参见a以前对我的回答是另一个问题: https://stackoverflow.com/a/12651163/466862
See also a previous answer of mine to another question: https://stackoverflow.com/a/12651163/466862
换句话说,您的代码应更改为:
In other words your code should be changed to:
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;
}
这篇关于ClassCastException无法将数据源强制转换为javax.sql.ConnectionPoolDataSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!