Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Create forward-compatible beans in EJB, Part 1

How to write EJB 1.0 beans to port to EJB 1.1 servers

  • Print
  • Feedback

Page 5 of 6

List 7-A. JDBC in PortableContext1_0 (EJB 1.0)
import javax.ejb.EJBContext;
import java.util.Properties;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class PortableContext1_0 extends PortableContext {
  public Object lookup(String name, Class type)
      throws PortableContextException {
    try {
      Properties props = ejbContext.getEnvironment();
      String value = props.getProperty(name);
      if (value == null)
        return null;
      if (name.startsWith("java:comp/env/jdbc"))
        return DriverManager.getConnection(value);
      else {
        if (type == String.class)
          return value;
        else
          return primitiveWrapper(value, type);
      }
    } catch(Exception e) {
      throw new PortableContextException(e);
    }
  }
  private Object primitiveWrapper(String value, Class type)
      throws PortableContextException {
    if (type == Double.class)
      return new Double(value);
    if (type == Integer.class)
      return new Integer(value);
    if (type == Boolean.class)
      return new Boolean(value);
    if (type == Long.class)
      return new Long(value);
    if (type == Byte.class)
      return new Byte(value);
    if (type == Short.class)
      return new Short(value);
    if (type == Float.class)
      return new Float(value);
    else
      throw new PortableContextException();
  }
}


List 7-B. JDBC in PortableContext1_1 (EJB 1.1)
import javax.ejb.EJBContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.SQLException;
 
public class PortableContext1_1 extends PortableContext {
  InitialContext jndiContext;
 
  public Object lookup(String name, Class type)
      throws PortableContextException {
    try {
      jndiContext = new InitialContext();
      Object value = jndiContext.lookup(name);
      if (name.startsWith("java:comp/env/jdbc")) {
        DataSource ds = (DataSource)value;
        return ds.getConnection();
      } else
        return value;
    } catch(NamingException ne) {
      throw new PortableContextException(ne);
    } catch(SQLException se) {
      throw new PortableContextException(se);
    }
  }
}


In the PortableContext1_0 implementation, the name variable is used to obtain the JDBC URL from the environment properties. The JDBC URL is used in the DriverManager.getConnection() to obtain the JDBC connection. In the PortableContext1_1 context, the name is used to look up the JDBC factory (javax.sql.DataSource), which is in turn used to get the connection.

Below is an example of how the PortableContext would be used to get a database connection in a forward-compatible enterprise bean.

List 8. Using the PortableContext to access a JDBC connection in both EJB 1.0 and EJB 1.1
public class AccountBean implements javax.ejb.EntityContext {
  int id;
  double balance;
  EntityContext ejbContext; PortableContext portableContext;
    ...
  public void ejbLoad() {
    try {       Connection con = (Connection)
           portableContext.lookup("java:comp/env/jdbc/AccountDB", Connection.class);
           
        PreparedStatement ps = con.prepareStatement("select .... ");
    ...
  }
  ...
}


EJB 1.1 recommends, but does not require, that the JDBC DataSource be mapped to the java:comp/env/jdbc directory name. This naming scheme is adopted on the PortableContext for consistency and forward compatibility.

  • Print
  • Feedback

Resources