Race condition although using transactions

I have this transaction:

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");

However, as you can see here, the transaction does not interfere with the race condition:

[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 

The board is set to PAIDtwice. My exception is not thrown, and there is no rollback or anything else.

What am I doing wrong?

+3
source share
2 answers

. - , , , . , . . http://en.wikibooks.org/wiki/Java_Persistence/Locking#JPA_2.0_Locking.

, "" ( ) JPA @Version. , , . JPA , , .

: : https://blogs.oracle.com/carolmcdonald/entry/jpa_2_0_concurrency_and , JPA , , . , JDBC, " " , ; , .

+7

, . SERIALIZABLE, SQL, . PostgreSQL 9.1, MS SQL Server Oracle , . READ COMMITTED, , , SERIALIZABLE.

, Dan R.K. MIT, PostgreSQL 9.1, Wisconsin Courts . . Wiki.

+1

All Articles