Sample EJB application using the IMS Universal Database resource adapter CCI programming interface
The following sample EJB bean demonstrates the basic programming flow for a JCA application using the IMS Universal Database resource adapter in a managed environment.
package client;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.resource.ResourceException;
import javax.resource.cci.Connection;
import javax.resource.cci.ConnectionFactory;
import javax.resource.cci.Interaction;
import javax.resource.cci.ResultSet;
import javax.transaction.UserTransaction;
import com.ibm.ims.db.cci.SQLInteractionSpec;
/**
* Bean implementation class for Enterprise Bean: StatefulBeanManaged
*/
public class BeanManagedSampleBean implements javax.ejb.SessionBean {
private javax.ejb.SessionContext mySessionCtx;
public void execute() throws Exception {
InitialContext ic = new InitialContext();
ConnectionFactory cf =
(ConnectionFactory) ic.lookup("java:comp/env/MyMCF");
Connection conn = null;
UserTransaction ut = null;
try {
ut = this.mySessionCtx.getUserTransaction();
ut.begin();
conn = cf.getConnection();
Interaction ix = conn.createInteraction();
SQLInteractionSpec iSpec = new SQLInteractionSpec();
// This query will return information for each person
// in the phonebook with the last name WATSON
iSpec.setSQL("SELECT * FROM " +
"PCB01.PHONEBOOK WHERE LASTNAME='WATSON'");
ResultSet rs = (ResultSet) ix.execute(iSpec, null);
// Print out the first name of every person in the
// phonebook with the last name WATSON
while (rs.next()) {
System.out.println(rs.getString("FIRSTNAME"));
}
rs.close();
ix.close();
ut.commit();
conn.close();
} catch (ResourceException e) {
ut.rollback();
conn.close();
} catch (SQLException e) {
ut.rollback();
conn.close();
}
}
/**
* getSessionContext
*/
public javax.ejb.SessionContext getSessionContext() {
return mySessionCtx;
}
/**
* setSessionContext
*/
public void setSessionContext(javax.ejb.SessionContext ctx) {
mySessionCtx = ctx;
}
/**
* ejbCreate
*/
public void ejbCreate() throws javax.ejb.CreateException {
}
/**
* ejbActivate
*/
public void ejbActivate() {
}
/**
* ejbPassivate
*/
public void ejbPassivate() {
}
/**
* ejbRemove
*/
public void ejbRemove() {
}
}