Java Connector Overview and an Example #Java Short Notes

  • Java Connector Architecture (JCA) enables integration of the J2EE components to any Enterprise Information Systems (EIS). EIS can be heterogeneous where scalability is a must.
  • JDBC assumes DBMS/RDBMS in the back-end, JCA targets any kind of EIS.
  • One of the key parts of JCA is the Resource Adapter – usually a part of the application servers. Multiple resource adapters can be used to connect to different EISs of heterogeneous types.
  • When resource adapters are plugged into the Application Servers they provide necessary transaction, security, and connection pooling mechanisms
  • In the JCA, an application contract defines the API that provides the client view to access EISs. The API can be EIS/resource adapter specific or standard.
  • Common Client Interface (CCI) – a set of interfaces and classes that allows J2EE applications to interact with heterogeneous EISs.
  • Example of JCA
    • EJBs, Servlets can connect to EISs through CCI and then access the EISs as required
    • J2EE SDK provides a black box resource adapter that we will use in our example to connect to EISs. We will assume RDBMS as the EIS in the example
    • Example: The flow of the application: client->servlet->resource adapter->EIS Tier (RDBMS – stored procedure)
    • In a servlet example: first step: import javax.resource.cci.*;
    • javax.resource.cci.*; – for ResourceException class
    • import cci blackbox classes: import com.sun.connector.cciblackbox.*;
    • Also, import servlet and application specific classes
    • public class ExampleServletConnector extends HttpServlet{   private Connection con;   private ConnectionFactory cf;   private String user;  public void init() throws ServletException{      try{            //establish JNDI interface            InitialContext ic = new InitialContext();           //look up user and password           user = (String) ic.lookup("java:comp/env/user");           String password = (String) ic.lookup("java:comp/env/password");          //reference to the connection factory for the CCI black box resource adapter - coded name CCIEIS          cf = (ConnectionFactory) ic.lookup("java:comp/env/CCIEIS");         //collect connection specification, connect to the database         ConnectionSpec conSpec = new ConnectionSpec (user,password);       con = cf.getConnection(conSpec);      }catch(ResourceException rex){         rex.printStackTrace();      }catch(NamingException nex){        nex.printStackTrace();      }  }
    • Now define the doGet method for processing. An example doGet method is as follows
    • public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException {  PrintWriter output = null;  res.setContentType("text/html");  try{     PrintWriter out = res.getWriter();     //find the number of rows in the account table - check getAccountAccountCount method in the next item     int accCount = getAccountAccountCount();     out.println("Account Count" + accCount);  }catch(Exception ex){   ex.printStackTrace();  }}
    • getAccountAccountCount() method: to query the database and determine the number of rows in the account table
    • public int getAccountCount(){   int count = -1;   try{       Interaction ix = con.createInteraction();       CciInteractionSpec iSpec = new CciInteractionSpec();        iSpec.setSchema(user);        iSpec.setCatalog(null);        //stored procedure name to calculate the number of rows       iSpec.setFunctionName("ACCOUNTCOUNT");       RecordFactory rf = cf.getRecordFactory();       IndexedRecord iRec = rf.createIndexedRecord("InputRecord");       Record oRec = ix.execute(iSpec, iRec);       Iterator it = ((IndexedRecord)oRec).iterator();       //process        while(it.hasNext()){          Object obj = it.next();          if (obj instanceof Integer){             count = ((Integer) obj).intValue();          }else if (obj instanceOf BigDecimal){           count = ((BigDecimal) obj).intValue();          }        }   }catch(Exception ex){   }   return count;}

From: http://sitestree.com/?p=4956
Categories:Java Short Notes
Tags:
Post Data:2010-01-15 20:19:20

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada