12.07.2015 Views

Beginning Java EE 6 with GlassFish 3, Second Edition

Beginning Java EE 6 with GlassFish 3, Second Edition

Beginning Java EE 6 with GlassFish 3, Second Edition

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 4 ■ MANAGING PERSISTENT OBJECTSdeprecated). The only difference is that OPTIMISTIC_FORCE_INCREMENT will force an update (increment) tothe entity’s version column.Applications are strongly encouraged to enable optimistic locking for all entities that may beconcurrently accessed. Failure to use a locking mechanism may lead to inconsistent entity state, lostupdates, and other state irregularities. Optimistic locking is a useful performance optimization thatoffloads work that would otherwise be required of the database and is an alternative to pessimisticlocking, which requires low-level database locking.Pessimistic LockingPessimistic locking is based on the opposite assumption to optimistic locking, because a lock is eagerlyobtained on the entity before operating on it. This is very resource restrictive and results in significantperformance degradation, as a database lock is held using a SELECT ... FOR UPDATE SQL statement toread data.Databases typically offer a pessimistic locking service that allows the entity manager to lock a row ina table to prevent another thread from updating the same row. This is an effective mechanism to ensurethat two clients do not modify the same row at the same time, but requires expensive, low-level checksinside the database. Transactions that would cause this constraint to be violated result in aPessimisticLockException being thrown and the transaction marked for rollback.Optimistic locking is appropriate in dealing <strong>with</strong> moderate contention among concurrenttransactions. But in some applications <strong>with</strong> a higher risk of contentions, pessimistic locking may bemore appropriate, as the database lock is immediately obtained as opposed to the often late failure ofoptimistic transactions. For example, in times of economic crises, stock markets receive huge numbersof selling orders. If 100 million Americans need to sell their stock options at the same time, the systemneeds to use pessimistic locks to ensure data consistency. Note that at the moment the market is ratherpessimistic instead of optimistic, and that has nothing to do <strong>with</strong> JPA.Pessimistic locking may be applied to entities that do not contain the annotated @Version attribute.SummaryIn this chapter, you learned how to query entities. The entity manager is central to articulating entities<strong>with</strong> persistence. It can create, update, find by ID, remove, and synchronize entities <strong>with</strong> the database<strong>with</strong> the help of the persistence context, which acts as a level-one cache. JPA also comes <strong>with</strong> a verypowerful query language, JPQL, which is database vendor-independent. You can retrieve entities <strong>with</strong> arich syntax using WHERE, ORDER BY, or GROUP BY clauses, and, when concurrent access occurs to yourentities, you know how to use versioning and when to use optimistic or pessimistic locking.In the next chapter, you’ll learn more about the life cycle of entities and will see how to hook somelogic to this life cycle using callback annotations or listeners.166

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!