我有这个交易:
em.getTransaction().begin();
{
final Payment payment = em.find(Payment.class, id);
if (payment.status != Status.INIT)
throw new IllegalStateException("Cannot set to PAID, is not INIT but " + status);
payment.status = Status.PAID;
}
em.getTransaction().commit();
log.info("Payment " + id + " was paid");
但是,正如您在此处看到的,事务不会阻止竞争条件:
[11:10:18.265] INFO [PaymentServlet] [MSP] Status COMPLETED
[11:10:18.265] INFO [PaymentServlet] Payment c76f9e75-99d7-4721-a8ac-e3a638dd8317 was paid
[11:10:18.267] INFO [PaymentServlet] [MSP] Status COMPLETED
[11:10:18.267] INFO [PaymentServlet] Payment c76f9e75-99d7-4721-a8ac-e3a638dd8317 was paid
付款设置为PAID
两次。我的异常没有被抛出,也没有回滚或任何东西。
我究竟做错了什么?