严重: 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和关闭资源就报错误
javax.servlet.ServletException: org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper cannot be cast to org.apache.tomcat.dbcp.dbcp.BasicDataSource连接可以取得,但是执行SQL和关闭资源就报错误
public class LoginFactory {
Connection connection;
CallableStatement callableStatement;
ResultSet resultSet; public UserBean loginControl(String uid,String pwd) throws Exception{
UserBean userBean = new UserBean();
connection = ConnectionPoolInit.getConnection();
System.out.println(connection); callableStatement = connection.prepareCall("{call usp_web_login (?,?)}");
callableStatement.setString(1,uid);
callableStatement.setString(2, pwd);
resultSet = callableStatement.executeQuery(); while (resultSet.next()){
userBean.setUserId(uid);
userBean.setUserName(resultSet.getString("username"));
userBean.setLogInfo(resultSet.getString("loginfo"));
}
ConnectionPoolInit.close(resultSet,callableStatement,connection);
return userBean;
}
}
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的话就不会报这种类型转换错误了
public final class ConnectionPoolInit {
private static BasicDataSource basicDataSource = null; private ConnectionPoolInit(){} public static void createDataSource() throws Exception{
Properties properties = new Properties();
InputStream inputStream = ConnectionPoolInit.class.
getClassLoader().getResourceAsStream("dbcpconfig.properties");
properties.load(inputStream);
basicDataSource = (BasicDataSource)BasicDataSourceFactory.createDataSource(properties);
} public static DataSource getDataSource(){
return basicDataSource;
} public static synchronized Connection getConnection() throws Exception{
return basicDataSource.getConnection();
} public static void close(ResultSet resultSet,Statement statement,Connection connection) throws Exception{
try{
if (resultSet != null) {
resultSet.close();
}
} catch (Exception e){
throw new Exception(e.getMessage());
} finally {
try{
if (statement != null) {
statement.close();
}
} catch (Exception e) {
throw new Exception(e.getMessage());
} finally {
try{
if (connection != null) {
((BasicDataSource)connection).close();
}
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
}
}
}
PoolGuardConnectionWrapper
BasicDataSourcepublic class BasicDataSource implements DataSource private class PoolGuardConnectionWrapper extends
DelegatingConnection没有异常发生的具体行号位置吗?
这句说的很清楚,你BasicDataSourceFactory.createDataSource(properties)无法强制类型转换为BasicDataSource类型。;