DataSource Configuration

Frequently an application only needs to access a single database in the course of a transaction. In these circumstances, you can avoid the overhead of a full XA transaction by using a local- or no- transaction configuration.

To provide maximum compatibility with drivers, his type of configuration uses the JDBC Driver type configuation mechanism rather than a DataSource.

Connection Transaction Types

A datasource is configued in a -ds.xml file using a <no-tx-datasource> or <local-tx-datasource> element.

A <no-tx-datasource> does not support transactional behaviour; all operations are atomic in themselves just as if autoCommit was true for the underlying JDBC Connection.

A <local-tx-datasource> supports transactional behaviour by delegating the transaction to the underlying database. However, the duration of the transaction is determined by the Transaction Manager and the commit and rollback methods must not be used by an application.

Connection Properties

The following elements are used to configure the JDBC connection:

<jndi-name> Required
Once
The name with which this DataSource will be bound into JNDI. A value of "MyDS" will be bound to the absolute name "java:/MyDS".
<connection-url> Required
Once
The JDBC connection URL to use when creating a connection. The syntax of the URL is driver specific.
<driver-class> Required
Once
The fully qualified class name for the JDBC driver.
<connection-property> Optional
Multiple

Additional property values that will be supplied to the JDBC driver when obtaining a connection. For example:

<connection-property name="databaseName">
  myDatabase
</connection-property>
<user-name> Required*
Once
The username to be used when obtaining a connection. This element is required unless other security is configured.
<password> Required*
Once
The password to be used when obtaining a connection. This element is required unless other security is configured.

An example of a very simple configuration for connecting to an Oracle database would be:

<datasources>
<local-tx-datasource>
<jndi-name>MyOracleDS</jndi-name>
<connection-url>jdbc:oracle:thin:@host:1521:sid</connection-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<user-name>scott</user-name>
<password>tiger</password>
</local-tx-datasource>
</datasources>

Connection Management

Once a connection has been obtained, the following elements can be used to determine how it is managed by the connector:

<min-pool-size> Default 0
Once
The minimum number of connections that will be retained in the connection pool.
<max-pool-size> Default 20
Once
The maximum number of connections that will be entered in the connection pool.
<blocking-timeout-millis> Default 5000
Once
The amount of time that a thread will wait for a connection to be released if the pool is full. If less than <max-pool-size> connections have been allocated, the thread nevers blocks but instead allocates a new connection.
<idle-timeout-minutes> Default 15
Once

The approximate time that a connection will remain idle before being closed. Idle connections are checked for at an interval of half the lowest value for all connection pools; thus a connection may actually remain in the pool for between 1 and 1.5 times this value.

This value can be used to prevent errors when the underlying driver automatically closes idle connections.

<transaction-isolation> Optional
Once

The isolation level to use for this connection.

This can be specified using the constants defined in java.sql.Connection:

  • TRANSACTION_READ_UNCOMMITTED
  • TRANSACTION_READ_COMMITTED
  • TRANSACTION_REPEATABLE_READ
  • TRANSACTION_REPEATABLE_READ

Alternatively an integer value can be used to specify driver-specific levels.

If omitted, this will default to the default isolation level provided by the database and driver.

<new-connection-sql> Optional
Once
An SQL statement that will be executed immediately after a connection is created.
<valid-connection-sql> Optional
Once

An SQL statement that will be executed whenever a connection is obtained from the pool to determine if it is still usable. Any SQLException thrown will cause the connection to be discarded before it is returned to the application.

See Handling Connection Errors for more information.

<exception-sorter-class-name>

Optional
Once

The fully qualifed name of a class which can be used to detect SQLExceptions that indicate unusable connections. Should such an SQLException be thrown, then the connection will immediately be discarded from the pool.

See Handling Connection Errors for more information.