DataSource Properties
The following properties can be specified as a connection property or a DataSource property:
-
Login credentials: Specify the
userandpasswordconnection properties in the connection URL or asusernameandpasswordDataSource properties. For security reasons defining these in the URL is not recommended. -
Default schema: Specify
schemain the connection URL or use theDataSourcepropertydefaultSchema.
The remaining properties supported by NuoDB’s DataSource allow the internal connection pool to be configured.
| Property | Description | Default |
|---|---|---|
|
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 |
No default |
|
The default read-only state of connections created by this pool.
If not set, then read-only state is determined by the |
No default |
|
The default schema of connections created by this pool.
If not specified, the database will default to the |
No default |
|
The initial number of connections that are created when the pool is started. |
|
|
The maximum number of connections (either active or idle) that can be allocated from this pool at the same time. |
|
|
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 |
|
|
The maximum number of connections that should be kept in the pool at all times. |
|
|
Time in milliseconds that the pool waits (when there are no available connections) for a connection to be returned before throwing an exception. |
|
|
The minimum number of established connections (either active or idle) that should be kept in the pool at all times. |
|
|
The connection password to be passed to the JDBC driver to establish a connection, unless |
Mandatory, no default |
|
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 |
|
|
Indicates whether objects are validated before being returned to the pool.
If set to true, the |
|
|
Indicates whether objects are validated by the idle object evictor thread.
If an object fails to validate, it is dropped from the pool.
If |
|
|
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. |
|
|
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 |
|
|
The SQL query used to validate connections from this pool.
|
|
|
The URL of the NuoDB database.
Takes the format:
|
Mandatory, no default |
|
The delimiter used to separate the URLs of the APs in the value of the
|
|
|
The connection username to be passed to the NuoDB JDBC driver to establish a connection, unless |
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
maxIdleproperty, invokeDataSource.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.
|
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.