Hello,
we use hibernate, oracle and pessimstic locking, to avoid concurrent modification through different threads.
This worked fine for a while. But now we sometimes get deadlocks. We analyzied and found the cause.
Our mapping:
Code:
<class name="TraceObject" table="TraceObjects">
<cache usage="read-write" />
<id name="objectId" column="objectId" type="long">
<generator class="native">
<param name="sequence">TraceObjectsSeq</param>
</generator>
</id>
<timestamp name="dbVersionControl" />
...
</class>
<joined-subclass name="VehicleObject" table="VehicleObjects" extends="TraceObject">
<key column="id" />
...
</joined-subclass>
In thread A we retrieve the vehicleObject through Criteria:
Code:
criteria.setLockMode(LockMode.UPGRADE);
criteria.uniqueResult();
This results in a select statement with
FOR UPDATE OF TraceObjects.objectId. This only locks the relevant data in table TraceObjects.
In thread B we retrieve the vehicleObject through Session:
Code:
getSession().get(getPersistentClass(), ao_Id, LockMode.UPGRADE);
This results in a select statement with
FOR UPDATE. This locks the relevant data in table TraceObjects and joined table VehicleObjects.
We think, our problem are the different deepnesses of the locks.
Is there a way to create locks on criterias just with "FOR UPDATE" not with "FOR UPDATE OF"? Does anybody know, why the locking from session and from criteria works different?