Transactions

Features:

Cost:

Additional processing for each object that is locked. Additional processing and temporary heap space for each transactional field which is modified. Additional processing for deadlock detection. Temporarily blocked threads when there is transaction contention.

Usage:

Avoid:

Summary:

Transactions are a powerful tool for maintaining application data consistency and scaling. But this feature comes at a cost. Avoid using transactions where they are not necessary.

Java monitors

Features:

  • Monitors (the Java synchronize keyword) provide a simple mutual exclusion mechanism.

  • Lighter weight than transactions.

  • Easy to cause undetected deadlocks.

  • Multiple threads sharing read access to a resource become single-threaded when accessing the resource.

Usage:

  • Use monitor when synchronization is required for non-transactional resources.

Avoid:

  • Using monitors on transactional resources (they are already protected by transaction locking).

READ_COMMITTED_SNAPSHOT Transaction Isolation Level

Use of this isolation level carries a performance penalty. An extra shared memory copy of the object data must be made the first time the data is accessed with a transaction. Subsequent accesses then use the read image, and commit frees the memory.

The default isolation level, SERIALIZABLE, does not carry this penalty.