Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Use when working with @Transactional, multi-step database operations, distributed transactions, or any code that needs atomicity guarantees. Covers propagation rules, isolation levels, read-only optimization, and common pitfalls.
.claude/skills/rrezartprebreza-transactional-patterns/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-01 | ✗→✓ | ▲ Improved | 12% | 0% |
| case-10 | ✗→✓ | ▲ Improved | 98% | 0% |
| case-19 | ✗→✓ | ▲ Improved | 1% | 0% |
| case-22 | ✗→✓ | ▲ Improved | 62% | 0% |
| case-02 | ✓→✓ | = Same ✓ | 74% | 0% |
rollback configuration and caller before changing propagation.
Spring Data repository transactions; repository annotations are not inherently wrong.
REQUIRED — joins existing transaction or creates onereadOnly = true for appropriate read units of work. It is an optimization hint,not write prevention, authorization or automatic routing to a read replica.
does not make writes to another database or an HTTP provider atomic.
The snippets below are illustrative; domain classes and imports come from the application. Adapt the service template and compare the good and bad examples when editing a service.
java@Service @RequiredArgsConstructor @Transactional(readOnly = true) // default for all methods in this service public class OrderService { @Transactional // overrides readOnly for writes public Order createOrder(CreateOrderRequest request) { inventoryService.reserve(request.items()); // participates in same TX return orderRepository.save(Order.from(request)); } public Optional<Order> findById(UUID id) { return orderRepository.findById(id); // readOnly = true inherited } }
| Propagation | Behavior | |-------------|----------| | REQUIRED (default) | Join existing TX or create new | | REQUIRES_NEW | Always create new TX, suspend existing | | SUPPORTS | Join if exists, proceed without TX if not | | NOT_SUPPORTED | Always run without TX | | MANDATORY | Must have existing TX, throw if not | | NEVER | Must NOT have TX, throw if one exists |
Participating REQUIRED calls use the outer transaction's isolation, timeout and read-only settings. Catching an inner failure does not clear its rollback-only marker; outer commit may still throw UnexpectedRollbackException.
REQUIRES_NEW needs another connection while the outer transaction retains its resources. Account for pool capacity and lock contention. Reserve independent commits for records that must survive a failed operation (for example, an attempted action), not a success audit row that claims a rolled-back order was created. Do not reference an uncommitted parent row from an independent audit transaction.
java// REQUIRES_NEW — for audit logging that must survive rollback @Transactional(propagation = Propagation.REQUIRES_NEW) public void logAuditEvent(AuditEvent event) { auditRepository.save(event); // commits independently of parent TX } // Illustrative: record an attempt for an existing order; failures are unchecked. @Transactional public void processOrder(Order order) { auditService.logAuditEvent(new AuditEvent("ORDER_START", order.getId())); try { // ... process, may throw } catch (RuntimeException e) { auditService.logAuditEvent(new AuditEvent("ORDER_FAILED", order.getId())); throw e; // parent TX rolls back, audit TX already committed } }
java// ❌ BROKEN — self-invocation bypasses Spring proxy, @Transactional ignored @Service public class OrderService { @Transactional public void processAll(List<UUID> ids) { ids.forEach(id -> this.processSingle(id)); // bypasses proxy! } @Transactional(propagation = Propagation.REQUIRES_NEW) public void processSingle(UUID id) { ... } // never creates new TX } // ✅ FIX — extract the independently transactional operation to another bean @Service @RequiredArgsConstructor public class OrderService { private final OrderProcessor orderProcessor; // separate bean @Transactional public void processAll(List<UUID> ids) { ids.forEach(id -> orderProcessor.processSingle(id)); // goes through proxy } }
java// Default: RuntimeException and Error roll back; checked exceptions do not. // Check project-wide rollback configuration before adding per-method rules. @Transactional(rollbackFor = InsufficientInventoryException.class) // checked exception public Order createOrder(CreateOrderRequest request) throws InsufficientInventoryException { ... }
Never use noRollbackFor to recover from OptimisticLockException: the persistence provider marks the transaction rollback-only. Roll it back and retry the whole unit of work in a fresh transaction, with a bounded policy and a fresh entity read.
java@Entity public class Order { @Version private Long version; // Hibernate handles conflicts automatically } // Handles concurrent updates @Transactional public Order updateStatus(UUID id, OrderStatus newStatus) { Order order = orderRepository.findById(id).orElseThrow(); order.updateStatus(newStatus); // conflict can surface at flush or commit return orderRepository.save(order); }
Retry support ships in core Spring Framework (org.springframework.resilience.annotation) — no Spring Retry dependency. Enable once with @EnableResilientMethods, then retry transient failures such as optimistic-lock conflicts:
java@Configuration @EnableResilientMethods public class ResilienceConfig {} @Service @RequiredArgsConstructor public class OrderStatusFacade { private final OrderService orderService; // separate bean — retry must wrap the TX @Retryable(includes = ObjectOptimisticLockingFailureException.class, maxRetries = 3, delay = 50, jitter = 25) public Order updateStatus(UUID id, OrderStatus newStatus) { return orderService.updateStatus(id, newStatus); // fresh @Transactional per attempt } }
Put @Retryable on a method that calls the @Transactional method on another bean — each attempt needs a fresh transaction. Retrying inside the failed transaction re-runs code in a TX already marked rollback-only. For hot write paths, @ConcurrencyLimit(10) (same package) caps concurrent invocations instead of letting contention turn into retry storms.
Use the version-specific retry guidance for dependencies and annotation imports. A retrying facade must not already hold a transaction that each attempt's REQUIRED method would join.
A saga coordinates durable local steps; wrapping remote calls in @Transactional does not implement one.
independently, and reconcile timeouts where the remote outcome is unknown.
Do not save failure state and then throw a rollback-triggering exception from the same transaction: that erases the state. Recovery must resume from durable state after a crash. Use the project's existing workflow mechanism; see messaging/outbox and idempotency for delivery and duplicate effects.
For best-effort local notifications, a commit-bound listener prevents delivery on rollback. For required delivery, persist an outbox entry with the business change or use the project's durable publication mechanism. AFTER_COMMIT alone cannot recover a process crash or retry a failed delivery.
java// Publisher — inside the TX @Transactional public Order place(UUID id) { Order order = orderRepository.findById(id).orElseThrow(); order.place(); eventPublisher.publishEvent(new OrderPlaced(order.getId())); // published now; listener defers handling return orderRepository.save(order); } // Listener — runs ONLY if the TX commits successfully @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) public void onOrderPlaced(OrderPlaced event) { emailService.sendConfirmation(event.orderId()); // best effort; no durable retry }
AFTER_COMMIT runs after the DB commits, but resources may still be bound to the completed transaction. Delegate database writes to a separate REQUIRES_NEW bean. Listener failure cannot undo the committed business write; make delivery failures observable and recoverable. Without an active transaction the listener is skipped by default. @Async does not add durable delivery. See domain events.
Test through Spring proxies and verify committed data from a new transaction. Cover checked versus unchecked failures, inner rollback-only propagation, optimistic conflicts at commit, audit rollback, and listener behavior after commit and rollback. Use the production database engine for isolation and contention tests. A test-wide rollback can hide after-commit behavior.
@Transactional methods on this — self-invocation bypasses proxyrollbackFor@Transactional on private methods — Spring proxy can't interceptspring-retry for a new Boot 4 retry path - use core @Retryable and @EnableResilientMethods; preserve existing integrations when outside the task's scope.| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-01 | fail→pass | 24,593 | 14,568 | -41% | 1 | 1 | 0% | 4,187 | 4,702 | +12% | 0 | 0 | — |
case-02 | pass→pass | 17,377 | 15,923 | -8% | 1 | 1 | 0% | 2,716 | 4,738 | +74% | 0 | 0 | — |
case-03 | pass→pass | 16,746 | 14,759 | -12% | 1 | 1 | 0% | 2,498 | 4,496 | +80% | 0 | 0 | — |
case-04 | pass→pass | 13,453 | 60,367 | +349% | 1 | 1 | 0% | 2,114 | 4,103 | +94% | 0 | 0 | — |
case-05 | pass→pass | 14,225 | 10,843 | -24% | 1 | 1 | 0% | 2,178 | 4,149 | +90% | 0 | 0 | — |
case-06 | pass→pass | 13,875 | 9,418 | -32% | 1 | 1 | 0% | 2,378 | 3,827 | +61% | 0 | 0 | — |
case-07 | pass→pass | 10,357 | 7,329 | -29% | 1 | 1 | 0% | 1,488 | 3,416 | +130% | 0 | 0 | — |
case-08 | pass→pass | 18,244 | 15,339 | -16% | 1 | 1 | 0% | 2,499 | 4,705 | +88% | 0 | 0 | — |
case-09 | pass→pass | 21,063 | 12,324 | -41% | 1 | 1 | 0% | 3,171 | 4,350 | +37% | 0 | 0 | — |
case-10 | fail→pass | 11,609 | 4,836 | -58% | 1 | 1 | 0% | 1,574 | 3,123 | +98% | 0 | 0 | — |
case-11 | pass→pass | 17,741 | 13,731 | -23% | 1 | 1 | 0% | 2,698 | 4,036 | +50% | 0 | 0 | — |
case-12 | pass→pass | 10,397 | 11,272 | +8% | 1 | 1 | 0% | 1,555 | 3,953 | +154% | 0 | 0 | — |
case-13 | pass→pass | 18,347 | 12,051 | -34% | 1 | 1 | 0% | 2,549 | 4,132 | +62% | 0 | 0 | — |
case-14 | pass→pass | 14,598 | 10,373 | -29% | 1 | 1 | 0% | 2,156 | 4,097 | +90% | 0 | 0 | — |
case-15 | pass→pass | 9,028 | 65,434 | +625% | 1 | 1 | 0% | 1,378 | 3,448 | +150% | 0 | 0 | — |
case-16 | pass→pass | 10,938 | 8,478 | -22% | 1 | 1 | 0% | 1,747 | 3,618 | +107% | 0 | 0 | — |
case-17 | pass→pass | 17,692 | 15,638 | -12% | 1 | 1 | 0% | 2,922 | 4,601 | +57% | 0 | 0 | — |
case-18 | pass→pass | 17,568 | 25,644 | +46% | 1 | 1 | 0% | 2,536 | 3,995 | +58% | 0 | 0 | — |
case-19 | fail→pass | 39,286 | 11,655 | -70% | 1 | 1 | 0% | 3,914 | 3,943 | +1% | 0 | 0 | — |
case-20 | pass→pass | 11,036 | 7,862 | -29% | 1 | 1 | 0% | 1,579 | 3,335 | +111% | 0 | 0 | — |
case-21 | pass→pass | 13,583 | 9,502 | -30% | 1 | 1 | 0% | 1,848 | 3,840 | +108% | 0 | 0 | — |
case-22 | fail→pass | 18,581 | 15,398 | -17% | 1 | 1 | 0% | 3,039 | 4,937 | +62% | 0 | 0 | — |
case-23 | pass→pass | 11,173 | 13,372 | +20% | 1 | 1 | 0% | 2,151 | 4,463 | +107% | 0 | 0 | — |
case-24 | pass→pass | 24,735 | 39,434 | +59% | 1 | 1 | 0% | 3,772 | 5,940 | +57% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 24 cases were attempted. The headline lift of +17 percentage points is the difference between those two pass rates over the 24 comparable cases.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
| Model | Method | Date | Lift |
|---|---|---|---|
| gemini-3.6-flash | verified | 9/1/2026 | +25% |
Other measured skills in the registry, with their headline benchmark lift.