Question

So, I realize there are probably better options for database pooling than JDBC, but still something is driving me crazy. In all the example code they suggest pooling like this.

GenericObjectPool connectionPool = new GenericObjectPool(null);

ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, un, pw);

PoolableConnectionFactory poolableConnectionFactory = 
           new PoolableConnectionFactory(connectionFactory, connectionPool, null, "SELECT 1", false, true);
PoolingDataSource dataSource = new PoolingDataSource(connectionPool);

return dataSource;

Why is the PoolableConnectionFactory created? It is never used, and from looking at the source, I can't see that it actually ties anything together in the constructor or does anything magical besides setting it's own private variables. The JDBC site is rather cryptic with their information and all my digging through source code has left me confused. Is there a reason to do this? Apparently you can alternatively just instantiate the object with new without actually assigning it to a variable and that will suffice... confusing me even further.

Was it helpful?

Solution

Assuming these classes are commons-DBCP classes, the constructor of PoolableConnectionFactory registers the instance being created as the factory of the pool pased as argument:

_pool = pool;
_pool.setFactory(this);

So, creating the factory creates it, and makes the pool use it to create new connections when needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top