DevPortal

How do I configure Datasource for J2EE Applications?


Table of Contents


How do I configure Datasource for J2EE Applications?

A datasource provides pooled connections to applications. The physical connections are obtained from a JDBC Connection Factory and pooled by the datasource. It also provides other configurable features such as connection validation, pool sizes, connection time out, and pool refresh interval.

A datasource on Server is always associated with a JDBC Connection Factory. Multiple datasources can be associated with the same JDBC Connection Factory.

The JDBC API supports two extensions of the datasource interface. These extensions are:

The X/Open CAE specification (XA) facilitates two-phase commit in transactions and forms a part of the JDBC 2.0 Standard Extension.

Note: In this guide, datasource has been used to define both datasource and XA datasource.

How do I manually create JDBC Resources?

You can configure a datasource manually by using the resource-config.xml. To add a JDBC connection, add a datasource tag under the datasources tag. The resource-config.xml appears as below:

<resource-service>
  <resources>
    <default-resources> </default-resources>
    <datasources>
      <datasource connection-factory="testcf" description="No Description" name="demo" transaction-participation="true">
        <pool-properties cache-size="20" connection-request-timeout-seconds="" idle-timeout-seconds="" initial-pool-size="1" max-pool-size="20" min-pool-size="5" refresh-interval-seconds=""/>
        <connection-initialization db-retrials="0" db-retry-interval-millis="0"/>
        <connection-validation class="" on-acquire="false" on-release="false" sql=""/>
        <connection-tracking enable="false"/>
      </datasource>
    </datasources>
    <connection-factories> <connection-factories>
    <mail-resources> <mail-resources>
    <url-resources> <url-resources>
    <jms-adapters> </jms-adapters>
  <resources>
</resource-service>

The fields name and connection-factory are mandatory for the datasource tag.

How do I use Resource Shell to create Datasources?

To add a datasource using the Resource Shell, on the resourceadmin:> prompt, enter:resourceadmin:> add_datasource

There are two ways to add a datasource using add_datasource.

Interactive

To use the interactive mode, enter add_datasource, and press Enter. This asks for all the required fields one by one. No special flag is required for this mode.

Enter the following values needed for the command resourceadmin:> add_datasource

resourceadmin>  - jndi name * #my_datasource
resourceadmin>  - connection factory name * #my_jdbc_cf
resourceadmin>  - initial pool size [2] #1
resourceadmin>  - max pool size [20] #20
resourceadmin>  - min pool size [10] #10
resourceadmin>  - prepared statement cache size [30] #
resourceadmin>  - transaction participation [true] #
resourceadmin>  - transaction isolation level #
resourceadmin>  - validation sql # (the SQL statement which will be used
by the Server to validate connections in the pool)
resourceadmin>  - validator class # (the name of the validator class
which will be used by the Server to validate connections in the pool)
Creating Data Source my_datasource
Data Source [my_datasource] started
Created Data Source my_datasource
Added DataSource [my_datasource]

Note: Fields followed by * are mandatory. Press Enter for the default values. Typing 'q' and pressing Enter at any point cancels the current command.

Non-interactive

To use the non-interactive mode, enter add_datasource. All the required attributes - the JNDI name and the JDBC Connection Factory Name in this case - have to be specified. All optional attributes have default values that can be modified.

There are three ways to specify the fields:

How do I use JDBC Resource MBean to create Datasources?

Obtain the Resource Service MBean and call the JDBCResourceMBean by entering JDBCResourceMBean jdbcResource= resourceServiceMBean.getJDBCResourceMBean();

Call the createDataSource method on this MBean. Depending upon the requirement, you can call one the described methods:

Method 1
* @param jndiName: JNDI name of the data source.
*
* @ param connectionFactoryName: JNDI name of the connection factory for the datasource.
*
* @ param initialPoolSize: Initial number of database connections to be
* opened at the starting of the datasource.
*
* @param minPoolSize: Minimum number of database connections opened simultaneously
* at any point of time.
*
* @param maxPoolSize: Maximum number of database connections opened simultaneously
* at any point of time.
*
* @throws ResourceException: If some problem occurs in datasource creation.
* e.g. JNDI name may already be in use.
*/
public void createDataSource(String jndiName, String connectionFactoryName,
int minPoolSize, int maxPoolSize)throws ResourceException;

