Spring Hibernate不更新实体
我有一个 Spring/JPA/Hibernate 应用程序,并且有一段代码每分钟由石英作业调用以发送一些通知。
@Transactional
public void sendNotifications(){
Set<MyEntity> entitySet = getDAO().findEntitiesToNotify();
for (MyEntity e : entitySet) {
sendEntityNotificationEmail(e);
e.setNotification(true);
this.merge(e);
}
return false;
}
这在大多数情况下都工作得很好。关键词是“大多数时候”,因为有时这个事务不会更新数据库,所以通知电子邮件每分钟都会发送一次,直到我手动去杀死quartz作业或更新数据库。日志中没有错误,数据库也没有更新。有谁知道可能是什么问题吗?或者我可以通过任何方式调试这个问题。是否会发生一些竞争条件,因为此方法每分钟都会调用一次,因此一个新线程在另一个线程正在处理它时再次调用同一任务,因此更改不会持久?
I have a Spring/JPA/Hibernate app and there is a piece of code which is called every minute by a quartz job to send out some notifications.
@Transactional
public void sendNotifications(){
Set<MyEntity> entitySet = getDAO().findEntitiesToNotify();
for (MyEntity e : entitySet) {
sendEntityNotificationEmail(e);
e.setNotification(true);
this.merge(e);
}
return false;
}
This works fine most of the time. The key phrase being "most of the time", because at times, this transaction does not update the database, so the notification email is sent every minute until I manually go and kill the quartz job or update the database. There is no error in the logs, nor is the database being updated. Does anyone have any idea on what could the issue be? Or any way I can debug this issue. Is there some race condition happening because this method is called every single minute, so a new thread is calling the same task again while another one is processing it, and therefore the change is not persisted?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在其他线程中发送通知可能比再次从数据库获取实体、更新实体状态并保存更好
it could be better to send notifications in other thread than get entity from database again, update entity state and save