Optimistic lock failed



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,






Locking in general is explained on:


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:


The RollbackException wraps the real exception - see:


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

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

        <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
ObjectDB - Fast Object Database for Java (JPA/JDO)

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.

Post Reply

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