EJB.B.7 How to Obtain Database Connections


Section EJB.14.4 specifies how an enterprise bean should obtain connections to resources such as JDBC API connections. The connection acquisition protocol uses resource manager connection factory references that are part of the enterprise bean's environment.

The following is an example of how an enterprise bean obtains a JDBC connection:

 public class EmployeeServiceBean implements SessionBean {    EJBContext ejbContext;     public void changePhoneNumber(...) {       ...        // obtain the initial JNDI context        Context initCtx = new InitialContext();        // perform JNDI lookup to obtain resource manager connection           // factory        javax.sql.DataSource ds = (javax.sql.DataSource)           initCtx.lookup("java:comp/env/jdbc/EmployeeAppDB");        // Invoke factory to obtain a connection. The security        // principal is not given, and therefore        // it will be configured by the Deployer.        java.sql.Connection con = ds.getConnection();        ...     }  } 


Java 2 Platform, Enterprise Edition. Platform and Component Specifications
Java 2 Platform, Enterprise Edition: Platform and Component Specifications
ISBN: 0201704560
EAN: 2147483647
Year: 2000
Pages: 399

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net