PrepZone Logo
PrepZone
Back to AI Coding

AI Coding · Medium

The assistant added @Transactional to make this transfer atomic. Will it work?

AI CodingCode ReviewSenior
java
@Service
public class TransferService {
    public void transfer(long from, long to, BigDecimal amount) {
        validate(from, to, amount);
        doTransfer(from, to, amount);
    }

    @Transactional
    private void doTransfer(long from, long to, BigDecimal amount) {
        Account a = accounts.findById(from).orElseThrow();
        Account b = accounts.findById(to).orElseThrow();
        a.debit(amount);
        b.credit(amount);
    }
}