我在AWS Elastic beantalk上有一个负载平衡的Spring Boot Web应用程序服务器。服务器访问相同的MySQL数据库。我们以编程方式在AWS上创建外部实体。需要监控这些实体的状态。
这将导致线程启动并检查相应实体的状态。我面临的问题是,当这些线程正在运行时(每个实例一个),应用程序本身无法从其他位置更新记录。
我一直收到错误消息“锁定等待超时。尝试重新启动事务”。我了解该记录已锁定在数据库前端,但不了解原因。下面是在该线程上实现的逻辑。
@Override
public void run() {
session = this.sessionFactory.openSession();
try {
while (true) {
Thread.sleep(5000);
validateWorkspaceState();
if (tries == 30)
break;
}
} catch (Exception e) {
//Some log
} finally {
singleNodecloudStackManagementService.clearThread(workspaceId);
this.session.close();
}
}
执行状态检查:
private void validateWorkspaceState() {
Transaction tx = null;
try {
tx = session.beginTransaction();
log.info("Validating status for workspace " + workspaceId);
rmlWorkspace = (RmlWorkspace) session.get(RmlWorkspace.class,workspaceId);
if (workspaceRequiresStatusCheck()) {
//Perform actions for status check
} else {
log.info("Exiting status check as the workspace does not require status check");
tries++;
}
txCommitAndOpenSession(tx);
} catch (RollbackException e) {
log.error(e.getMessage());
}
}
提交txn并再次打开会话:
public void txCommitAndOpenSession(Transaction tx) {
tx.commit();
session.close();
session = this.sessionFactory.openSession();
}
主要问题是-为什么它会导致锁定?