DataSource Properties

The following properties can be specified as a connection property or a DataSource property:

  • Login credentials: Specify the user and password connection properties in the connection URL or as username and password DataSource properties. For security reasons defining these in the URL is not recommended.

  • Default schema: Specify schema in the connection URL or use the DataSource property defaultSchema.

The remaining properties supported by NuoDB’s DataSource allow the internal connection pool to be configured.

Property Description Default

defaultAutoCommit

The default auto-commit state of connections created by this pool. If not set, the default is the NuoDB JDBC driver default. (If not set, the setAutoCommit() method is not called.)

No default

defaultReadOnly

The default read-only state of connections created by this pool. If not set, then read-only state is determined by the Protocol.IsReadOnly which retrieves the default value of false from the server.

No default

defaultSchema

The default schema of connections created by this pool. If not specified, the database will default to the USER schema.

No default

initialSize

The initial number of connections that are created when the pool is started.

10

maxActive

The maximum number of connections (either active or idle) that can be allocated from this pool at the same time.

100

maxAge

Time in milliseconds to keep a connection. When a connection is returned to the pool, the pool checks to see if the current time less the time when the connection was created has reached maxAge. If so, it closes the connection rather than returning it to the pool. A value of 0 means that connections are left open and no age check is performed.

0 (disabled)

maxIdle

The maximum number of connections that should be kept in the pool at all times.

maxActive

maxWait

Time in milliseconds that the pool waits (when there are no available connections) for a connection to be returned before throwing an exception.

0 (wait forever)

minIdle

The minimum number of established connections (either active or idle) that should be kept in the pool at all times.

initialSize

password

The connection password to be passed to the JDBC driver to establish a connection, unless DataSource#getConnection(username,password) is used.

Mandatory, no default

testOnBorrow

Indicates whether objects are validated before being borrowed from the pool. If the object fails to validate, it is dropped from the pool and an attempt to borrow another is made. If testOnBorrow is set to true, the validationQuery property must be set to a non-null string or no test is performed. For information on more efficient validation, see validationInterval property.

false

testOnReturn

Indicates whether objects are validated before being returned to the pool. If set to true, the validationQuery property must be set to a non-null string or no test is performed. For information on more efficient validation, see validationInterval property.

false

testWhileIdle

Indicates whether objects are validated by the idle object evictor thread. If an object fails to validate, it is dropped from the pool. If testWhileIdle is set to true, you must set the validationQuery property to a non-null string or no test is performed. For information on more efficient validation, see validationInterval property.

false

timeBetweenEvictionRunsMillis

Time in milliseconds to sleep between runs of the idle connection validation/cleaner thread. This value should not be set under 1 second. It dictates how often we check for idle, abandoned connections, and how often we validate idle connections.

5000

validationInterval

To avoid excess validation, only run validation at most at this frequency, given in milliseconds. If a connection is due for validation, but has been validated previously within this interval, it is not validated again. See testOnReturn, testOnBorrow and testWhileIdle.

30000

validationQuery

The SQL query used to validate connections from this pool.

  • If specified, this query does not have to return any data, it just can’t throw a SQLException. See testOnReturn, testOnBorrow and testWhileIdle.

  • A commonly used query is SELECT 1 /* My app name */ FROM DUAL - putting the application name in the query helps to identify the queries when SQL logging.

null

url

The URL of the NuoDB database. Takes the format:
"jdbc:com.nuodb://<hosts>/<database-name>?<params>"
For example: jdbc:com.nuodb://localhost/test?schema=Hockey.

  • <hosts> defines one or more APs to use to request connections to TEs. Hosts must be separated by the character defined by the url-delimiter property (by default, a comma).

  • For more information, see NuoDB Connection URL for more information.

Mandatory, no default

url-delimiter

The delimiter used to separate the URLs of the APs in the value of the url property.

  • For example, using the default delimiter (a comma) a URL containing two hosts might be jdbc:com.nuodb://ap-host1,ap-host2/testdb.

  • Note that ap-host1 and ap-host2 identify the location of admin processes.

, (comma)

username

The connection username to be passed to the NuoDB JDBC driver to establish a connection, unless DataSource#getConnection(username,password) is used.

Mandatory, no default

Configure DataSource

The NuoDB com.nuodb.jdbc.DataSource class can be configured in any of the following three ways:

  • Using the setter methods. For example to set the maxIdle property, invoke DataSource.setMaxIdle(50).

  • Set each property in a Java Properties object and pass it to the DataSource constructor.

    Each property name is defined by a corresponding PROP_XXX constant on the DataSource class:

    Properties properties = new Properties();
    ...
    properties.put(DataSource.PROP_MAX_IDLE, 50);
    DataSource dataSource = new DataSource(properties);
  • Load the Java Properties object from a properties file.

    This allows the properties to be externalized, they can be changed without the need to recompile the code, and is recommended.

    url=jdbc:com.nuodb://localhost/test
    ...
    maxIdle=50

Examples

Example 1: Validating Connections

validationQuery is used to check the validity of a connection. The validity of a connection can also be checked using the Connection.isValid() method used by Third-Party Data Sources. However, if testOnReturn, testOnBorrow, or testWhileIdle are enabled, validationQuery must be provided.

The following example shows how to enable a validation query to check the validity of a connection.

public static final String DATABASE_URL = "jdbc:com.nuodb://localhost/test";

Properties p = new Properties();
p.setProperty(DataSource.PROP_URL, DATABASE_URL);
p.setProperty(DataSource.PROP_USERNAME, user);
p.setProperty(DataSource.PROP_PASSWORD, password);
p.setProperty(DataSource.PROP_TESTONBORROW, "true");
p.setProperty(DataSource.PROP_VALIDATIONQUERY, "SELECT 1 /* My App Name */ FROM DUAL");

DataSource ds = new DataSource(p);
con = ds.getConnection();
....

The comment /* My App Name */ in the query helps identify validation queries from different clients when viewing logs or System tables.

  • Validating connections reduces the likelihood of the client receiving a connection to a TE that is no longer available. However it does not guarantee the connection is valid. Always catch SQLTransientException and retry by getting a new connection to a different TE (similar to retrying after deadlock).

  • Validating a connection requires executing an additional query. Consider whether this overhead is acceptable.

Example 2: Working with JNDI

The following example uses DataSource from within an application server via a JNDI lookup.

The name to lookup (in this example, jdbc/nuoDB) is not predefined. Any name may be used, according to how your server is set up.
DataSource ds = (DataSource)initialContext.lookup("jdbc/nuoDB");

try (Connection con = ds.getConnection()) { ... use connection ... }

For example, see Defining a JNDI Resource.