That sucks. I hope that the expert group can change this so that simple pessimist locking can work portable in the future.

-dain

On Feb 21, 2008, at 1:53 PM, Evan Ireland wrote:

Dain,

Sounds like you are wanting a pessimistic lock. The semantics
of WRITE_LOCK are defined in optimistic terms in the spec.

The JPA 2.0 expert group is looking into this. In the mean time,
only non-portable apps can get this behaviour.

-----Original Message-----
From: Dain Sundstrom [mailto:[EMAIL PROTECTED]
Sent: Friday, 22 February 2008 9:50 a.m.
To: [email protected]
Subject: EntityManager.lock not working

I have a test case with two threads executing the following code:

    public void run() {
        try {
            beginTx();

            Employee david =
entityManager.getReference(Employee.class, pk);
            entityManager.lock(david, LockModeType.WRITE);
            entityManager.flush();

            Assert.assertTrue(entityManager.contains(david));

            Assert.assertEquals(david.getId(), pk);
            Assert.assertEquals(david.getFirstName(), "David");
            Assert.assertEquals(david.getLastName(), "Blevins");
            Assert.assertEquals(1000000.0, david.getSalary());

            log("READ");

            // wait for other threads to read
            try {
                startBarrier.await(2, TimeUnit.SECONDS);
            } catch (Exception e) {
            }


            log("WRITE");
            david.setSalary(2000000);

        } catch (Throwable throwable) {
            this.throwable = throwable;
        } finally {
            try {
                commitTx();
            } catch (Throwable throwable) {
                if (this.throwable == null) this.throwable =
throwable;
            }
        }
    }

The first thread successfully reads the row and waits.  The
second throws an exception at the entityManager.flush()
command after the write lock.  I would assume that when I say
"give me a write lock"
OpenJPA would "give me a write lock".  Is there anyway to get
write lock in my application for single rows without having
to switch everything to SERIALIZABLE?

-dain



Reply via email to