1

我收到错误消息:

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" );
4

1 回答 1

1

问题在于我如何要求建立联系。您需要在 Apache 的驱动程序 URL 下向它询问您的池:

Connection conn = DriverManager.getConnection( 
     "jdbc:apache:commons:dbcp:test_pool"
);

所以格式是:

"jdbc:apache:commons:dbcp:" + TheNameOfYourPool
于 2016-05-02T22:44:18.140 回答