transactional-patterns

Solid

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.

AI & Automation 225 stars 37 forks Updated 6 days ago MIT

Install

View on GitHub

Quality Score: 88/100

Stars 20%
78
Recency 20%
100
Frontmatter 20%
70
Documentation 15%
100
Issue Health 10%
80
License 10%
100
Description 5%
100

Skill Content

# Transactional Patterns ## Basic Rules - `@Transactional` belongs on **service methods**, never controllers or repositories - Default propagation is `REQUIRED` — joins existing transaction or creates one - Always use on methods that write to the DB or coordinate multiple writes - `@Transactional(readOnly = true)` on all read-only service methods — enables optimizations ```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 | 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 | ```java // REQUIRES_NEW — for audit logging that must survive rollback @Transactional(propagation = Propagation.REQUIRES_NEW) public void logAuditEvent(AuditEvent event) { auditRepository....

Details

Author
rrezartprebreza
Repository
rrezartprebreza/spring-boot-skills
Created
4 months ago
Last Updated
6 days ago
Language
Java
License
MIT

Integrates with

Similar Skills

Semantically similar based on skill content — not just same category