Optimistic lock failed

#1

Hi,

I frequently see exceptions during development such as

com.objectdb.o._RollbackException: Failed to commit transaction: Optimistic lock failed for object com.example.MyEntity#3 (object has version 1 instead of 3)

Could you please explain what this means, and what I can do about it.

Thanks you,

Carl

 

 

#2

Hi,

Locking in general is explained on:

http://www.objectdb.com/java/jpa/persistence/lock

If you must avoid optimistic lock exceptions you can use pessimistic exceptions, but it is more efficient to remain with optimistic locking (which is the default) and just handle these exceptions.

A simple case of optimistic exception is demonstrated on:

http://www.objectdb.com/database/forum/41

The RollbackException wraps the real exception - see:

http://www.objectdb.com/api/java/jpa/exceptions

ObjectDB Support
#3

One more thing. You can disable optimistic locking and exceptions in the configuration:

    <database>
        <size initial="256kb" resize="256kb" page="2kb" />
        <recovery enabled="false" sync="false" path="." max="100mb" />
        <recording enabled="true" sync="false" path="." mode="write" />
        <locking version-check="false" />
        ...

This is currently an undocumented feature (that was added because of a user request) - but it is expected to be officially supported in next versions.

Update: The configuration above is now documented and part of ObjectDB 2.1.

ObjectDB Support
#4

You have to put a try catch block around it to handle the exception. The simplest solution is to make your method synchronized. This should work unless your accessing that object in the database from multiple methods simultaneously or you've got some super wierd situation where that causes a signifcant performance issue.If you can't use synchronized then you just have to catch the exception and try again in the catch block. If you still get the error then you can try waiting before trying the transaction again or trying multiple times.

Reply