Java Reference
In-Depth Information
In this example, the SqlMapClientTemplate is configured to use a MyOracleSQL-
ExceptionTranslator , which is configured with the DataSource . The DataSource is
used by the SQLErrorCodeSQLExceptionTranslator to identify the database and
select the default SQLException mapping. Once this is done, any ORA-00060 and
ORA-08177 errors that are encountered by the SqlMapClientTemplate will be
mapped to the appropriate concurrency exception.
12.3 Handling concurrent updates
with JDO and Hibernate
As you might expect, handling concurrent updates in a Hibernate or JDO applica-
tion is a lot easier than in an i BATIS/JDBC application. For the most part, you sim-
ply configure Hibernate or JDO to use a particular concurrency mechanism and it
takes care of the rest. With optimistic and pessimistic locking, JDO and Hibernate
automatically generate the required SQL statements. And serializable or repeat-
able read transactions work the same way in Hibernate and JDO applications as
they do in i BATIS/JDBC applications.
Hibernate and JDO implement optimistic locking for objects using the same
change-tracking mechanisms we described earlier. If the update fails because the
version number, timestamp, or column values are different, JDO and Hibernate
throw an exception. JDO and Hibernate implement pessimistic locking for objects
by loading an object with a SELECT FOR UPDATE statement that locks the corre-
sponding row. In this chapter, it is important to remember that object locking
means locking the corresponding database table row or rows.
First we'll examine a domain model-based example that will be used to illus-
trate how to handle concurrent updates in a JDO or Hibernate application. After
that, we'll delve into the details of configuring the JDO and Hibernate to use each
concurrency mechanism.
12.3.1
Example domain model design
Using a particular concurrency mechanism in a JDO or Hibernate application is
mostly a matter of configuration and requires little or no coding. But it always
helps to use an example to make things concrete. Figure 12.6 shows the domain
model version of the business logic for the Send Orders to Restaurant use case. It
uses JDO or Hibernate to retrieve and update the orders that are ready to be sent
to the restaurant.
The key classes are as follows:
 
 
 
 
Search WWH ::




Custom Search