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.
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:
Connection conn = DriverManager.getConnection(
"jdbc:apache:commons:dbcp:test_pool"
);
So the format is:
"jdbc:apache:commons:dbcp:" + TheNameOfYourPool