• 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 ...
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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 {
} catch (Exception ex) {
throw new EJBException("Unable to connect to database. " +


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?

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!
Everybody! Do the Funky Monkey! Like this tiny ad!
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic