connectionconnection-poolingguicec3p0bonecp

Configuring connection pool size with guice, mybatis, and c3p0 or bonecp


I'm using Guice/Mybatis to manage and execute my queries to a MySQL database. My class that sets up the connection and extends MyBatisModule contains these relevant bits:

Properties myBatisProperties = new Properties();
myBatisProperties.setProperty("c3p0.maxPoolSize", "5");
myBatisProperties.setProperty("c3p0.initialPoolSize", "5");
bindDataSourceProviderType(C3p0DataSourceProvider.class);
bindTransactionFactoryType(JdbcTransactionFactory.class);
addMapperClass(getMapperClass());
Names.bindProperties(binder(), myBatisProperties);
bind(getBindServiceClass()).to(getBindServiceToClass());

When I configure either using c3p0 or bonecp using its properties of bonecp.maxConnectionsPerPartition and bonecp.partitionCount, and the appropriate DataSourceProvider.class in place of C3p0DataSourceProvider, both c3p0 and bonecp ignore my max connections configuration and just open (and hold open) a new connection for every query until mysql runs out of available connections.

When I use the default mybatis pool provider PooledDataSourceProvider and its properties, mybatis.pooled.maximumActiveConnections and mybatis.pooled.maximumIdleConnections, it respects my configuration and pools connections correctly.

What am I doing wrong?

Further documentation for this is available at http://mybatis.github.io/guice/datasources.html


Solution

  • It looks like the problem was due to the my creating a new injector every time. Specifically this line:

    Injector injector = Guice.createInjector(new MyDAOPoolModule()); return injector.getInstance(MyDAOService.class);

    If instead I make the injector a singleton, it only creates the one pool.