严重: 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和关闭资源就报错误

解决方案 »

  1.   

    帮我看一下怎样转为连接池,多谢
    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;
        }
    }
      

  2.   

    看不出你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的话就不会报这种类型转换错误了
      

  3.   

    这是连接池的实现,谢谢,帮我看一下
    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());
                    }
                }
            }
        }
    }
      

  4.   

    看了org.apache.tomcat.dbcp.dbcp源码,感觉这两者之间没有直接关联,正常情况应该是不会类型转换的
    PoolGuardConnectionWrapper
    BasicDataSourcepublic class BasicDataSource implements  DataSource private class PoolGuardConnectionWrapper extends
                        DelegatingConnection没有异常发生的具体行号位置吗?
      

  5.   

    数据驱动用的JTDS第三方sql server驱动driverClassName=net.sourceforge.jtds.jdbc.Driver,tomcat7.0,sqlserver2000,jdk1.7。用微软官方的驱动提示同样的错误
      

  6.   

    cannot be cast to org.apache.tomcat.dbcp.dbcp.BasicDataSource
    这句说的很清楚,你BasicDataSourceFactory.createDataSource(properties)无法强制类型转换为BasicDataSource类型。;
      

  7.   

    问题已经解决,原来是apache的实现有问题,现在换了c3p0就好了,郁闷被折磨了好几天。推荐c3p0希望兄弟们不要走弯路了