DataSource setup

The com.google.code.ibaguice.core.datasource package of ibaguice-core module comes with an easy-to-use set of classes that makes easier the built-in iBatis javax.sql.DataSource creation using Guice, through configurable Data Source Providers.

Configurable means that users are free to bind com.google.inject.name.Named properties and let Guice injects them.

Moreover we added, in separated modules, the support for the popular:

Unpooled Data Source Provider

The com.google.code.ibaguice.core.datasource.UnpooledDataSourceProvider is the provider that builds the org.apache.ibatis.datasource.unpooled.UnpooledDataSource and helps users to configure it.

Follows below the resume table of supported properties:

Property Required Bind 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.loginTimeout false int
JDBC.driverProperties false java.util.Properties

Pooled Data Source Provider

The com.google.code.ibaguice.core.datasource.PooledDataSourceProvider is the provider that builds the org.apache.ibatis.datasource.pooled.PooledDataSource and helps users to configure it.

Follows below the resume table of supported properties:

Property Required Bind 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.loginTimeout false int
JDBC.driverProperties false java.util.Properties

JNDI Data Source Provider

The com.google.code.ibaguice.core.datasource.JndiDataSourceProvider is the provider that builds the org.apache.ibatis.datasource.jndi.JndiDataSourceFactory and helps users to configure it.

Follows below the resume table of supported properties:

Property Required Bind Java Type
jndi.initialContext false java.lang.String
jndi.dataSource false java.lang.String

Apache DBCP Provider

Users that want to use the Apache DBCP have to add in the pom.xml the following dependency:

<dependencies>
    ...
    <dependency>
        <groupId>com.google.code.ibaguice.datasource</groupId>
        <artifactId>ibaguice-dbcp</artifactId>
        <version>XX.XX</version>
    </dependency>
    ...
</dependencies>

Basic Data Source Provider

The com.google.code.ibaguice.datasource.dbcp.BasicDataSourceProvider is the provider that builds the org.apache.commons.dbcp.BasicDataSource and helps users to configure it.

Follows below the resume table of supported properties; for more details please consult the Official reference.

Property Required Bind 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.loginTimeout false int
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
com.google.code.ibaguice.datasource.dbcp.BasicDataSourceProvider properties

Shared Pool Data Source

The com.google.code.ibaguice.datasource.dbcp.SharedPoolDataSourceProvider is the provider that builds the org.apache.commons.dbcp.datasources.SharedPoolDataSource and helps users to configure it.

NOTE This class requires a javax.sql.ConnectionPoolDataSource existing binding in order to be instantiated.

Follows below the resume table of supported properties:

Property Required Bind 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
com.google.code.ibaguice.datasource.dbcp.SharedPoolDataSourceProvider properties

Per User Pool DataSource

The com.google.code.ibaguice.datasource.dbcp.PerUserPoolDataSourceProvider is the provider that builds the org.apache.commons.dbcp.datasources.PerUserPoolDataSource and helps users to configure it.

NOTE This class requires a javax.sql.ConnectionPoolDataSource existing binding in order to be instantiated.

Follows below the resume table of supported properties:

Property Required Bind 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
com.google.code.ibaguice.datasource.dbcp.PerUserPoolDataSourceProvider properties

Configuring 'PerUser' properties since these settings can't be easily covered using com.google.inject.name.Named annotations, and we don't want to put a limit on how users can retrieve these informations, the library provides the com.google.code.ibaguice.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 he user bindings:

import com.google.inject.Provider;
import com.google.inject.name.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 com.google.code.ibaguice.datasource.dbcp.PerUserPoolDataSourceModule and binding it:

PerUserPoolDataSourceModule perUserPoolDataSourceModule = new PerUserPoolDataSourceModule();
perUserPoolDataSourceModule.setPerUserDefaultAutoCommitProviderClass(MyPerUserDefaultAutoCommit.class);

So, finally you're ready to create the context:

import com.google.code.ibaguice.core.SqlSessionFactoryModule;
import com.google.code.ibaguice.datasource.dbcp.PerUserPoolDataSourceModule;
import com.google.code.ibaguice.datasource.dbcp.PerUserPoolDataSourceProvider;

Injector injector = Guice.createInjector(
    new SqlSessionFactoryModule(PerUserPoolDataSourceProvider.class),
    perUserPoolDataSourceModule
);

that's all :)

Note it's strongly reccommended to use the same com.google.code.ibaguice.datasource.dbcp.PerUserPoolDataSourceModule to set also the other 'per user' bindings, resumed in the following table:

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 com.google.code.ibaguice.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.

Follows below the resume table of supported properties:

Property Required Bind 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
com.google.code.ibaguice.datasource.dbcp.DriverAdapterCPDSProvider properties

C3P0 Provider

The com.google.code.ibaguice.datasource.C3p0DataSourceProvider is the provider that builds the com.mchange.v2.c3p0.ComboPooledDataSource and helps users to configure it.

Users that want to use the C3P0 have to add in the pom.xml the following dependency:

<dependencies>
    ...
    <dependency>
        <groupId>com.google.code.ibaguice.datasource</groupId>
        <artifactId>ibaguice-c3p0</artifactId>
        <version>XX.XX</version>
    </dependency>
    ...
</dependencies>

Follows below the resume table of supported properties; for more details please consult the Official reference.

Property Required Bind 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
c3p0.acquireIncrement false int
c3p0.acquireRetryAttempts false int
c3p0.acquireRetryDelay false int
c3p0.automaticTestTable false java.lang.String
c3p0.breakAfterAcquireFailure false boolean
c3p0.checkoutTimeout false int
c3p0.connectionCustomizerClassName false java.lang.String
c3p0.connectionTesterClassName false java.lang.String
c3p0.idleConnectionTestPeriod false int
c3p0.initialPoolSize false int
c3p0.maxAdministrativeTaskTime false int
c3p0.maxConnectionAge false int
c3p0.maxIdleTime false int
c3p0.maxIdleTimeExcessConnections false int
c3p0.maxPoolSize false int
c3p0.maxStatements false int
c3p0.maxStatementsPerConnection false int
c3p0.minPoolSize false int
c3p0.preferredTestQuery false java.lang.String
c3p0.propertyCycle false int
c3p0.testConnectionOnCheckin false boolean
c3p0.testConnectionOnCheckout false boolean
c3p0.unreturnedConnectionTimeout false int
c3p0.usesTraditionalReflectiveProxies false boolean