Programming WebLogic Resource Adapters
The following sections describe connection management in WebLogic Server resource adapters. For more information on connection management, see Chapter 6, "Connection Management," of the J2CA 1.5 Specification.
Connection Management Contract
One of the requirements of the J2CA 1.5 Specification is the connection management contract. The connection management contract between WebLogic Server and a resource adapter:
- Provides a consistent application programming model for connection acquisition for both managed and non-managed (two-tier) applications.
- Enables a resource adapter to provide a connection factory and connection interfaces based on the common client interface (CCI) specific to the type of resource adapter and EIS. This enables JDBC drivers to be aligned with the J2EE 1.5 Connector Architecture with minimum impact on the existing JDBC APIs.
- Enables an application server to provide various services—transactions, security, advanced pooling, error tracing/logging—for its configured set of resource adapters.
- Supports connection pooling.
The resource adapter's side of the connection management contract is embodied in the resource adapter's
Connection Factory and Connection
A J2EE application component uses a public interface called a connection factory to access a connection instance, which the component then uses to connect to the underlying EIS. Examples of connections include database connections and JMS (Java Message Service) connections.
A resource adapter provides connection and connection factory interfaces, acting as a connection factory for EIS connections. For example, thejavax.sql.DataSource and java.sql.Connection interfaces are JDBC-based interfaces for connecting to a relational database.
An application looks up a connection factory instance in the Java Naming and Directory Interface (JNDI) namespace and uses it to obtain EIS connections. See Obtaining the ConnectionFactory (Client-JNDI Interaction).
Resource Adapters Bound in JNDI Tree
Version 1.5 resource adapters can be bound in the JNDI tree as independent objects, making them available as system resources in their own right or as message sources for message-driven beans (MDBs). In contrast, version 1.0 resource adapters are identified by their
ConnectionFactory objects bound in the JNDI tree.
In a version 1.5 resource adapter, at deployment time, the
ResourceAdapter Bean (if it exists) is bound into the JNDI tree using the value of the jndi-name element in the weblogic-ra.xml file. As a result, administrators can view resource adapters as single deployable entities, and they can interact with resource adapter capabilities publicly exposed by the resource adapter provider. For more information, see jndi-name in weblogic-ra.xml Schema.
Obtaining the ConnectionFactory (Client-JNDI Interaction)
The application assembler or component provider configures the Connection Factory requirements for an application component in the application's deployment descriptor. For example:
res-auth: Application or Container
The resource adapter deployer provides the configuration information for the resource adapter.
An application looks up a
ConnectionFactory instance in the Java Naming and Directory Interface (JNDI) namespace and uses it to obtain EIS connections. The following events occur when an application in a managed environment obtains a connection to an EIS instance from a Connection Factory, as specified in the res-type variable.
Note: A managed application environment defines an operational environment for a J2EE-based, multi-tier, Web-enabled application that accesses EISes.
- The application server uses a configured resource adapter to create physical connections to the underlying EIS.
- The application component looks up a
ConnectionFactory instance in the component's environment by using the JNDI interface, as shown in Listing 5-1.
Listing 5-1 JNDI Lookup
//obtain the initial JNDI Naming context
Context initctx = new InitialContext();
// perform JNDI lookup to obtain the connection factory
javax.resource.cci.ConnectionFactory cxf =
The JNDI name passed in the method NamingContext.lookup is the same as that specified in the res-ref-name element of the deployment descriptor. The JNDI lookup results in an instance of type java.resource.cci.ConnectionFactory as specified in the res-type element.
- The application component invokes the getConnection method on the ConnectionFactory to obtain an EIS connection. The returned connection instance represents an application level handle to an underlying physical connection. An application component obtains multiple connections by calling the method getConnection on the connection factory multiple times.
javax.resource.cci.Connection cx = cxf.getConnection();
- The application component uses the returned connection to access the underlying EIS.
- After the component finishes with the connection, it closes the connection using the close method on the Connection interface:
If an application component fails to close an allocated connection after its use, that connection is considered an unused connection. The application server manages the cleanup of unused connections.
Configuring Outbound Connections
Outbound resource adapters based on the J2CA 1.5 Specification can be configured to have one or more outbound connections, each having its own WebLogic Server-specific authentication and transaction support. You configure outbound connection properties in the ra.xml and weblogic-ra.xml deployment descriptor files.
Connection Pool Configuration Levels
You use the outbound-resource-adapter element and its subelements in the weblogic-ra.xml deployment descriptor to describe the outbound components of a resource adapter.
You can define outbound connection pools at three levels:
- Global—Specify parameters that apply to all outbound connection groups in the resource adapter using the default-connection-properties element. See default-connection-properties.
- Group—Specify parameters that apply to all outbound connection instances belonging to a particular connection factory specified in the ra.xml deployment descriptor using the connection-definition-group element. A one-to-one correspondence exists from a connection factory in ra.xml to a connection definition group in weblogic-ra.xml. The properties specified in a group override any parameters specified at the global level. See connection-definition-group.
The connection-factory-interface element (a subelement of the connection-definition-group element) serves as a required unique element (a key) to each connection-definition-group. There must be a one-to-one relationship between the connection-definition-interface element in weblogic-ra.xml and the connectiondefinition-interface element in ra.xml.
- Instance—Under each connection definition group, you can specify connection instances using the connection-instance element of the weblogic-ra.xml deployment descriptor. These correspond to the individual connection pools for the resource adapter. You can use the
connection-properties subelement to specify properties at the instance level too; properties specified at the instance level override those provided at the group and global levels. See connection-instance.
Multiple Outbound Connections Example
The following is an example of a weblogic-ra.xml deployment descriptor that configures multiple outbound connections:
Listing 5-2 weblogic-ra.xml Deployment Descriptor: Multiple Outbound Connections
<?xml version="1.0" ?>
Configuring Inbound Connections
The J2CA 1.5 Specification permits you to configure a resource adapter to support inbound message connections. The following are the main steps for configuring an inbound connection:
- Provide a JNDI name for the resource adapter in the weblogic-ra.xml deployment descriptor. See jndi-name.
- Configure a message listener and
ActivationSpec for each supported inbound message type in the ra.xml deployment descriptor. For information about requirements for an
ActivationSpec class, see Chapter 12, "Message Inflow" in the J2CA 1.5 Specification.
- Within the packaged enterprise application, include a configured EJB message-driven bean (MDB). In the resource-adapter-jndi-name element of the weblogic-ejb-jar.xml deployment descriptor, provide the same JNDI name assigned to the resource adapter in the previous step. Setting this value enables the MDB and resource adapter to communicate with each other.
- Deploy the resource adapter as discussed in Deploying WebLogic Server Applications.
- Deploy the MDB. For more information, see Message-Driven EJBs in Programming WebLogic Enterprise JavaBeans and Deploying WebLogic Server Applications.
The following example shows how an inbound connection with two message listener/activation specs could be configured in the ra.xml deployment descriptor:
Listing 5-3 Example of Configuring an Inbound Connection
Configuring Connection Pool Parameters
This section explains how to configure WebLogic Server resource adapter connection pool parameters in the weblogic-ra.xml deployment descriptor. For more details, see weblogic-ra.xml Schema.
initial-capacity: Setting the Initial Number of ManagedConnections
Depending on the complexity of the Enterprise Information System (EIS) that the
ManagedConnection is representing, creating
ManagedConnections can be expensive. You may decide to populate the connection pool with an initial number of
ManagedConnections upon startup of WebLogic Server and therefore avoid creating them at run time. You can configure this setting using the initial-capacity element in the weblogic-ra.xml descriptor file. The default value for this element is 1
Because no initiating security principal or request context information is known at WebLogic Server startup, you create initial connections using a security subject that you specify in the security element, as described in Configuring Security Identities for Resource Adapters.
max-capacity: Setting the Maximum Number of ManagedConnections
ManagedConnections are created, they consume more system resources—such as memory and disk space. Depending on the Enterprise Information System (EIS), this consumption may affect the performance of the overall system. To control the effects of
ManagedConnections on system resources, you can specify a maximum number of allocated
ManagedConnections in the max-capacity element of the weblogic-ra.xml descriptor file.
If a new
ManagedConnection (or more than one
ManagedConnection in the case of capacity-increment being greater than one) needs to be created during a connection request, WebLogic Server ensures that no more than the maximum number of allowed
ManagedConnections are created. Requests for newly allocated
ManagedConnections beyond this limit results in a ResourceAllocationException being returned to the caller.
capacity-increment: Controlling the Number of ManagedConnections
In compliance with the J2CA 1.5 Specification, when an application component requests a connection to an EIS through the resource adapter, WebLogic Server first tries to match the type of connection being requested with an existing and available
ManagedConnection in the connection pool. However, if a match is not found, a new
ManagedConnection may be created to satisfy the connection request.
Using the capacity-increment element in the weblogic-ra.xml descriptor file, you can specify a number of additional
ManagedConnections to be created automatically when a match is not found. This feature provides give you the flexibility to control connection pool growth over time and the performance hit on the server each time this growth occurs.
shrinking-enabled: Controlling System Resource Usage
Although setting the maximum number of
ManagedConnections prevents the server from becoming overloaded by more allocated
ManagedConnections than it can handle, it does not control the efficient amount of system resources needed at any given time. WebLogic Server provides a service that monitors the activity of
ManagedConnections in the connection pool of a resource adapter. If the usage decreases and remains at this level over a period of time, the size of the connection pool is reduced to the initial capacity or as close to this as possible to adequately satisfy ongoing connection requests.
This system resource usage service is turned on by default. However, to turn off this service, you can set the shrinking-enabled element in the weblogic-ra.xml descriptor file to false.
shrink-frequency-seconds: Setting the Wait Time Between Attempts to Reclaim Unused ManagedConnections
Use the shrink-frequency-seconds element in the weblogic-ra.xml descriptor file to identify the amount of time (in seconds) the Connection Pool Manager will wait between attempts to reclaim unused
ManagedConnections. The default value of this element is 900 seconds.
highest-num-waiters: Controlling the Number of Clients Waiting for a Connection
If the maximum number of connections has been reached and there are no available connections, WebLogic Server retries until the call times out. The highest-num-waiters element controls the number of clients that can be waiting at any given time for a connection.
highest-num-unavailable: Controlling the Number of Unavailable Connections
When a connection is created and fails, the connection is placed on an unavailable list. WebLogic Server attempts to recreate failed connections on the unavailable list. The highest-num-unavailable element controls the number of unavailable connections that can exist on the unavailable list at one time.
connection-creation-retry-frequency-seconds: Recreating Connections
To configure WebLogic Server to attempt to recreate a connection that fails while creating additional
ManagedConnections, enable the connection-creation-retry-frequency-seconds element. By default, this feature is disabled.
match-connections-supported: Matching Connections
A connection request contains parameter information. By default, the connector container calls the matchManagedConnections() method on the
ManagedConnectionFactory to match the available connection in the pool to the parameters in the request. The connection that is successfully matched is returned.
It may be that the
ManagedConnectionFactory does not support the call to matchManagedConnections(). If so, the matchManagedConnections() method call throws a javax.resource.NotSupportedException. If the exception is caught, the connector container automatically stops calling the matchManagedConnections() method on the
You can set the match-connections-supported element to specify whether the resource adapter supports connection matching. By default, this element is set to true and the matchManagedConnections() method is called at least once. If it is set to false, the method call is never made.
If connection matching is not supported, a new resource is created and returned if the maximum number of resources has not been reached; otherwise, the oldest unavailable resource is refreshed and returned.
test-frequency-seconds: Testing the Viability of Connections
The test-frequency-seconds element allows you to specify how frequently (in seconds) connections in the pool are tested for viability.
test-connections-on-create: Testing Connections upon Creation
You can set the test-connections-on-create element to enable the testing of connections as they are created. The default value is
test-connections-on-release: Testing Connections upon Release to Connection Pool
You can set the test-connections-on-release element to enable the testing of connections as they are released back into the connection pool. The default value is
test-connections-on-reserve: Testing Connections upon Reservation
You can set the test-connections-on-reserve element to enable the testing of connections as they are reserved from the connection pool. The default value is
Connection Proxy Wrapper - 1.0 Resource Adapters
The connection proxy wrapper feature is valid only for resource adapters that are created based on the J2EE 1.0 Connector Architecture. When a connection request is made, WebLogic Server returns to the client (by way of the resource adapter) a proxy object that wraps the connection object. WebLogic Server uses this proxy to provide the following features:
- Connection leak detection capabilities
- Late XAResource enlistment when a connection request is made before starting a global transaction that uses that connection
If the connection object returned from a connection request is cast as a
Connection implementation class (rather than an interface implemented by the
Connection class), a
ClassCastException can occur. This exception is caused by one of the following:
- The resource adapter performing the cast
- The client performing the cast during a connection request
An attempt is made by WebLogic Server to detect the
ClassCastException caused by the resource adapter. If the server detects that this cast is failing, it turns off the proxy wrapper feature and proceeds by returning the unwrapped connection object during a connection request. The server logs a warning message to indicate that proxy generation has been turned off. When this occurs, connection leak detection and late XAResource enlistment features are also turned off.
WebLogic Server attempts to detect the
ClassCastException by performing a test at resource adapter deployment time by acting as a client using container-managed security. This requires the resource adapter to be deployed with security credentials defined.
If the client is performing the cast and receiving a
ClassCastException, the client code can be modified, as in the following example.
Assume the client is casting the connection object to
- Rather than having
MyConnection be a class that implements the resource adapter's
Connection interface, modify
MyConnection to be an interface that extends
- Implement a
MyConnectionImpl class that implements the
Turning Proxy Generation On and Off
If you know for sure whether or not a connection proxy can be used in the resource adapter, you can avoid a proxy test by explicitly setting the use-connection-proxies element in the WebLogic Server 8.1 version of weblogic-ra.xml to true or false.
Note: WebLogic Server still supports J2CA 1.0 resource adapters. For 1.0 resource adapters, continue to use the WebLogic Server 8.1 deployment descriptors found in weblogic-ra.xml. It contains elements that continue to accommodate 1.0 resource adapters.
If set to true, the proxy test is not performed and connection properties are generated.
If set to false, the proxy test is not performed and connection proxies are generated.
If use-connection-proxies is unspecified, the proxy test is performed and proxies are generated if the test passes. (The test passes if a ClassCastException is not thrown by the resource adapter).
Note: The test cannot detect a ClassCastException caused by the client code.
If a resource adapter's
ManagedConnectionFactory implements the
ValidatingManagedConnectionFactory interface, then the application server can test the validity of existing connections. You can test either a specific outbound connection or the entire pool of outbound connections for a particular
ManagedConnectionFactory. Testing the entire pool results in testing each connection in the pool individually. For more information on this feature, see section 220.127.116.11 "Detecting Invalid Connections" in the J2CA 1.5 Specification.
Configuring Connection Testing
The following optional elements in the weblogic-ra.xml deployment descriptor allow you to control the testing of connections in the pool.
- test-frequency-seconds—The connector container periodically tests all the free connections in the pool. Use this element to specify the frequency with which the connections are tested. The default is 0, which means the connections will not be tested.
- test-connections-on-create—Determines whether the connection should be tested upon its creation. By default it is false.
- test-connections-on-release—Determines whether the connection should be tested upon its release. By default it is false.
- test-connections-on-reserve—Determines whether the connection should be tested upon its reserve. By default it is false.
Testing Connections in the Administration Console
To test a resource adapter's connection pools:
- In the Administration Console, open the Deployments page and select the resource adapter in the Deployments table.
You will see a table of connection pools for the resource adapter and the test status of each pool.
- Select the connection pool you want to test and click Test.
See Test outbound connections in the console help.