How do you get a JDBC database registered with a JNDI name so that it can be accessed from an EJB?

Dan Christopherson

The short answer is that it depends on which container you're using to some extent. The one thing that (should be) common in EJB 1.1 containers is that the ejb-jar.xml file's entry for that bean needs to contain a 'resource-ref' stanza, like so:

The res-ref-name is the most interesting part. This is the JNDI name relative to the java:comp/env namespace. Hence, to get this connection you'd do (in your bean):
  Context context = new InitialContext();
  DataSource source = context.lookup("java:comp/env/jdbc/LocalDB2");
which gives you a DataSource that you can call getConnection on.

The other half of this is container specific and done at deployment time by a 'Deployer' or 'Assembler' (to use the rolenames specified by the EJB spec.) This can work very differently from one container to the next, but here are a couple of (abbreviated) examples.

With Weblogic 5.1, you must define a connection pool in, then edit the weblogic specific deployment descriptor (using the EJB Deployment tool) to associate the resource-ref specified in ejb-jar.xml with that connection pool.

With Inprise Application Server 4.0, all of the parameters for the connection (JDBC driver, connection URL, etc.) are specified in the inprise specific deployment descriptor (also editable via their deployment tool).

Other servers will have other ways of associating the resource-ref with a pre-defined connection pool.

0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact