• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Database connections in setEntityContext?

 
Roger Yates
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Roger Yates
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clarified on re-reading HFE - page 196 explains that it's ok to get the connection at this point, as long as you don't try to use it (i.e. issue any JDBC calls on it).

I've answered myself instead of just deleting the post, in case anyone else is interested!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic