Java Reference
In-Depth Information
by the JDO specification. There are numerous ways to integrate the JDO into the J2EE framework. For
example, you can build a two-tier system servlet and JSP pages on the client tier and JDO and other
business processes on the second tier. You can also build a three-tier system with a servlet and/or JSP
at the client tier, the session EJB or BMP entity beans in the middle tier, and the JDO at the back end
(that is, the resource tier). You can certainly combine JDO with J2EE in any other innovative ways.
When you integrate the JDOs into the J2EE framework, the J2EE components typically take advantage
of JDO's simple, object-oriented, and transparent persistence service. The servlet, JSP, or EJBs access
the persisted object state via JDOs instead of by accessing the database directly. From these J2EE
components' points of view, accessing JDOs and accessing databases share a great deal of logical
similarity. This process is illustrated in
Figure 23-3
.
Figure 23-3:
Data persistence with (a) JDBC and (b) JDO
Figure 23-3
(a) illustrates the scenario that session EJBs, servlet, or JSPs access the persisted data via
JDBC. The J2EE components first get a transactional
Connection
to the database from the
DataSource
that represents the underlying database. The
DataSource
serves as a
Connection
factory. The J2EE components then access the database through the
Connection
, which returns a
ResultSet
object that wraps up the retrieved rows of data. The client then walks through the
ResultSet
to access each record.
In
Figure 23-3
(b), JDO is used for data persistence. Instead of getting a database
Connection
, the
J2EE clients first get a transactional
PersistenceManager
from the
PersistenceManagerFactory
. They then access the persisted data through the
PersistenceManager
. The
PersistenceManager
returns either a single persisted object or a
collection of persisted objects. The J2EE components retrieve or update the persisted object state using
the
accessor
methods (that is,
getters
and
setters
) of the persistent classes.
The code examples in the two scenarios illustrated in
Figure 23-3
also share a great deal of similarity.
For the JDBC approach, the code may look like what is shown here:
… …
InitialContext ctx = new InitialContext();