DBCP 池出错:"java.sql.SQLException: Configuration file not found"

Getting error with DBCP Pool: "java.sql.SQLException: Configuration file not found"

我收到错误:

java.sql.SQLException: Configuration file not found
        at org.apache.commons.dbcp.PoolingDriver.getConnectionPool(PoolingDriver.java:137)
        at org.apache.commons.dbcp.PoolingDriver.connect(PoolingDriver.java:175)
        at java.sql.DriverManager.getConnection(DriverManager.java:664)
        at java.sql.DriverManager.getConnection(DriverManager.java:270)
        at com.test.sql.Test.main(Model.java:95)

我检查了连接,没有任何问题。这只是一个池错误。如果我不使用池而是直接打开一个连接(使用下面的connectionFactory),我可以连接并执行一条语句并得到一个结果集。

创建和使用池的代码:

    AbandonedConfig cfg = new AbandonedConfig ();
    cfg.setLogAbandoned (true);
    cfg.setRemoveAbandonedTimeout (5);
    cfg.setRemoveAbandoned (true);
    GenericObjectPool connectionPool = new AbandonedObjectPool(null, cfg);
    connectionPool.setTestWhileIdle (true);
    connectionPool.setTestOnBorrow (true);
    connectionPool.setTestOnReturn (true);
    connectionPool.setMaxActive (5);
    connectionPool.setMaxWait (5000);
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://localhost:3306/Test?user=testuser&password=password",null);
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true, cfg);
    poolableConnectionFactory.setValidationQuery ("SELECT 1");

    Class.forName("org.apache.commons.dbcp.PoolingDriver");
    PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
    driver.registerPool("test_pool",connectionPool);


    //This throws the error
    Connection conn = DriverManager.getConnection( "jdbc:apache:commons:dbcp:" );

    //This does too
    //Connection conn = DriverManager.getConnection( "jdbc:apache:commons:dbcp:Test" );

问题出在我请求连接的方式上。您需要在 Apache 的驱动程序 URL:

下询问您的池
Connection conn = DriverManager.getConnection( 
     "jdbc:apache:commons:dbcp:test_pool"
);

所以格式是:

"jdbc:apache:commons:dbcp:" + TheNameOfYourPool