Chapter 12. J2EE Connector Architecture scenario 403
Example 12-3 Data conversion (Unicode to EBCDIC)
rateRecord = new GenericRecord(account.getBytes(“IBM037”));
Example 12-4 shows how the entire output COMMAREA returned from the
enterprise tier is converted from EBCDIC to a String format using a String
constructor.
Example 12-4 Data conversion (EBCDIC to Unicode)
String commarea_rate = new String(rateRecord.getCommarea(),"IBM037");
12.3.3 Connection management
Once you have a mapping record structure in Java from COBOL or C code, you
are ready to make a connection and supply the required data. This section
describes the things that need to be done by the application program to connect
to CICS using a J2EE connector.
A connection can be
managed or nonmanaged. A managed connection is
managed by the application server. A non-managed connection is obtained
directly through the resource adapter.
The class diagram in Figure 12-4 on page 394 shows a managed connection.
The application program uses ConnectionFactory and Connection to manage the
connection. A reference to ConnectionFactory is obtained from the JNDI name
space using InitialContext, and an instance of Connection is obtained from
ConnectionFactory. The physical aspects of connection management are
handled by the following components:
򐂰 Application server and resource adapter
Connections from the application server to the CICS Transaction Gateway
through the ECI resource adapter can utilize the application server pool
manager to reuse free connections as they become available. This applies
both to network connections when using a remote Gateway daemon and to
local connections when using a local CICS Transaction Gateway. Similarly,
terminals in use with the EPI resource adapter are also pooled, although
connections are not pooled for the EPI resource adapter.
򐂰 Resource manager
Some resource managers have their own connection management functions.
The CICS Transaction Gateway internally manages the connections from the
Client daemon to the attached CICS regions. This is not visible to the
application server or resource adapter.
404 Patterns: Implementing Self-Service in an SOA Environment
Rational Software Architect generates another Java bean called a J2C Java
bean that implements connection management for us. The intializeBinding()
method handles the JNDI look up for managed connections. A resource
reference to the CICS ECI resource adapter connection factory, called
MartAccountCFRef, is defined in the deployment descriptor to map to the actual
JNDI name. The value of the JNDI reference can be changed at deployment
time.
JNDI lookup
The ITSOMart application looks up a connection factory instance with a
managed connection using the JNDI interface. Example 12-5 shows this lookup.
Example 12-5 Acquiring a connection factory using JNDI lookup
protected void initializeBinding() throws ResourceException {
ConnectionFactory cf = null;
String jndiName = "MartAccountCFRef";
javax.naming.Context ctx = null;
try {
ctx = new javax.naming.InitialContext();
if (ctx != null)
cf = (ConnectionFactory) ctx.lookup("java:comp/env/" + jndiName);
if (cf == null)
throw new javax.naming.NamingException();
} catch (javax.naming.NamingException exception) {
try {
if (ctx != null)
cf = (ConnectionFactory) ctx.lookup(jndiName);
} catch (javax.naming.NamingException exception1) {
}
}
For non-managed connections, the intializeBinding() method first creates a
managedConnectionFactory instance and sets the required values. Once the
connection properties are set, it creates the connection from the
managedConnectionFactory instance. Example 12-6 shows acquiring a
connection factory using managedConnectionFactory in a non-managed
environment.
Example 12-6 Acquiring a connection factory
com.ibm.connector2.cics.ECIManagedConnectionFactory mcf = new
com.ibm.connector2.cics.ECIManagedConnectionFactory();
mcf.setConnectionURL("sourcecode.rtp.raleigh.ibm.com");
mcf.setServerName("gifty");
mcf.setUserName("ANUPAG");
Chapter 12. J2EE Connector Architecture scenario 405
mcf.setPassword("anpag");
mcf.setTraceLevel(Integer.valueOf("1"));
cf = (ConnectionFactory) mcf.createConnectionFactory();
Once the connection is acquired, whether managed or non-managed, it is set
using setConnectionFactory:
setConnectionFactory(cf);
Dealing with a connection
The application component invokes the getConnection method on the
connection factory to get a CICS connection, as shown in Example 12-7. The
returned connection instance represents an application-level handle to an
underlying physical connection.
Example 12-7 Acquiring a connection
// use connection factory to get a connection handle
conn = cf.getConnection();
You can also specify a user ID and password in a connection spec, as shown in
Example 12-8.
Example 12-8 Acquiring a connection specifying user ID and password
// create a connection spec
ECIConnectionSpec connspec = new ECIConnectionSpec(userid, password);
// use connection factory to get a connection handle
conn = cf.getConnection(connspec);
The enterprise system uses the user ID and password for authentication and
authorization. A connection specification is unique to a resource adapter, so the
application needs to use the required connection specification, such as
ECIConnectionSpec. Creating ConnectionSpec is optional because the
authentication details can be specified when deploying the application.
After the component finishes with the connection, it closes the connection using
the close method, as shown in Example 12-9:
Example 12-9 Closing a connection
// close a connection
conn.close();
If an application component fails to close an allocated connection after its use,
that connection is considered an unused connection. The application server

Get Patterns: Implementing Self-Service in an SOA Environment now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.