【发布时间】:2019-01-05 09:05:55
【问题描述】:
我用 Java 编写了一个任务调度程序,它每隔一分钟调用一次方法。现在这个应用程序被部署到 SIT 服务器上,它上面运行了 2 个实例。现在让我告诉你我构建的场景。
<task:scheduled-tasks scheduler="myScheduler">
<task:scheduled ref="myBean" method="takeLunch" fixed-delay="60000" />
</task:scheduled-tasks>
<task:scheduler id="myScheduler"/>
流量是
1. Get the employees who are ready to take the lunch. This is the eligiblity condition.
SELECT EMP_ID FROM EMPLOYEES WHERE WORK_STATUS='COMPLETED'
(这里会出现死锁,因为两个实例都试图同时触发查询吗?)
2. I've another table called "LUNCH_STATUS" where I will keep track of their lunch.
INSERT INTO LUNCH_STATUS(EMP_ID,STATUS) .....
此处将插入所有员工 ID,状态为空。
3. I will get the first employee from the LUNCH_STATUS whose status is empty and I will update in the same table as the status "LUNCH IN PROGRESS"
4. While taking lunch, I've some business logic, once the lunch is done, I will update the status as "COMPLETED"
UPDATE LUNCH_STATUS SET STATUS='COMPLETED' WHERE EMP_ID = ?
5. Once this update is done, I should update the main table EMPLOYEES
UPDATE EMPLOYEES SET WORK_STATUS='WORK RESUMED' WHERE EMP_ID=?
当我在本地机器上运行时,这可以正常工作,但有时在 SIT 服务器中却不行。
现在,这里的问题是,有时当多名员工有资格享用午餐时,即使流程完成,应用程序也不会更新状态,因为午餐已完成。某处记录被锁定。任何想法我应该考虑哪些步骤?
对于所有这些 DAO 方法(INSERT、SELECT 和 UPDATE),我将 @Transactional 注释和隔离属性用作 SERIALIZABLE。
请指导我应该去哪里使用锁定机制,或者应该重新设计流程以了解如何使用隔离。
【问题讨论】:
-
我已经回答了您的问题,如果设置有任何问题,请告诉我。
标签: java spring scheduled-tasks taskscheduler