說明
Shared mutable state between threads must be properly synchronised. Service beans (singletons by default in Spring) must not store request-scoped mutable state in instance fields.
為什麼重要
- Data races — concurrent modification causes unpredictable behaviour
- Heisenbugs — race conditions are nearly impossible to reproduce locally
- State corruption — partial writes visible to other threads
Enforcement
IS2_INCONSISTENT_SYNC — field is sometimes synchronised and sometimes not.
正例
@Service
public class OrderService {
// ✅ immutable or thread-safe state only
private final OrderRepository orderRepository;
// ✅ request-scoped state passed as method parameter
public Order process(OrderRequest request) {
// ...
}
}
反例
@Service
public class OrderService {
private Order currentOrder; // ❌ mutable shared state in singleton
public void process(OrderRequest request) {
this.currentOrder = orderRepository.findById(request.getId()); // ❌ data race
}
}