OptimisticLockException

#1

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

Questions:

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.

 

 

#2

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

#3

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
#4

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.

Reply