简体   繁体   English

DBCP 池出错:“java.sql.SQLException:找不到配置文件”

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

I am getting the error:我收到错误:

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)

I checked the connection and there's nothing wrong with it.我检查了连接,没有任何问题。 It's just a pool error.这只是一个池错误。 If I don't use the pool but open a connection directly (using the connectionFactory below), I can connect and execute a statement and get a result set.如果我不使用池而是直接打开连接(使用下面的connectionFactory ),我可以连接并执行语句并获得结果集。

The code to create and use the pool:创建和使用池的代码:

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

The issue is with how I was asking for a connection.问题在于我如何请求连接。 You need to ask it for your pool underneath Apache's driver URL:您需要在 Apache 的驱动程序 URL 下询问您的池:

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

So the format is:所以格式是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM