Optimistic locking mechanism on external datastore

Hi,

I am building a cuba-application beside an existing business-application
based on an oracle database.
New functions should go bit by bit to the cuba side, but there is many functionality
which will stay for a long time in the old application.

Now my question:
What is the best way to implement an optimistic locking between both applications ?
I think in cuba you are using the “Versioned” interface to do this ?

Because it is not possible, to change the old application, but it is possible, to write
the locking (of the old app) info to the database from the cuba side, I thought about the following:

First: create a trigger on oracle to all tables, to increase on every change a numeric field, witch is
not used for anything else.
Second: I implement the interface Versioned to the Entity-classes and set and get the value
of this field.

Is this possible ?

Thanks very much.

Martin

1 Like

Hi Martin,

Optimistic locking is implemented by EclipseLink (JPA framework) and it assumes that some attribute of the entity has @Version annotation. The Versioned interface is just a declaration thing, it doesn’t affect what JPA does. When JPA issues SQL UPDATE, it includes the version field in the WHERE clause, so only the record with the expected version (not incremented by someone else) will be updated. If the update statement hits no records, the exception is thrown.

Perhaps if you annotate the attribute mapped to the new field with @Version, it will do the trick. But the trigger should not increment the field when the update goes from CUBA side - the value should come from the CUBA application.

Regards,
Konstantin

1 Like

Hi Konstantin,

thanks for your suggestion. I tried it and it works.
I changed the trigger in the oracle in the way, that increasing only
takes place, when :NEW.version <= :OLD.version. So from side of the old
app trigger is working and from new side the cuba mechanism is changing
the value.

Thanks and regards,

Martin

1 Like