Third-Party Data Sources
NuoDB’s own DataSource offers connection pooling but several other third-party data sources (such as Hikari, DBCP2, and C3P0) can be used instead.
All of these libraries can be used stand-alone, with Spring or with Spring Boot.
| NuoDB neither endorses nor recommends any of the data source libraries discussed here. |
Best Practices
For each DataSource, we recommend the following:
-
Setting a timeout so that connections will automatically be closed and replaced periodically.
-
Enabling testOnBorrow so that each connection is validated as it is borrowed from the pool to be used.
-
Set maximum time for a connection to live so that APs can periodically rebalance connections and also create connections to new TEs (typically after scaling out). The properties are Hikari:
maxLifetime, DBCP2:maxConnLifetimeMillisor C3P0:maxConnectionAge.
|
The following assumptions apply to all the examples on this page.
|
Hikari Configuration
Hikari is a widely used third-party DataSource which can be used with NuoDB.
It provides configuration properties similar to NuoDB DataSource.
Create a properties file
Create a properties file that includes the following recommended properties:
| Hikari Property | Description | Recommended Value | Mandatory |
|---|---|---|---|
|
Use NuoDB’s Driver class. |
|
Yes |
|
Connection URL for database |
|
Yes |
|
Database user to connect as. |
Application specific |
Yes |
|
Password for database user. |
Application specific |
Yes |
|
Enables connection aging
equivalent to Time in milliseconds, defaults to |
Application specific |
No |
|
Maximum time an application will wait for a connection from the pool, equivalent to |
Application specific |
No |
|
Maximum time a connection will sit idle in the pool before it is closed.
Idle connections are only closed if there are more than |
Application specific |
No |
|
For other configuration properties, see Hikari Configuration.
To use Hikari with Spring Boot, see below.
driverClassName=com.nuodb.jdbc.Driver
jdbcUrl=jdbc:com.nuodb://localhost/test?schema=HOCKEY
username=dba
password=dba
maxLifetime=600000
connectionTimeout=5000
minimumIdle=10
maximumPoolSize=20
Apache Commons DBCP2 Configuration
Create a properties file
Create a properties file that includes the following recommended properties:
| DBCP Property | Description | Recommended Value | Mandatory |
|---|---|---|---|
|
Use NuoDB’s Driver class. |
|
Yes |
|
Connection URL for database |
|
Yes |
|
Database user to connect as. |
Application specific |
Yes |
|
Password for database user. |
Application specific |
Yes |
|
Enables connection aging
equivalent to |
xref:/client-development/nuodb-java-jdbc-driver/notes-for-using-java-jdbc-driver/working-with-timeouts.adoc#determine-connection-aging-value[Application specific |
No |
|
How long to wait for a connection from the pool, equivalent to |
Application specific |
No |
|
Maximum time a connection will sit idle in the pool before it is closed.
Time in microseconds, defaults to |
Application specific |
No |
|
Should each connection be validated as it is taken from the pool - invokes |
|
No |
driverClassName=com.nuodb.jdbc.Driver
url=jdbc:com.nuodb://localhost/test?schema=HOCKEY
username=dba
password=dba
maxConnLifetimeMillis=600000
maxWaitMillis=5000
initialSize=10
maxTotal=20
testOnBorrow=true
For other configuration properties, see BasicDataSource Configuration Parameter. To use DBCP2 with Spring Boot, see DBCP2 Using Spring Boot.
DBCP defaults to an initially empty pool that can grow up to 8 connections (initialSize=0, maxTotal=8, maxIdle=8, minIdle=0).
By default, it does not validate each connection before providing it to the application.
|
Load the properties file and create a data source
To load the file and create a Apache Commons DBCP2 DataSource, include the following in the Java source file:
Properties props = new Properties();
props.load(new FileReader("/path/to/dbcp2.properties"));
BasicDataSource datasource = BasicDataSourceFactory.createDataSource(props);
c3p0 Configuration
c3p0 is another pooled DataSource library and is the only one integrated into Hibernate (see below).
c3p0 supports several configuration options.
Perhaps the simplest is to create the default c3p0.properties configuration file at the root of your classpath.
Create a properties file
Create a properties file called c3p0.properties at the root of your classpath.
Include the following recommended properties in the file:
| c3p0 Property | Description | Recommended Value | Mandatory |
|---|---|---|---|
|
Use NuoDB’s Driver class. |
|
Yes |
|
Connection URL for database |
|
Yes |
|
Database user to connect as. |
Application specific |
Yes |
|
Password for database user. |
Application specific |
Yes |
|
Enables connection aging
equivalent to |
No |
|
|
How long to wait for a connection from the pool, equivalent to |
Application specific |
No |
|
Should each connection be validated as it is taken from the pool - run a query to perform the check.
Defaults to |
|
No |
|
Validation query used to check a connection when |
|
No |
c3p0.driverClass=com.nuodb.jdbc.Driver
c3p0.jdbcUrl=jdbc:com.nuodb://localhost/test?schema=HOCKEY
c3p0.user=dba
c3p0.password=goalie
c3p0.maxConnectionAge=600
c3p0.checkoutTimeout=5000
c3p0.minPoolSize=10
c3p0.maxPoolSize=20
c3p0.testConnectionOnCheckout=true
c3p0.preferredTestQuery=SELECT 1 /* Name of Application */ FROM DUAL
If using Spring Boot, the driverClass, jdbcUrl, user, and password properties in c3p0.properties are ignored.
Specify the equivalent spring.datasource properties (see below).
|
|
For other configuration properties, see c3p0 Configuration Properties. To use c3p0 with Spring Boot, see c3p0 Using Spring Boot.
Create a data source
Creating a c3p0 DataSource instance will automatically load the properties file, if it exists at the root of the classpath. To create a c3p0 data source, include the following in the Java source file:
ComboPooledDataSource datasource = new ComboPooledDataSource();
c3p0 and Hibernate
To use with Hibernate, set the same recommended properties either in the hibernate.cfg.xml file or the hibernate.properties file.
<property name="hibernate.c3p0.max_connection_age">600</property>
<property name="hibernate.c3p0.checkout_timeout">5000</property>
hibernate.c3p0.max_connection_age=600
hibernate.c3p0.checkout_timeout=5000
For more on using Hibernate to configure c3p0, see Using c3p0 with Hibernate.
Spring Boot Configuration
When configuring your application using Spring Boot, HikariCP is used as the default connection pool. Both DBCP2 and c3p0 are also supported.
Note the following:
-
The
spring-boot-starter-jdbcJAR must be on the classpath. -
The dedicated configuration properties for HikariCP and DBCP2 are specified in
application.properties. The properties use the prefixspring.datasource.hikari.*for HikariCP andspring.datasource.dbcp2.*for DBCP2. -
The default configuration file to configure c3p0 is
c3p0.propertiesif it is present at the root of the classpath. -
To explicitly specify the connection pool, use
spring.datasource.type. -
Regardless of the connection pool, the following connection properties must be specified using
spring.datasource.xxxinapplication.properties:-
URL
-
driver class
-
username
-
password
-
-
Properties are specified using kebab-case. For example,
max-size.
| Alternatively, Spring Boot can be configured to use a NuoDB DataSource. |
Hikari Using Spring Boot
Hikari is the default connection pool when using Spring Boot.
It can also be specified using spring.datasource.type=com.zaxxer.hikari.HikariDataSource.
Specify the additional properties in the application.properties file.
For more properties, see Hikari Configuration.
# NuoDB's driver class
spring.datasource.driver-class-name=com.nuodb.jdbc.Driver
# Database URL
spring.datasource.url=jdbc:com.nuodb://localhost/test?schema=HOCKEY
# Username and password
spring.datasource.username=dba
spring.datasource.password=goalie
# Hikari specific properties
spring.datasource.hikari.minimum-idle=10
spring.datasource.hikari.maximum-pool-size=20
spring.datasource.hikari.max-lifetime=600000
spring.datasource.hikari.connection-timeout=5000
For a full list of Spring Boot’s Hikari properties, see Spring Boot’s Common Application Properties page.
DBCP2 Using Spring Boot
DBCP2 must be requested explicitly and the DBCP2 JAR must be on the classpath. For more properties, see Apache Commons DBCP2 Configuration.
# Request DBCP2
spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource
# NuoDB's driver class
spring.datasource.driver-class-name=com.nuodb.jdbc.Driver
# Database URL
spring.datasource.url=jdbc:com.nuodb://localhost/test?schema=HOCKEY
# Username and password
spring.datasource.username=dba
spring.datasource.password=goalie
# DBCP specific properties
spring.datasource.dbcp2.initial-size=10
spring.datasource.dbcp2.max-total=20
spring.datasource.dbcp2.max-conn-lifetime-millis=600000
spring.datasource.dbcp2.max-wait-millis=5000
For a full list of Spring Boot’s DBCP2 properties, see Spring Boot’s Common Application Properties page.
c3p0 Using Spring Boot
Request c3p0 using spring.datasource.type and ensure the c3p0 JAR is on the classpath.
There are no Spring Boot properties specifically for c3p0, but you must still specify spring.datasource.driver-class-name, spring.datasource.url, spring.datasource.username, and spring.datasource.password.
# Request c3p0
spring.datasource.type=com.mchange.v2.c3p0.ComboPooledDataSource
# NuoDB's driver class
spring.datasource.driver-class-name=com.nuodb.jdbc.Driver
# Database URL
spring.datasource.url=jdbc:com.nuodb://localhost/test?schema=HOCKEY
# Username and password
spring.datasource.username=dba
spring.datasource.password=goalie
For more properties, see c3p0 Configuration in the c3p0.properties file.
Values for c3p0.driverClass, c3p0.jdbcUrl, c3p0.user, and c3p0.password in the properties file are ignored.
|