Specifying Properties on the Connection URL

You can specify connection properties on the JDBC URL directly, as shown:

jdbc://com.nuodb://ap-host[:port]/database_name?[connection_properties]

where connection properties are a set of key-value pairs separated by ampersands (&).

This is an important feature for situations where you control only the JDBC URL and none of the underlying Java code. Similar flexibility can be acheived by initializing the DataSource from a properties file (see this example).

Recognized properties can be found at Connection Properties.

Using a DataSource

Connection properties can only be specified in the JDBC URL:

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

String url = new StringBuilder().append(DATABASE_URL)
                .append("?isolation=read_committed")     (2)
                .append("&clientInfo=MyApp")             (3)
                .toString();

Properties properties = new Properties();
properties.put(DataSource.PROP_URL, url);
properties.put(DataSource.PROP_USER, "dba");
properties.put(DataSource.PROP_PASSWORD, "goalie");
properties.put(DataSource.PROP_SCHEMA, "Hockey");

javax.sql.DataSource dataSource = new com.nuodb.jdbc.DataSource(properties);
1 We are connecting to a local database named test.
2 Specifying read_committed as the default transaction isolation level.
3 Setting MyApp as the clientInfo
Setting clientInfo allows connections from this client to be identified by the CLIENTINFO column of the SYSTEM.CONNECTIONS and SYSTEM.LOCALCONNECTIONS system tables.

Using a DriverManager

You can also set properties programmatically using a Driver Manager, as shown below, but remember this approach is less favored since only the DataSource has connection-pooling built-in.

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

Properties connectProperties = new Properties();
connectProperties.put("user", DB_USER);
connectProperties.put("password", DB_PASSWORD);
connectProperties.put("schema", "test");
connectProperties.put("isolation", "read_committed");
connectProperties.put("clientInfo", "MyApp");

try {
    connection = DriverManager.getConnection(DATABASE_URL, connectProperties);
    connection.setAutoCommit(false);
} catch (SQLException e) {
    throw new RuntimeException(
        "Cannot open database " + database + " at " + DATABASE_URL +
            "; check user credentials and AP status.", e );
}