日期:2014-05-20  浏览次数:20778 次

高手请进,帮忙看一下连接池的异常
严重: Servlet.service() for servlet [LoginServlet] in context with path [/chart] threw exception [org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper cannot be cast to org.apache.tomcat.dbcp.dbcp.BasicDataSource] with root cause
javax.servlet.ServletException: org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper cannot be cast to org.apache.tomcat.dbcp.dbcp.BasicDataSource

连接可以取得,但是执行SQL和关闭资源就报错误

------解决方案--------------------
不太懂,类型转换出问题了。。
------解决方案--------------------
你的连接必须转化成池化连接。
------解决方案--------------------
看不出你connection内部获得的细节

Tomcat uses the Apache Commons DBCP package (see http://jakarta.apache.org/commons/dbcp/) for database connection pooling. When you lookup a DataSource object using JNDI, like this:

Context context = new InitialContext();
DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/ari");

the object that you get is an instance of class org.apache.commons.dbcp.BasicDataSource. When you call getConnection on this object, you get an instance of class org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper (which obviously implements java.sql.Connection, and wraps the OracleConnection object).

https://forums.oracle.com/forums/thread.jspa?threadID=279238

可能如果从BasicDataSource中获得的PoolGuardConnectionWrapper实例的connection的话就不会报这种类型转换错误了
------解决方案--------------------
看了org.apache.tomcat.dbcp.dbcp源码,感觉这两者之间没有直接关联,正常情况应该是不会类型转换的
PoolGuardConnectionWrapper
BasicDataSource

public class BasicDataSource implements DataSource 

private class PoolGuardConnectionWrapper extends
DelegatingConnection

没有异常发生的具体行号位置吗?
------解决方案--------------------
LZ看下你是否把数据源引错了
------解决方案--------------------
cannot be cast to org.apache.tomcat.dbcp.dbcp.BasicDataSource
这句说的很清楚,你BasicDataSourceFactory.createDataSource(properties)无法强制类型转换为BasicDataSource类型。;