Day 11 — JTA Declarative Transactions & Concurrency Control¶
Daily Time Investment: 2.0 hours | Week: 2 | Phase: 3
Daily Schedule¶
| Segment | Duration | Activity |
|---|---|---|
| Core Theory | 45 min | @Transactional propagation attributes, rollback rules, @Version optimistic locking, pessimistic locking |
| Book Reading | 30 min | Pro Persistence Ch12 (Locking & Concurrency) + EE Patterns Ch6 (Transactions) |
| Hands-On Lab | 45 min | Simulate concurrent updates; verify OptimisticLockException; prove REQUIRES_NEW audit isolation |
Files in This Day¶
-
Pro Persistence Ch12 — Advanced Topics: Locking & Concurrency
@Versionoptimistic locking,OptimisticLockException,LockModeTypeenum (OPTIMISTIC,OPTIMISTIC_FORCE_INCREMENT,PESSIMISTIC_WRITE,PESSIMISTIC_READ), lock timeouts, Second-Level Cache (@Cacheable,shared-cache-mode), lifecycle callbacks, Bean Validation integration. -
Lab Guide — JTA Transactions & Concurrency
@Transactional(TxType.REQUIRED)ACID commit, unchecked exception auto-rollback,REQUIRES_NEWautonomous audit log,rollbackOnfor checked exceptions,@Versioncollision detection (OptimisticLockException).
Prerequisites to Continue¶
New concepts not seen in Phase 1 or Phase 2
- JTA (Jakarta Transactions) — a distributed transaction protocol; in Jakarta EE,
@Transactionalis CDI-intercepted; the container managesbegin()/commit()/rollback()automatically — you never call these manually - ACID properties — Atomicity (all-or-nothing), Consistency (invariants maintained), Isolation (concurrent transactions don't interfere), Durability (committed data survives crashes)
- Transaction propagation —
REQUIREDjoins existing transaction or starts new;REQUIRES_NEWalways suspends the outer transaction and starts a completely independent one @Version— a numeric column (Long version) that JPA increments automatically on everyUPDATE; concurrent updates useWHERE id=? AND version=?— the second writer sees the version was already incremented and throwsOptimisticLockException- Optimistic vs Pessimistic Locking — Optimistic: no DB lock acquired, conflicts detected only at commit (fast, suitable when conflicts are rare); Pessimistic: acquires
SELECT FOR UPDATEimmediately (slower, prevents all concurrent modifications) TxType— the CDI equivalent of EJB'sTransactionAttributeType; usejakarta.transaction.Transactional.TxTypeenum values