Video in progress

Middle 🌱 Spring

What's the difference between REQUIRED and REQUIRES_NEW propagation in @Transactional?

REQUIRED (the default) joins an open transaction if there is one, otherwise opens a new one. REQUIRES_NEW always opens a new, separate transaction, and the outer one is suspended meanwhile.

📖 Answer

Say the audit record has to stay no matter what happens to the order.

@Service
class OrderService {
    @Transactional
    public void place(Order o) {
        orders.save(o);
        audit.log("order placed " + o.getId());
        payments.charge(o); // failure here -> rollback
    }
}

@Service
class AuditService {
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void log(String msg) { auditRepo.save(new AuditEntry(msg)); }
}

With REQUIRED, the audit would be rolled back together with the outer transaction. With REQUIRES_NEW it commits on its own and stays.

But it isn't free. Every REQUIRES_NEW is a separate DB connection: the outer transaction holds the first one while grabbing a second. In production with a small pool and heavy load you get something that looks like a deadlock — everyone is stuck waiting for a second connection. Also, the inner transaction doesn't see the outer one's uncommitted data.

And the inner method must be in a different bean — call it from the same class and propagation does nothing.