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:
... jca/xads javax.resource.cci.ConnectionFactory Container Shareable ... jca/xads myapp.XADsWithTx ...
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):
InitialContext ic = new InitialContext( ); ConnectionFactory cf = (ConnectionFactory) ic.lookup("java:comp/env/jca/testds"); Connection con = (Connection) cf.getConnection( ); Interaction i = (Interaction) con.createInteraction( );
// CciInteractionSpec implements java.resource.cci.InteractionSpec, and is one // of the implementation classes provided by the resource adapter itself CciInteractionSpec ispec = new CciInteractionSpec( ); ispec.setFunctionName("EXECUTEQUERY"); ispec.setQuery("select firstname, lastname from tblEmployee where salary > ? and lastname like ?"); ispec.setPrepared(true); ispec.setMaxRows(10); ispec.setResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE);
// 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)); rec.add("mount%");
ResultSet rs = (ResultSet) i.execute(ispec, rec);
while (rs.next( )) { out.println("First Name: " + rs.getString(1) + " Last Name: " + rs.getString(2)); }
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.
Introduction
Web Applications
Managing the Web Server
Using JNDI and RMI
JDBC
Transactions
J2EE Connectors
JMS
JavaMail
Using EJBs
Using CMP and EJB QL
Packaging and Deployment
Managing Domains
Clustering
Performance, Monitoring, and Tuning
SSL
Security
XML
Web Services
JMX
Logging and Internationalization
SNMP