Method 2
* @param jndiName: JNDI name of the data source.
*
* @ param connectionFactoryName: JNDI name of the connection factory for the datasource
*
* @param minPoolSize: Minimum number of database connections opened simultaneously
* at any point of time.
*
* @param maxPoolSize: Maximum number of database connections opened simultaneously
* at any point of time.
*
* @param preparedStatementCacheSize: size of the preparedStatementCache
*
* @param participateInTransaction: indicates whether connection from datasource
* should participate in transaction
*
* @param transactionIsolation: the isolation level that has to be set on all the
* connections from the datasource
*
* @param validationSQL: an SQL that would be used to validate a connection
*
* @param validatorClass: a validation class that would be used to validate the
* connections
*
* @throws ResourceException: If some problem occurs in datasource creation.
* e.g. JNDI name may already be in use.
*/
public void createDataSource(String jndiName, String connectionFactoryName,
int minPoolSize,int maxPoolSize, Integer preparedStatementCacheSize,
Boolean participateInTransaction, String transactionIsolation,
String validationSQL, String validatorClass) throws ResourceException;

How do I configure Datasources using the Console?

To configure a datasource on the Console, select Configure > Resources. On the screen, you can see the following details related to the existing datasources:

Table 1: Details for an existing datasource

FieldsDescription
NameThe JNDI name of the datasource. This is a mandatory field and must be unique.
JDBC Connection FactoryName of the JDBC Connection Factory being used by the datasource.
Max Pool SizeThe maximum number of connections to be retained in the connection pool.
Min Pool SizeThe minimum number of connections to be obtained and retained in the connection pool.
PropertiesUse Edit to modify and view the existing properties for the datasource.
AddUse Add to add a new datasource.
DeleteUse Delete to delete a selected datasource.

Adding Datasources

Click Add to add a datasource.

You can choose to create a datasource on an existing JDBC Connection Factory, or create a new Connection Factory.

Table 2: Details needed for creating a datasource on a new Connection Factory

FieldsOptionsDescription
Driver/XA Datasource-XA datasource provides connections that can participate in distributed transactions. Selecting a driver type automatically provides values for the Class and Connection URL fields.
-Oracle DBFor details about the drivers used, refer to Table 7 below.
-SQL DBFor details about the drivers used, refer to Table 7 below.
-XA datasource implementationFor XA data sources, the vendor provides the class that implements javax.sql.XADataSource interface. JDBC vendors that support distributed transactions must implement these interfaces. If Database is IBM Informix, enter the class as com.informix.jdbcx.IfxXADataSource. If Database is Oracle, enter the class as oracle.jdbc.xa.client.OracleXADataSource.
-OthersYou can use other drivers, or other XA datasource implementations. You must provide appropriate values for the class and the Connection URL fields.
Class-JDBC driver or the XA datasource implementation class name is mandatory and is provided by the database vendor. It is used to access the database. Pramati Server supports JDBC 2.0 Drivers.
Connection URL-Refers to the location. If database is Cloudscape, type URL as jdbc:cloudscape:. If database is Oracle, type URL as jdbc:oracle:thin:@.
JNDI Name-Clients locate a resource by using the JNDI name. It is used as a reference to obtain all information about the resource and is unique in the server name space. Connection Factory JNDI name is mandatory.
Classpath-This option allows you to specify the location of the driver/implementation classes or jars. So different versions of the same driver can be used in the same instance of a running Server. For example, you can create a datasource with OracleDriver version 3, and create another one with version 4. This is possible since the driver jars are not picked up from a common location, but are taken from the path specified in the input box. In case you do not specify the path, the Server attempts to pick them up from the \lib\ext directory under the Server installation directory, or from the system classpath.
Authorized by-Provides the authentication required to access the database. The application or container can provide authentication information. The authorization mode can be pre-defined.
-ApplicationThis field does not require you to supply user name and password. They will be provided by the application while connecting to the Connection Factory.
-ContainerThis field requires you to supply user name and password at the time of creation.
--User name: required to connect to the database.
--Password: required to authenticate access.

Table 3: Drivers supported by the Pramati Server

Name of DatabaseName of Driver
Oracle databaseoracle.jdbc.driver.OracleDriver
Merant's Oracle drivercom.merant.datadirect.jdbc.oracle.OracleDriver
Informix JDBC drivercom.informix.jdbc.IfxDriver
Merant's Informix JDBC drivercom.merant.datadirect.jdbc.informix.InformixDriver
Cloudscape Embedded drivercom.cloudscape.core.JDBCDriver
Cloudscape RMI drivercom.cloudscape.core.RmiJdbcDriver
Merant's SQL Server Drivercom.merant.datadirect.jdbc.sqlserver.SQLServerDriver
JTurbo driver for SQL Servercom.ashna.jturbo.driver.Driver

