Optimistic locking failure

#1

Optimistic locking failure:

I am using a Semaphore to force this execution of concurrents transactions T1 and T2 that cause parallel updates to A/B but it does not throw an OptimisticLockException and roll back.

           T1                           T2
----------------------------------------------------------
READ(A) version=1 value=0   |
READ(B) version=1 value=0   |
                             | READ(A)  version=1 value=0
                             | READ(B)  version=1 value=0
                             | B = A + B + 20
                             | WRITE(B) version=2 value=20
A = A + B + 10              |
WRITE(A) version=2 value=10 |
-----------------------------------------------------------
Results A: version=2 value=10, B: version=2 value=20

 

Thanks.

#2

Please explain the test output:

Sequential=true Lock=WRITE
Thread[Thread-2,5,main] READ  A1 version=1 value=0
Thread[Thread-2,5,main] READ  B2 version=1 value=0
Thread[Thread-2,5,main] WRITE A1 version=2 value=10
Thread[Thread-3,5,main] READ  A1 version=2 value=10
Thread[Thread-3,5,main] READ  B2 version=1 value=0
Thread[Thread-3,5,main] WRITE B2 version=2 value=30
A1 version=2 value=10, B2 version=2 value=30

Sequential=false Lock=WRITE
Thread[Thread-6,5,main] READ  A1 version=1 value=0
Thread[Thread-6,5,main] READ  B2 version=1 value=0
Thread[Thread-6,5,main] WRITE A1 version=2 value=10
Thread[Thread-7,5,main] READ  A1 version=2 value=10
Thread[Thread-7,5,main] READ  B2 version=1 value=0
Thread[Thread-7,5,main] WRITE B2 version=2 value=30
A1 version=2 value=10, B2 version=2 value=30
  • There are 2 runs. Which one demonstrates the issue? the sec
  • According to the output in both cases the execution of the threads is sequential, so exception is not expected. Please explain the problem.
ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#3

Please try this new version attached,  the output would be something like this:

Thread[Thread-1,5,main] READ  A1 version=1 value=0
Thread[Thread-1,5,main] READ  B2 version=1 value=0
Thread[Thread-2,5,main] READ  A1 version=1 value=0
Thread[Thread-2,5,main] READ  B2 version=1 value=0
Thread[Thread-2,5,main] WRITE B2 version=2 value=20
Thread[Thread-1,5,main] WRITE A1 version=2 value=10
A1 version=2 value=10, B2 version=2 value=20

Thanks.

text/x-java iconTest_2.java (4.41KB)
Only concurrent schedule execution
#4

Thank you for the revised test that demonstrates the issue well.

Please try build 2.3.7_23 that should throw an exception in this case.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#5

It would be great if was fixed the same problem with LockModeType.READ:

Thread[Thread-1,5,main] READ  A1 version=1 value=0
Thread[Thread-1,5,main] READ  B2 version=1 value=0
Thread[Thread-2,5,main] READ  A1 version=1 value=0
Thread[Thread-2,5,main] READ  B2 version=1 value=0
Thread[Thread-2,5,main] WRITE B2 version=2 value=20
Thread[Thread-1,5,main] WRITE A1 version=2 value=10
A1 version=2 value=10, B2 version=2 value=20

Thanks.

#6

You are right again. To support this, starting build 2.3.7_24 OPTIMISTIC (READ) is implemented as OPTIMISTIC_FORCE_INCREMENT (WRITE). This is allowed by JPA, and is required by ObjectDB in order to identify this conflict that your test demonstrates.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

Post Reply

To post a reply and/or subscribe to update notifications - please login