Data source

Your application needs to connect to one or more databases? Void Framework is capable of handling multiple data sources at the same time through the use of DataSourceManager. It will provide all the necessary methods to obtain a connection from the desired data source. Each data source can be configured independently. Your application can, for example, be connected to PostgreSQL and Oracle at the same time.


Installation

Void Framework offers different implementations, depending on the implementation chosen, the configuration keys may change. Here you will find all the information to fully configure each implementation.

C3P0
<dependency>
    <groupId>dev.voidframework</groupId>
    <artifactId>voidframework-datasource-c3p0</artifactId>
    <version>1.16.0</version>
</dependency>
HikariCP
<dependency>
    <groupId>dev.voidframework</groupId>
    <artifactId>voidframework-datasource-hikaricp</artifactId>
    <version>1.16.0</version>
</dependency>

Configuration

The following configuration keys can be used in the configuration file of your application:

  • required voidframework.datasource.default.driver the driver to be used to communicate with the database.
  • required voidframework.datasource.default.url the JDBC format URL to be used to reach the database.
  • required voidframework.datasource.default.username the username to be provided durint the authentication step.
  • required voidframework.datasource.default.password the password to be provided durint the authentication step.
  • optional C3P0 only voidframework.datasource.default.statementCacheNumDeferredCloseThreads the number of threads to track when Connections are in use, and only destroy Statements when their parent Connections are not otherwise in use.
  • optional HikariCP only voidframework.datasource.default.cachePrepStmts enable or disable the prepared statements cache.
  • optional voidframework.datasource.default.prepStmtCacheSize the number of prepared statements cache to keep in the cache.
  • optional HikariCP only voidframework.datasource.default.prepStmtCacheSqlLimit the size of the largest SQL query for which the parsing result will be keep in the cache.
  • optional voidframework.datasource.default.autoCommit enable or disable the auto commit behavior when the connection goes back into the pool.
  • optional HikariCP only voidframework.datasource.default.connectionInitSql the SQL statement that will be executed after every new connection.
  • optional HikariCP only voidframework.datasource.default.connectionTestQuery the SQL statement that will be executed to test if the connection is still valid.
  • optional voidframework.datasource.default.connectionTimeout the milliseconds to wait before timing out during the connection.
  • optional voidframework.datasource.default.idleTimeout the milliseconds to wait before closing an unused connection.
  • optional HikariCP only voidframework.datasource.default.keepaliveTime the milliseconds to wait before attempting to keep the connection alive.
  • optional voidframework.datasource.default.minimumIdle the minimum number of alive connections in the pool.
  • optional voidframework.datasource.default.maximumPoolSize the maximum number of connections allowed in the pool.
  • optional C3P0 only voidframework.datasource.default.acquireIncrement determines how many connections at a time C3P0 will try to acquire when the pool is exhausted.
  • optional voidframework.datasource.default.maxConnectionAge the milliseconds to wait before closing a connection.

Usage

@Service
public class ExampleDataSourceService {

    private final Provider<DataSourceManager> dataSourceManagerProvider;

    @Inject
    public ExampleService(final Provider<DataSourceManager> dataSourceManagerProvider) {
        this.dataSourceManagerProvider = dataSourceManagerProvider;
    }

    public void example() throws SQLException {
      final Connection conn = dataSourceManagerProvider
          .get()
          .getConnection("default");
      /* ... */
      conn.close();
    }
}