ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzatz_5.4.0.1/51/program/datajca.htm

106 lines
4.1 KiB
HTML
Raw Permalink Normal View History

2024-04-02 14:02:31 +00:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<LINK rel="stylesheet" type="text/css" href="../../../rzahg/ic.css">
<title>Access data with J2EE Connector Architecture connectors</title>
</head>
<BODY>
<!-- Java sync-link -->
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
<h5><a name="datajca"></a>Access data with J2EE Connector Architecture connectors</h5>
<p>As indicated in the J2EE 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 WebSphere Studio Application Development (WSAD) tools, most of the following deployment descriptors and code are generated for you. This example shows the manual method of accessing an EIS resource.</p>
<p>For each EIS connection, do the following:</p>
<ol>
<li><p>Declare a connection factory resource reference in your application component's deployment descriptors, as shown in this example:</p>
<pre> &lt;resource-ref&gt;
&lt;description&gt;description&lt;/description&gt;
&lt;res-ref-name&gt;eis/myConnection&lt;/res-ref-name&gt;
&lt;res-type&gt;javax.resource.cci.ConnectionFactory&lt;/res-type&gt;
&lt;res-auth&gt;Application&lt;/res-auth&gt;
&lt;/resource-ref&gt;</pre></li>
<li><p>Configure, during deployment, each resource adapter and associated connection factory through the console. See <a href="dataj2ci.htm">Installing J2C resource adapters</a> and <a href="dataj2cf.htm">Configuring J2C connection factories</a> for more information.</p></li>
<li><p>Locate the corresponding connection factory for the EIS resource adapter using Java Naming and Directory Interface (JNDI) lookup in your application component, during run time. For more information, see <a href="datacflk.htm">Example: Connection factory lookup</a>.</p></li>
<li><p>Get the connection to the EIS from the connection factory.</p></li>
<li><p>Create an interaction from the Connection object.</p></li>
<li><p>Create an InteractionSpec object. Set the function to execute in the InteractionSpec object.</p></li>
<li><p>Create a Record instance for the input and output data used by function.</p></li>
<li><p>Run the function through the Interaction object.</p></li>
<li><p>Process the record data from the function.</p></li>
<li><p>Close the connection.</p></li>
</ol>
<p>This code segment shows how an application component might create an interaction and execute it on the EIS:</p>
<pre>...
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(&quot;java:comp/env/eis/myConnection&quot;);
// create a connection
connection = connectionFactory.getConnection();
// Create Interaction and an InteractionSpec
interaction = connection.createInteraction();
interactionSpec = new InteractionSpec();
interactionSpec.setFunctionName(&quot;GET&quot;);
// 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 */}
}
}</pre>
<p>See the <a href="codex.htm">Code example disclaimer</a> for legal information about this code example.</p>
</body>
</html>