我在我的项目中使用spring jpa事务。一个案例包括在同步方法中插入数据,以及另一个线程访问数据时未更新数据。我的代码如下:@transactional在春天jpa不更新表
public UpdatedDTO parentMethod(){
private UpdatedDTO updatedDTO = getSomeMethod();
childmethod1(inputVal);
return updatedDTO;
}
@Transactional
public synchronized childmethod1(inputVal){
//SomeCodes
//Place where update takes place
TableEntityObject obj = objectRepository.findByInputVal(inputVal);
if(obj == null){
childMethod2(inputVal);
}
}
@Transactional
public void childMethod2(inputVal){
//Code for inserting
TableEntityObject obj = new TableEntityObject();
obj.setName("SomeValue");
obj.setValueSet(inputVal);
objectRepository.save(obj);
}
现在,如果两个线程同时访问,并且如果第一线程完成childmethod2和childmethod1和没有后完成parentMethod()如果第二线程说到childMethod1(),并且如果存在数据的检查,该数据为空并没有通过第一线更新。我已经尝试了很多方法,如
@Transactional(propagation = Propagation.REQUIRES_NEW)
public synchronized childmethod1(inputVal){
//SomeCodes
//Place where update takes place
TableEntityObject obj = objectRepository.findByInputVal(inputVal);
if(obj == null){
childMethod2(inputVal);
}
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void childMethod2(inputVal){
//Code for inserting
TableEntityObject obj = new TableEntityObject();
obj.setName("SomeValue");
obj.setValueSet(inputVal);
objectRepository.save(obj);
}
也试图在childMethod1()中取消@transactional,但没有任何结果。我知道我在这里做错了什么,但无法弄清楚我在哪里做错了什么。可以帮我解决这个问题
为什么你的方法同步和事务? – 2014-10-06 11:42:45