问题描述
我正在创建一个批处理程序,它将从Oracle数据库中选择状态为“ x”的记录,并进行一些处理,一旦处理成功,就将状态更新为“ y”。
但是,同一批处理将有多个实例在运行,我必须确保没有两个批处理选择相同的记录为“ x”状态。我正在尝试通过在Spring数据中使用“选择...进行更新”来实现这一目标。
虽然测试相同,但我可以看到该锁并不限制其他会话中的读取。我无法发布原始代码,但下面显示了相同的伪代码。使用@Lock和@Transactional对Source的任何引用都将有所帮助。 ++我可以看到在跟踪日志中写入了正确的查询(选择更新),但是仍然没有获得锁定。
---Starting Pseudo code---
@Controller
class {
@PutMapping
@Transactional
methodCall(){
City city = emMethod(Long id)
//My print statement that data has been selected
sleep thread for 15 sec
//Print statement after sleep
update Method call here
}
}
---End of Controller class
@Repository
Class Repos{
@Autowired
EntityManager em;
public City emMethod(Long id){
Query q = em.CreateQuery("Select C from City C where id =:id");
q.setParameter("id",id);
**q.setLockMode(LockModeType.PESSIMISTIC_WRITE)**;
List list = q.getResultList();
return (City) list.get(0);
}
@Modifying
myUpdateMethod(City city){//update body here};
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)