Java Reference
In-Depth Information
throws HibernateException, SQLException {
Query query = session.getNamedQuery("findOrdersToSend");
query.setLockMode("waitingOrder", LockMode.UPGRADE);
Calendar cutOffTime = Calendar.getInstance();
cutOffTime.add(Calendar.MINUTE, -timeWindowInMinutes);
query.setParameter("cutOffTime", cutOffTime.getTime());
return query.list();
}});
}
}
The call to Query.setLockMode() specifies that the orders identified by the wait-
ingOrder alias should be locked. When Hibernate executes this query, it will use a
SELECT FOR UPDATE that locks the rows in the PLACED_ORDER table.
One drawback of how Hibernate implements pessimistic locking is that
because it is a programmatic API the developer is responsible for ensuring that it
is used consistently. In comparison, JDO' s approach of specifying the concurrency
mechanism using a PersistenceManagerFactory property is easier to use. Fortu-
nately, most of the loads and queries for a class are centralized in its repository,
which reduces the chances of forgetting to lock an object.
Another drawback is that pessimistic locking can only be used when the appli-
cation loads objects by calling one of the methods described earlier. Unlike JDO ,
Hibernate will not lock a row when the application navigates to object.
The third drawback is that locking objects for only certain transactions is diffi-
cult because it must be done programmatically. Either a repository would have to
define locking and nonlocking versions of some methods, or its caller would have
to tell the repository when to use pessimistic locking
Despite these drawbacks, Hibernate's pessimistic locking mechanism is useful
for certain applications. A Hibernate application can also use serializable transac-
tions.
Using serializable or repeatable read transactions
There are three main ways to configure the transaction isolation level in a Hiber-
nate application:
Using the TransactionInterceptor to specify the isolation level on a per-
transaction basis, as shown in section 12.2.4
Configuring the DataSource , as shown in section 12.2.4
Setting the hibernate.connection.isolation SessionFactory property to a
value of 8 ( JDBC Connection.SERIALIZABLE )
 
 
 
Search WWH ::




Custom Search