Apache Commons DBCP Provider

The org.mybatis.guice.datasource.dbcp package contains Google Guice providers for Apache Commons DBCP Data Source implementations.

for Apache Maven users the Apache Commons DBCP dependency is not transitive, so they have to take care about including it in their applications.

Basic Data Source Provider

The org.mybatis.guice.datasource.dbcp.BasicDataSourceProvider is the provider that builds the org.apache.commons.dbcp.BasicDataSource and helps users to configure it.

The table below contains all of the available properties for the Provider; for more details please consult the Official reference.

org.mybatis.guice.datasource.dbcp.BasicDataSourceProvider properties
Property Required Bind to Java Type
JDBC.driver true java.lang.String
JDBC.url true java.lang.String
JDBC.username true java.lang.String
JDBC.password true java.lang.String
JDBC.autoCommit false boolean
JDBC.driverProperties false java.util.Properties
DBCP.accessToUnderlyingConnectionAllowed false boolean
DBCP.defaultCatalog false java.lang.String
DBCP.defaultReadOnly false boolean
DBCP.defaultTransactionIsolation false int
DBCP.initialSize false int
DBCP.maxActive false int
DBCP.maxIdle false int
DBCP.maxOpenPreparedStatements false int
DBCP.maxWait false long
DBCP.minEvictableIdleTimeMillis false long
DBCP.minIdle false int
DBCP.numTestsPerEvictionRun false int
DBCP.poolPreparedStatements false boolean
DBCP.testOnBorrow false boolean
DBCP.testOnReturn false boolean
DBCP.testWhileIdle false boolean
DBCP.validationQuery false java.lang.String

Shared Pool Data Source

The org.mybatis.guice.datasource.dbcp.SharedPoolDataSourceProvider is the provider that builds the org.apache.commons.dbcp.datasources.SharedPoolDataSource and helps users to configure it.

Warning this provider requires a javax.sql.ConnectionPoolDataSource existing binding in order to be instantiated.

The table below contains all of the available properties for the Provider:

org.mybatis.guice.datasource.dbcp.SharedPoolDataSourceProvider properties
Property Required Bind to Java Type
JDBC.autoCommit false boolean
JDBC.loginTimeout false int
DBCP.defaultReadOnly false boolean
DBCP.defaultTransactionIsolation false int
DBCP.maxActive false int
DBCP.maxIdle false int
DBCP.maxWait false int
DBCP.minEvictableIdleTimeMillis false int
DBCP.numTestsPerEvictionRun false int
DBCP.testOnBorrow false boolean
DBCP.testOnReturn false boolean
DBCP.testWhileIdle false boolean
DBCP.validationQuery false java.lang.String
DBCP.name false java.lang.String
DBCP.jndi.key false java.lang.String
DBCP.jndi.value false java.lang.String
DBCP.rollbackAfterValidation false boolean
DBCP.timeBetweenEvictionRunsMillis false int
DBCP.description false java.lang.String

Per User Pool Data Source

The org.mybatis.guice.datasource.dbcp.PerUserPoolDataSourceProvider is the provider that builds the org.apache.commons.dbcp.datasources.PerUserPoolDataSource and helps users to configure it.

Warning this provider requires a javax.sql.ConnectionPoolDataSource existing binding in order to be instantiated.

The table below contains all of the available properties for the Provider:

org.mybatis.guice.datasource.dbcp.PerUserPoolDataSourceProvider properties
Property Required Bind to Java Type
JDBC.autoCommit false boolean
JDBC.loginTimeout false int
DBCP.defaultReadOnly false boolean
DBCP.defaultTransactionIsolation false int
DBCP.minEvictableIdleTimeMillis false int
DBCP.numTestsPerEvictionRun false int
DBCP.testOnBorrow false boolean
DBCP.testOnReturn false boolean
DBCP.testWhileIdle false boolean
DBCP.validationQuery false java.lang.String
DBCP.name false java.lang.String
DBCP.jndi.key false java.lang.String
DBCP.jndi.value false java.lang.String
DBCP.rollbackAfterValidation false boolean
DBCP.timeBetweenEvictionRunsMillis false int
DBCP.maxActive false int
DBCP.maxIdle false int
DBCP.maxWait false int
DBCP.description false java.lang.String

Since all these settings can't be easily covered using jakarta.inject.Named annotations, and we don't want to put a limit on how users can retrieve these informations, the library provides the org.mybatis.guice.datasource.dbcp.PerUserPoolDataSourceModule module that alleviates the task of binding per users properties.

So, developers have to write their own com.google.inject.Provider for mapped properties and binding them through the module; keep in mind that every provider is optional so, if not specified, properties won't be set.

Let's take a look at an example that shows how to set the perUserDefaultAutoCommit properties; first of all, let's implement the provider that communicates the user bindings:

import jakarta.inject.Provider;
import jakarta.inject.Named;

class MyPerUserDefaultAutoCommit implements Provider<Map<String, Boolean>> {

    private final Map<String, Boolean> perUserDefaultAutoCommitMap;

    public MyPerUserDefaultAutoCommit(
            @Named("perUserDefaultAutoCommit.url") URL url) {
        // reads the URL and populate a map
    }

    public Map<String, Boolean> get() {
        return this.perUserDefaultAutoCommitMap;
    }

}

then, create the org.mybatis.guice.datasource.dbcp.PerUserPoolDataSourceModule and create the context:

import org.mybatis.guice.core.SqlSessionFactoryModule;
import org.mybatis.guice.datasource.dbcp.PerUserPoolDataSourceModule;
import org.mybatis.guice.datasource.dbcp.PerUserPoolDataSourceProvider;

Injector injector = Guice.createInjector(
    new MyBatisModule(PerUserPoolDataSourceProvider.class),
    new PerUserPoolDataSourceModule.Builder()
        .setPerUserDefaultAutoCommitProviderClass(
            MyPerUserDefaultAutoCommit.class).create();
);

That's all :)

Warning it's strongly reccommended to use the same org.mybatis.guice.datasource.dbcp.PerUserPoolDataSourceModule instance to set also the other per user bindings, resumed in the following table

org.mybatis.guice.datasource.dbcp.PerUserPoolDataSourceModule properties
Provider Type to bind
Map<String, Boolean> perUserDefaultAutoCommit
Map<String, Boolean> perUserDefaultReadOnly
Map<String, Integer> perUserDefaultTransactionIsolation
Map<String, Integer> perUserMaxActive
Map<String, Integer> perUserMaxIdle
Map<String, Integer> perUserMaxWait

Connection Pool Data Source driver adapter

The org.mybatis.guice.datasource.dbcp.DriverAdapterCPDSProvider provides the org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS adapter for jdbc drivers that do not include an implementation of javax.sql.ConnectionPoolDataSource.

The table below contains all of the available properties for the Provider:

org.mybatis.guice.datasource.dbcp.DriverAdapterCPDSProvider properties
Property Required Bind to Java Type
JDBC.driver true java.lang.String
JDBC.url true java.lang.String
JDBC.username true java.lang.String
JDBC.password true java.lang.String
JDBC.loginTimeout false int
DBCP.maxActive false int
DBCP.maxIdle false int
DBCP.minEvictableIdleTimeMillis false int
DBCP.numTestsPerEvictionRun false int
DBCP.poolPreparedStatements false boolean
DBCP.timeBetweenEvictionRunsMillis false int
DBCP.maxOpenPreparedStatements false int
DBCP.description false java.lang.String