Click Next>> to add a datasource and set its properties.

Table 4: Step 2 of adding a datasource

FieldsDescription
Connection FactoryThis is the non-editable JDBC Connection Factory JNDI name associated with the datasource that you are creating.
JNDI NameSpecify here a JNDI name for the datasource.
Max Pool SizeSpecify here the maximum number of connections to be retained in the connection pool.
Min Pool SizeSpecify here the minimum number of connections to be retained in the connection pool.
Initial Pool SizeSpecify here the number of connections the connection pool will have when the datasource is created for the first time.
Idle TimeoutSpecify here the time interval in seconds after which a connection in the pool is closed if no request arrives.
Connection Request TimeoutSpecify here the time interval in seconds for which an application can wait after making a connection request. After this interval, an exception is thrown to the application.
Cached Prepared StatementsSpecify here the number of prepared statements to be cached.
Validation SQLSpecify here the SQL statement which the Server will use to validate connections in the pool*.
Validator ClassSpecify here the name of the validator class which the Server will use to validate connections in the pool*.
TransactionsSelect here whether or not connections from this datasource can participate in transactions.
Transaction Isolation LevelEvery connection from the datasource will be set to this transaction isolation level. If the transaction isolation level is not supported by the database or nothing is specified, then the default transaction level supported by the database will be used. The Isolation level can be selected from the drop-down list provided. The options provided are: Transaction None, Transaction Read Committed, Transaction Read Uncommitted, Transaction Repeatable Read, and Transaction Serializable.

Note: *Only one of Validation SQL or Validator Class will be used. If none are specified, the Server will use the internal connection validator. Click Save to add the new datasource.

Editing Datasources

After creating a datasource, click Properties against the JNDI name to be edited. You can view details for JNDI name, Class Name, Authorized by Server, and the URL and User Properties of the specific datasource. The fields that can be modified are Maximum Pool Size, Minimum Pool Size, Initial Pool Size, Refresh Interval, Idle Timeout, and Connection Refresh Timeout.

The Console manages all necessary validations.

Performance Tuning Tips

In this section we will discuss a few performance tuning tips that can be used to enhance the performance of the Server. These values can be provided at the time of creating the resource and can be modified at runtime using the Console.

Max Pool Size

This represents the maximum number of connection objects that are assigned to a pool. Maintaining a pool of datasource objects avoids the overhead of creating an object every time a request for a connection object is made. The default value for the connection pool size is 20.

This value should be set to the maximum number of connections that can be used at any given time. Setting a value lower than this would result in connections waiting for release of other connections that are in use at peak time.

The resource service ensures that the maximum pool size is not exceeded. If the limit is reached, and a new connection request is made when other connections are busy, the connection pool waits till a connection is free.

Min Pool Size

This value represents the minimum number of connections to be retained in the connection pool at any given time. If the number of requests exceed this value, a new connection object is taken from the pool and served. The default value is 5. For optimum performance, set this value to the average number of connections used at any given time.

Initial Pool Size

This value represents the number of connections the connection pool will have when the datasource is created for the first time. The default value is 1.

The primary purpose of this parameter is to allow faster response times for connection requests when the application uses the datasource pool for the first time.

Idle Timeout

The time interval in seconds after which a connection in the pool is closed if no request arrives. The default value is 300 seconds.

Tuning this value depends on a number of parameters. For example, if connection requests arrive at uneven intervals, this can result in pre-mature close of connections and would require connections to be created on subsequent requests, thus consuming processing time.

On the other hand, if this value is set to too high a value, connections would be idle in the pool, consuming memory. Ideally this value should be set to the average intervals at which connections are requested.

Connection Request Timeout

The time interval in seconds for which an application can wait after making a connection request. After this interval, an exception is thrown to the application.

The default value is 10 seconds.

This parameter should be configured carefully - setting too low a value might result in application connection requests getting timed out prematurely, while setting too high a value might make a connection request wait for longer than is necessary.

Cached Prepared Statements

This option represents the number of prepared statements that should be cached. The caching is done for each connection, that is, each connection has it's own prepared statement cache. Specify here the number of prepared statements to be cached. The value for cache is application dependent. For optimal performance, this value should be equal to the number of prepared statements in the application.

The default value is 50.

Deleting Datasources

Select the check box provided for the Datasource to be deleted on the Resources page, and click Delete. This deletes the selected datasource.


Related Topics:

© Pramati Technologies 2007 Runs on Pramati Server | Feedback | Legal