- Connection Management Contract
- Connection Management Architecture
- Application Programming Model
- Conclusion
3.3 Application Programming Model
An application uses a standard application programming model when obtaining connections. The model is similar whether an application obtains the connection using an application server in a managed environment or whether it obtains the connection independent of the application server.
An application developer follows defined steps and relies on particular information to establish a connection to an underlying EIS. Before the developer can establish a connection, the necessary connection factory needs to be properly deployed and configured. The deployer is responsible for the deployment of a resource adapter and an application. The deployer also deploys and configures the connection factory. What is entailed in deploying and configuring a connection factory? The deployer configures the connection factory by providing configuration information—port number, server name, and so forth. This configuration information represents the information required by a resource adapter to create physical connections to an EIS. Once the connection factory is configured, the application can use the connection factory to create connections to the EIS. See Code Example 3.1.
Example 3.1 Establishing a Connection
package com.aci; public class InventoryManagerEJB implements javax.ejb.SessionBean { private javax.resource.cci.ConnectionFactory cf; ... public int getQuantityAvailable(String productId) throws InventoryException { try { javax.resource.cci.Connection cx = getConnection(); CheckInventoryCommand command = new CheckInventoryCommand(cx, cf.getRecordFactory()); command.setProductId(productId); command.execute(); // Close the connection. cx.close(); return command.getProductQuantity(); } catch (Exception e) { throw new InventoryException(); } ... } public void removeFromInventory(String productId, int quantity) throws InventoryException { } public Connection getConnection() { try { // Get a connection using the ConnectionFactory. Connection cx = cf.getConnection(); return cx; } catch (ResourceException re) { throw new EJBException(re); } } private void initialize() { try { // Use JNDI interface to look up connection // factory instance. Context nc = new InitialContext(); // Lookup ConnectionFactory from the JNDI namespace. cf = (ConnectionFactory)nc.lookup( "java:comp/env/eis/MainframeCxFactory"); } catch (NamingException ne) { throw new EJBException(ne); } } public void ejbCreate() throws RemoteException { initialize(); } public void setSessionContext(SessionContext sc) {} public void ejbRemove() throws RemoteException {} public void ejbActivate() { /** Never Called **/ } public void ejbPassivate() { /** Never Called **/ } }
Code Example 3.1 uses the Connection and ConnectionFactory interfaces defined in the Common Client Interface. (See Chapter 7, Common Client Interface, for more information on the CCI.) When the application component needs to establish a connection, the developer uses the JNDI naming context to look up a ConnectionFactory instance.
Once the application component has a ConnectionFactory instance, the developer can obtain one or more connections to the EIS by invoking the factory's getConnection method. This method returns an application-level connection handle to the underlying physical connection. At this point, the developer may use the common client interface (CCI) application programming model to access the underlying EIS. See Chapter 12, Connection Management Contract, for details on a connection handle, and how an application server and resource adapter collaborate to manage connection pooling.
When the application component completes its work using the connection, the developer calls the close method on the connection handle.