I was under the impression that you are not allowed to use resource managers (e.g. database) from within setEntityContext (HFE, page 327).
Yet in the
J2EE tutorial (not HFE), the salesrep example contains code that seems to me to access a resource manager.
The salesrep example (described in j2eetutorial\doc\BMP4.html) has a CustomerBean entity bean with the following code:
public void setEntityContext(EntityContext context) {
this.context = context;
try {
makeConnection();
} catch (Exception ex) {
throw new EJBException("Unable to connect to database. " +
ex.getMessage());
}
}
...
private void makeConnection() throws NamingException, SQLException {
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup(dbName);
con = ds.getConnection();
}
Here, setEntityContext calls makeConnection, which gets a database connection. Does this violate the rules of EJB2.0 for not accessing resource managers (such as databases)?
If this is ok, is it because you are allowed to use a connection factory, but not the connection itself? (But surely a connection factory is a resource...?)
Any views?
Thanks,
Roger
SCJP1.2, SCJP1.4, studying for SCBCD
[ September 09, 2004: Message edited by: Roger Yates ]