• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Database connections in setEntityContext?

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic