Im working with the same entitity in differents threads and I receive an optimisticlockexception
. I think that in my case this exception must be avoided. The situation is the following:
I have the following class A:
public class A{
List<B> b;
List<C> c;
public void addinListB(B elem){
b.add(elem);
}
public void addinListC(C elem){
c.add(elem);
}
}
Then, I have SomeObject class that use and modify A class. The SomeObject is:
public class SomeObject {
private static final Object lock = new Object();
public A search( String idA ){
synchronized (lock) {
A a = null;
try{
a = (A) getPersistenceContext()
.createQuery("from A where "+ id = :id ")
.setParameter("id", id).getSingleResult();
}catch (NoResultException e) {
A = new A();
getPersistenceContext().persist(bday);
getPersistenceContext().flush();
}
}
return a;
}
}
@Transactional
public someMethodB(){
A a = search(1);
B b = new B();
a.addBinListB(B);
}
@Transactional
public someMethodC(){
A a = search(1);
C c = new c();
a.addCinListC(c);
}
}
So, the problem is the following:
We have 2 theards running at the same time, one is executing "someMethodB(1)" and the other one is executing "someMethodC(1)" looking for the same A id.
Supposition: A for id = 1 doesn't exists:
This mean that the first one winning the lock will create the A instance and the other one will used it. Causing to be the same atatched obejct being modify concurrently in both threads. So, when the 1st thread commit the transaction the flush is done, but when the 2nd one commits, an optimisticlockexception
is being thrown.
Is possible avoid the optimisticlockexception
in this case? I understund that the A object is "modify" adding two differents object (B and C) BUT we can realize that the A object is ONE object in the database, only both thread are adding new objects in different tables that are pointed to A.
Is there any way to modify concurrently the A entity (adding to A several B and C objects) and avoid the optimisticlockexception
?
Regards