Home > Articles > Programming > Java

J2EE Entity EJB Development

  • Print
  • + Share This
One of the more significant needs of the EJB specification is the need to connect to databases from within EJB. All J2EE-based components need to talk with a database, and J2EE provides support for this via JDBC. In this article, Paul Perrone looks at how J2EE-compliant environments provide access to the JDBC API and shows how they assist in configuring JDBC resources. Paul also describes how to create entity EJBs that use JDBC for data connectivity.
This article contains excerpts from Building Java Enterprise Systems with J2EE.
From the author of

EJB and JDBC

The need to connect to databases from within EJB is clearly one of the more significant needs of the EJB specification. All J2EE-based components need to talk with a database, and J2EE provides support for this via JDBC. Vendors such as Oracle and a future J2EE specification will allow the use of SQLJ to provide database connectivity. However, the primary approach that I advocate and assume here is to use JDBC. J2EE-compliant environments provide access to the JDBC API and serve as a convenient means for configuring JDBC resources via the XML-based deployment descriptor and for connecting to JDBC resources via JNDI.

JDBC driver configuration and data-source identification are accomplished via the XML-based deployment descriptor for J2EE modules. The <resource-ref> element can be defined for individual J2EE EJB session and entity beans. Zero or more JDBC resources may be configured per EJB within the J2EE deployment descriptor.

As an example of JDBC resource configuration for entity EJBs as exemplified via an ejb-xml.jar file, we have this:

<ejb-jar>
 ...
 <enterprise-beans>
  ...
  <entity>
   ...
   <resource-ref>
     <res-ref-name>jdbc/ejavaPool</res-ref-name>
     <res-type>javax.sql.DataSource</res-type>
     <res-auth>Container</res-auth>
   </resource-ref>
  </entity>
  ...
 </enterprise-beans>
 ...
</ejb-jar>

Utilizing such a data source from within an entity bean is then a simple matter of looking up the named data source from within the EJB and obtaining a handle to a javax.sql.DataSource object. The DataSource object can then be used to obtain a java.sql.Connection object. The underlying container is responsible for determining how this Connection object is to be allocated to your bean and will most likely maintain a pool of connections. From that point on, the bean can use the Connection object to create JDBC statements and obtain results as usual.

Because bean-managed entity beans must manage their own connectivity to the database, a means for connecting to a database via Java must be employed. Although in this article I use JDBC resources only from within a bean-managed persistence (BMP) entity bean EJB, JDBC resources can also be explicitly accessed from session beans. Session bean access to JDBC resources configured inside of zero or more <resource-ref> elements is located within the <session> subelement of the <enterprise-beans> element in an EJB deployment descriptor.

  • + Share This
  • 🔖 Save To Your Account