Open topic with navigation
NuoDB includes a
DataSource class that encompasses the
DriverManager#getConnection() method. The advantage of using the
DataSource class is that it hides many complexities associated with managing NuoDB connections, such as dealing with NuoDB Broker redundancy and reconnecting to the NuoDB database if a transaction process terminates. The
DataSource class can be used for connection pooling (by default, on) and/or statement caching (by default, off).
Note: Internally, the
DataSource class maintains a pool of connections for you and ensures that each thread in a multi-threaded application gets its own connection. This is important due to the distributed nature of the NuoDB architecture.
DataSource class takes a
Properties object specifying properties for the pool, and properties for connections in the pool. The following connection properties are supported:
|username||The connection username to be passed to the NuoDB JDBC driver to establish a connection, unless
|password||The connection password to be passed to the JDBC driver to establish a connection, unless
|defaultSchema||The default schema of connections created by this pool.||-|
|defaultReadOnly||The default read-only state of connections created by this pool. If not set, then readOnly is determined by the
|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
|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|
|maxIdle||The maximum number of connections that should be kept in the pool at all times.||maxActive|
|minIdle||The minimum number of established connections (either active or idle) that should be kept in the pool at all times.||initialSize|
|maxWait||The maximum number of milliseconds that the pool waits (when there are no available connections) for a connection to be returned before throwing an exception.||0|
|maxAge||Time in milliseconds to keep this 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
|maxStatementsPerConnection||The number of statements cached for a single pooled connection. If you set a non-zero value, statement caching is enabled and maxStatements = 0 global limit is not enforced. For more information, see Statement Caching.||0|
|testOnReturn||The indication of whether objects are validated before being returned to the pool. If set to true, the
|testOnBorrow||The indication of whether objects are validated before being borrowed the pool. If the object fails to validate, it is dropped from the pool and an attempt to borrow another is made. If
|testWhileIdle||The indication of whether objects are validated by the idle object evictor. If an object fails to validate, it is dropped from the pool. If
|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
|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
|timeBetweenEvictionRunsMillis||The number of 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|
|url||The URL of the NuoDB Broker. If required, this may be followed by the URLs of the secondary Broker(s) to be contacted if the primary Broker is unreachable. URLs must be separated by the character defined by the
|url-delimiter||The delimiter used to separate the URLs of the brokers in the value of the
For example, using the default delimiter (a comma) a URL containing two hosts might be
Note: host1 and host2 identify the location of admin processes.
The following code shows how to use the
DataSource class outside of an application server that is running the NuoDB JDBC driver:
Properties p = new Properties(); p.setProperty(DataSource.PROP_URL, "jdbc:com.nuodb://localhost/database"); p.setProperty(DataSource.PROP_USERNAME, "user"); p.setProperty(DataSource.PROP_PASSWORD, "password"); DataSource ds = new DataSource(p); con = ds.getConnection(); ....
Alternatively, the following code shows how to use a
DataSource from within an application server.
DataSource ds = (DataSource)initContext.lookup("jdbc/nuoDB"); Connection con = ds.getConnection(); Statement stmt = con.createStatement()