412 words


2011-04-28 01:37

Ran into this error for the first time (after many years of using odb):

Caused by: com.objectdb.o._OptimisticLockException: Optimistic lock failed for object model.UserSession#28 (object has version 2 instead of 3)

Also the first time I'm using odb in C/S mode rather than embedded.

Also have the Explorer open


a) what is it about?

b) is there a setting for pessimistic mode? In my app, I don't expect more than one thread to access a database at any one time, and in the rare case that this happens, the queries are short lived enough that a small delay while one thread is blocked will not be a problem. Additionally, I don't imagine any deadlock situation.



dmoshal's picture
Joined on 2010-05-22
User Post #44
2011-04-28 02:58

I haven't had this issue yet with ObjectDB, but during my early days with hibernate I got this extensively. It just basically means that:

1) Version field incremented

2) New object with incremented field was not used in a subsequent persist operation

Are you using a merge or persist? With a 'em.merge()', you have to take care to assign the reference passed back because it will be different to the one you passed in (it is also attached). This new reference will contain the updated version field. This is what I'd look at first.

I've never used ObjectDB in embedded mode, but I'm thinking that since your instance is changed, but due to living in the same JVM as the database - any changes made to the reference are also made to the original instance, hence always in sync? I could likely be wrong here.

EDIT: Merge will attach

Willks's picture
Joined on 2011-04-04
User Post #10
2011-04-28 13:19

Optimistic locking is enabled by ObjectDB automatically even if no version field is defined.

You can disable optimistic locking checks (and eliminate these exceptions) but first verify that the exceptions do not indicate a problem in your application (i.e. that you update an object based on an old retrieval, overriding another recent update).

Additional information on optimistic locking is available in the manual.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
support's picture
Joined on 2010-05-03
User Post #218
2011-04-28 19:49

I guess I should read doco more. I ran into the same error from a pessimistically locked entity (global tracking id generator, specific to internal requirement) - and got the same issue - but only after stress testing the insertion test case. 5 threads/100 repetitions gave Optimistic lock error on pessimistically tracked entity.


Modified conf file, and got the test case working perfectly.


However, as the OP posted, if they are using 'merge' that they require to assign back the result of the merge. Still haven't used ObjectDB in embedded mode. My issue was concurrency, and as OP posted, they are mainly single threaded.

Willks's picture
Joined on 2011-04-04
User Post #12

Post Reply

Please read carefully the posting instructions - before posting to the ObjectDB website.

  • You may have to disable pop up blocking in order to use the toolbar (e.g. in Chrome).
  • Use ctrl + right click to open the browser context menu in the editing area (e.g. for using a browser spell checker).
  • To insert formatted lines (e.g. Java code, stack trace) - select a style in the toolbar and then insert the text in the new created block.
  • Avoid overflow of published source code examples by breaking long lines.
  • You may mark in paragraph code words (e.g. class names) with the code style (can be applied by ctrl + D).
  • Long stack traces (> 50 lines) and complex source examples (> 100 lines) should be posted as attachments.
Maximum file size: 32 MB