Specifying Properties on the Connection URL
You can specify connection properties on the JDBC URL directly, as shown:
jdbc://com.nuodb://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
In the example below:
-
We are connecting to a database named
test
. -
Specifying
read_committed
as the default transaction isolation level. -
Setting
MyApp
as theclientInfo
-
Allows connections from this client to be identified by the
CLIENTINFO
column of theSYSTEM.CONNECTIONS
andSYSTEM.LOCALCONNECTIONS
system tables.
-
Properties properties = new Properties();
properties.put(DataSource.PROP_URL,
"jdbc:com.nuodb://localhost/test?isolation=read_committed&clientInfo=MyApp");
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);
Using a DriverManager
You can also set properties programmatically, as shown below, but remember this approach is less favored since only the DataSource
has connection-pooling built-in.
String connectURL = "jdbc:com.nuodb://broker_host:48004/my_database";
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(connectURL, connectProperties);
connection.setAutoCommit(false);
} catch (SQLException e) {
throw new RuntimeException(
"Cannot open database " + database + " at " + connectURL +
"; check user credentials and AP status.", e );
}