Accessing data using Java EE Connector Architecture connectors
To access data from a Java™ EE Connector Architecture (JCA) compliant application in WebSphere® Application Server, you configure and use resource adapters and connection factories.
About this task
An application component uses a connection factory to access a connection instance, which the component then uses to connect to the underlying enterprise information system (EIS). Examples of connections include database connections, Java Message Service connections, and SAP R/3 connections.
As indicated in the Java EE Connector Architecture (JCA) Specification, each enterprise information system (EIS) needs a resource adapter and a connection factory. This connection factory is then accessed through the following programming model. If you use Rational® Application Development tools, most of the following deployment descriptors and code are generated for you. This example shows the manual method of accessing an EIS resource.
Procedure
Example
The following code segment shows how an application component might create an interaction and implement it on the EIS:
javax.resource.cci.ConnectionFactory connectionFactory = null; javax.resource.cci.Connection connection = null; javax.resource.cci.Interaction interaction = null; javax.resource.cci.InteractionSpec interactionSpec = null; javax.resource.cci.Record inRec = null; javax.resource.cci.Record outRec = null; try { // Locate the application component and perform a JNDI lookup javax.naming.InitialContext ctx = new javax.naming.InitialContext(); connectionFactory = (javax.resource.cci.ConnectionFactory) ctx.lookup("java:comp/env/eis/myConnection"); // create a connection connection = connectionFactory.getConnection(); // Create Interaction and an InteractionSpec interaction = connection.createInteraction(); interactionSpec = new InteractionSpec(); interactionSpec.setFunctionName("GET"); // Create input record inRec = new javax.resource.cci.Record(); // Execute an interaction interaction.execute(interactionSpec, inRec, outRec); // Process the output... } catch (Exception e) { // Exception Handling } finally { if (interaction != null) { try { interaction.close(); } catch (Exception e) {/* ignore the exception*/} } if (connection != null) { try { connection.close(); } catch (Exception e) {/* ignore the exception */} } }