Using the Resource Adapter

After configuring the weblogic-ra.xml descriptor file and deploying the resource adapter, client components (servlet, JSP, EJB, etc.) can look up the connection factory either using the global JNDI name or via a resource reference to the connection factory. For instance, if a JSP page were to request a connection from Sun's XADataSource Connector, it could look up the connection factory using the global JNDI name it was bound to:

javax.naming.InitialContext ic = new javax.naming.InitialContext ( );
javax.resource.cci.ConnectionFactory cf = 
 (javax.resource.cci.ConnectionFactory) ic.lookup("myapp.XADsWithTx");

Alternatively, you could define a reference to the resource adapter in the XML descriptors for the web application that holds the JSP:




Here we've defined a resource reference jca/xads that points to the connection factory instance bound to the global JNDI name myapp.XADsWithTx. When you redeploy the web application, the connection factory will then be available to the JSP under the local ENC for the web application:

javax.naming.InitialContext ic = new javax.naming.InitialContext ( );
javax.resource.cci.ConnectionFactory cf = 
 (javax.resource.cci.ConnectionFactory) ic.lookup("java:comp/env/jca/xads");

In the same way, you can define a resource reference for an EJB component that is then available to all EJBs packaged within the EJB JAR file. Refer to Chapter 10 to see how you can set up references to J2EE resources.

Now you are ready to develop a client that uses the JDBC Connector to connect to the actual database. However, instead of using JDBC calls, the client will use the CCI contracts implemented by the XADataSource connector. Here's a summary of the steps that a client component needs to execute in order to interact with the underlying EIS (in this case, a database that lives on an SQL Server 2000 instance):

  1. Request a connection from the resource adapter, and set up an interaction:

    InitialContext ic = new InitialContext( );
    ConnectionFactory cf = (ConnectionFactory) ic.lookup("java:comp/env/jca/testds");
    Connection con = (Connection) cf.getConnection( );
    Interaction i = (Interaction) con.createInteraction( );
  2. Describe the kind of interaction with the underlying resource. In this case, we will run a PreparedStatement SQL query that returns, at most, 10 rows in the result set:

    // CciInteractionSpec implements java.resource.cci.InteractionSpec, and is one 
    // of the implementation classes provided by the resource adapter itself
    CciInteractionSpec ispec = new CciInteractionSpec( );
    ispec.setQuery("select firstname, lastname from tblEmployee
     where salary > ? and lastname like ?");
  3. Create an IndexedRecord instance that acts as a holder for any parameters that need to be provided for the interaction to succeed:

    // CciIndexedRecord implements java.resource.cci.IndexedRecord, and is one
    // of the implementation classes provided by the resource adapter itself
    IndexedRecord rec = new CciIndexedRecord( );
    rec.add(new Integer(1000));
  4. Execute the interaction using the InteractionSpec and IndexedRecord instances:

    ResultSet rs = (ResultSet) i.execute(ispec, rec);
  5. Iterate over the rows of the ResultSet and use the getXXX( ) methods to retrieve column values:

    while ( )) {
     out.println("First Name: " + rs.getString(1) + " Last Name: " + rs.getString(2));
  6. After completion, release any resources acquired during the interaction:

    rs.close( ); 
    i.close( ); 
    con.close( );

Make sure that you properly close the connection to the EIS before completion so that you avoid any connection leaks.

Clearly the exact nature of the interaction between the client and the EIS will depend on both the resource adapter and the EIS vendor itself. Thus, even though your code isn't portable across resource adapters, the JCA framework guarantees that your code remains portable across J2EE Application Servers.


Web Applications

Managing the Web Server

Using JNDI and RMI



J2EE Connectors



Using EJBs

Using CMP and EJB QL

Packaging and Deployment

Managing Domains


Performance, Monitoring, and Tuning




Web Services


Logging and Internationalization


WebLogic. The Definitive Guide
WebLogic: The Definitive Guide
ISBN: 059600432X
EAN: 2147483647
Year: 2003
Pages: 187 © 2008-2020.
If you may any questions please contact us: