What is a Transaction?
- A transaction is a group of operations that must be treated as one unit.
- In database terms, it follows ACID:
- Atomicity → all succeed OR all fail
- Consistency → data stays correct
- Isolation → concurrent transactions don’t break each other
- Durability → once committed, data is permanent
Why Transaction Alone Is NOT Enough to solve high concurrency ?
- Because under very high concurrency:
- DB locks become bottlenecks
- too many transactions slow DB
- row lock contention increases
- deadlocks appear
- DB CPU explodes
- Example : 100k users claiming coupon. If every request directly:
- begin transaction
- query stock
- update stock
- insert records
- commit
- Database may collapse.
What is @Transactional?
@Transactionalis implemented by AOP to automatically manage transaction lifecycle for business methods.@Service public class CouponService { @Transactional public void claimCoupon(Long userId, Long campaignId) { // 1. check stock // 2. deduct stock // 3. create user coupon // 4. create claim record } }- Spring automatically start transaction before method, commit if success or rollback if exception.
How it actually works (very important for debugging)
- Step 1: Spring creates a proxy object —— YourService → Proxy → Real Method
- Step 2: AOP intercepts method call —— call method() → [AOP intercept] → start transaction → execute method → success>commit/error>rollback
Transaction only works on PUBLIC methods
Self-call will FAIL (classic bug) —— because AOP proxy is bypassed
public void A() { B(); // ❌ transaction NOT applied } @Transactional public void B() {}Default rollback only for RuntimeException