Hi list,

I encountered the weird situation that an optimistic lock violation happened. I 
would like you to judge this is my fault or a bug.

First I created an entity listed on here (https://gist.github.com/851075).
Then I wrote several tests that used the entity. In the following test, an 
optimistic lock violation happened.

=== CODE STARTS HERE ===
        EntityManager em = emf.createEntityManager();
        {
            em.getTransaction().begin();
            for (int i = 0; i < 10; ++i) {
                Questionnaire q = new Questionnaire("id" + i, "eventId", 
"question", i, "answerText");
                em.persist(q);
            }
            // em.flush(); // (1)
            em.getTransaction().commit();
        }
        
        {
            em.getTransaction().begin();
            // em.flush(); // (3)
            Query q = em.createQuery("DELETE FROM Questionnaires t");
            q.executeUpdate();
            em.getTransaction().commit(); // (2)
        }

        em.close();
=== CODE END HERE ===

In (2) the exception happened. The stack trace is attached at the last.
When I changed the field type of 'questionNo' to Integer (int -> Integer), the 
violation did not happen.

Some one advised me to use flush() at (1), but it did not help.

When I added (3) and traced the function, I found that 
ImplHelper.getUpdateField(sm) in AbstractUpdateManager#populateRowManager 
returned a dirty flag that said the 'questionNo' was dirty.

=== CODE HERE ===
            } else if ((dirty = ImplHelper.getUpdateFields(sm)) != null) { 
                update(sm, dirty, (ClassMapping) sm.getMetaData(), rowMgr,
                    store, customs, false);
=== END ===

I only used a single thread, so I don't think the entities were changed in the 
other threads. (Remember that the field type of 'questionNo' was changed to 
Integer, the code worked.)

Does anyone know why the violation happens?

My environment is
  Mac OS X Snow leopard, Sun Java 6, and OpenJPA-2.0.1.



Exception in thread "main" <openjpa-2.0.1-r422266:989424 fatal store error> 
org.apache.openjpa.persistence.RollbackException: Optimistic locking errors 
were detected when flushing to the data store.  The following objects may have 
been concurrently modified in another transaction: [entity.Questionnaire-id3, 
entity.Questionnaire-id1, entity.Questionnaire-id5, entity.Questionnaire-id2, 
entity.Questionnaire-id6, entity.Questionnaire-id0, entity.Questionnaire-id8, 
entity.Questionnaire-id9, entity.Questionnaire-id4, entity.Questionnaire-id7]
        at 
org.apache.openjpa.persistence.EntityManagerImpl.commit(EntityManagerImpl.java:584)
        at client.Test.run(Test.java:44)
        at client.Test.main(Test.java:17)
Caused by: <openjpa-2.0.1-r422266:989424 nonfatal store error> 
org.apache.openjpa.persistence.OptimisticLockException: Optimistic locking 
errors were detected when flushing to the data store.  The following objects 
may have been concurrently modified in another transaction: 
[entity.Questionnaire-id3, entity.Questionnaire-id1, entity.Questionnaire-id5, 
entity.Questionnaire-id2, entity.Questionnaire-id6, entity.Questionnaire-id0, 
entity.Questionnaire-id8, entity.Questionnaire-id9, entity.Questionnaire-id4, 
entity.Questionnaire-id7]
        at 
org.apache.openjpa.kernel.BrokerImpl.newFlushException(BrokerImpl.java:2291)
        at org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:2139)
        at org.apache.openjpa.kernel.BrokerImpl.flushSafe(BrokerImpl.java:2037)
        at 
org.apache.openjpa.kernel.BrokerImpl.beforeCompletion(BrokerImpl.java:1955)
        at 
org.apache.openjpa.kernel.LocalManagedRuntime.commit(LocalManagedRuntime.java:81)
        at org.apache.openjpa.kernel.BrokerImpl.commit(BrokerImpl.java:1479)
        at 
org.apache.openjpa.kernel.DelegatingBroker.commit(DelegatingBroker.java:925)
        at 
org.apache.openjpa.persistence.EntityManagerImpl.commit(EntityManagerImpl.java:560)
        ... 2 more
Caused by: <openjpa-2.0.1-r422266:989424 nonfatal store error> 
org.apache.openjpa.persistence.OptimisticLockException: An optimistic lock 
violation was detected when flushing object instance "entity.Questionnaire-id3" 
to the data store.  This indicates that the object was concurrently modified in 
another transaction.
FailedObject: entity.Questionnaire@72940b84
        at 
org.apache.openjpa.jdbc.kernel.PreparedStatementManagerImpl.flushAndUpdate(PreparedStatementManagerImpl.java:123)
        at 
org.apache.openjpa.jdbc.kernel.BatchingPreparedStatementManagerImpl.flushAndUpdate(BatchingPreparedStatementManagerImpl.java:81)
        at 
org.apache.openjpa.jdbc.kernel.PreparedStatementManagerImpl.flushInternal(PreparedStatementManagerImpl.java:99)
        at 
org.apache.openjpa.jdbc.kernel.PreparedStatementManagerImpl.flush(PreparedStatementManagerImpl.java:87)
        at 
org.apache.openjpa.jdbc.kernel.ConstraintUpdateManager.flush(ConstraintUpdateManager.java:550)
        at 
org.apache.openjpa.jdbc.kernel.ConstraintUpdateManager.flush(ConstraintUpdateManager.java:107)
        at 
org.apache.openjpa.jdbc.kernel.BatchingConstraintUpdateManager.flush(BatchingConstraintUpdateManager.java:59)
        at 
org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.flush(AbstractUpdateManager.java:103)
        at 
org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.flush(AbstractUpdateManager.java:76)
        at 
org.apache.openjpa.jdbc.kernel.JDBCStoreManager.flush(JDBCStoreManager.java:731)
        at 
org.apache.openjpa.kernel.DelegatingStoreManager.flush(DelegatingStoreManager.java:131)
        ... 9 more


-- 
Kawanaka Shinya <[email protected]>
http://mayah.jp/






Reply via email to