Forums Register Login

Database connections in setEntityContext?

+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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!
mooooooo ..... tiny ad ....
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 589 times.
Similar Threads
ERROR [LogInterceptor] RuntimeException
SQLNestedException
Using datasource for login bean & login page?
How to do JNDI lookup?
lookup(java:comp
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 11:12:06.