Object-Relational
Mapping
instance. Others require the application explicitly to
acquire a Session per thread.
OPTIMISTIC AND PESSIMISTIC CONCURRENCY
Using a low-level API such as JDBC or ODBC gives the
application programmer direct control over transactions.
With these APIs, transactions are started and either committed or rolled back via APIs. There is no concept of
optimistic transactions.
With ORM, transactions are coarser grained and controlled by APIs on the Transaction instance associated with
a Session. Optimistic transactions are part of the model
and include version fields in the domain model that correspond to version columns in the database. The application specifies either optimistic or pessimistic transactions,
and the persistence provider automatically manages
version columns for optimistic transactions.
Some persistence standards support optimistic concurrency only, with minimally defined APIs for locking;
others support both optimistic and pessimistic concurrency models; and some products support more fine-grained policies for eagerly locking instances of classes
upon retrieval. These finer-grained concurrency models
are needed for certain high-concurrency applications
in which any access to a persistent instance requires an
aggressive lock to achieve high throughput.
DEADLOCK PREVENTION
Deadlocks occur when two or more transactions hold
locks on resources that are needed by other transactions
and none of the transactions can complete without canceling locks held by other transactions.
Low-level APIs such as JDBC and ODBC give the
programmer complete control over the lock requests, but
ORM persistence providers automatically generate and
execute database requests based on coarser-grained operations such as commit or rollback. The order of operations
of the database requests can accommodate foreign-key
constraints and batching of SQL commands.
If an application is subject to deadlocks, persistence
providers can often avoid the deadlocks by giving
the application control over the order of execution of
database operations that are not subject to foreign-key
constraints. This requires that the persistence provider is
able to determine the order in which database operations
should be applied, based on the order of changes made to
the domain object model.
SUMMARY
ORM is a technology that provides access to relational
data using the object-oriented paradigm. An ORM implementation consists of a language for mapping between
the object and relational domains and an API for storing, querying, and modifying application objects. There
are several standards with commercial and open source
implementations, as well as some nonstandard products.
The ease of defining a mapping depends on the
requirements of the application, its domain model, the
existence of a legacy relational or object model, and the
complexity of these models. ORM can provide significant
improvements in programmer productivity, application
quality, and maintainability. One of the most important
ways this is achieved is through separation of concerns:
separating the behavior of the domain object model from
the access of the data from the database. Using a standard
API allows the choice of implementation to be a late decision, providing more time for evaluation of alternative
mapping and database technologies. Q
REFERENCES
1. Booch, G. 1991. Object-oriented Analysis and Design with
Applications. Benjamin-Cummings Publishing.
2. Fowler, M. 2002. Patterns of Enterprise Application Architecture. Addison-Wesley.
3. Ambler, S. 2003. Agile Database Techniques. Wiley.
LOVE IT, HATE IT? LET US KNOW
feedback@acmqueue.com or www.acmqueue.com/forums
CRAIG RUSSELL is a senior staff engineer at Sun Micro-
systems. He is a member of the Apache Software Founda-
tion, the chair of the Apache OpenJPA Project Management
Committee, and a member of the Apache Incubator project
responsible for bringing projects into Apache. He is speci-
fication lead for Java Data Objects (JSR 1 2 and 243) and
leads the implementation team for its API and Technology
Compatibility Kit.
© 2008 ACM 1542-7730/08/0500 $5.00