我有一个 Java/Spring 应用程序,我使用 websockets 来同步数据。我有多个套接字方法来更新某些实体。我还有一个更新方法,根据接收到的数据更新实体,例如: 当我得到字段 x 时,我只更新字段 x。
但是,当我尝试更新某个字段时,它会在一段时间后崩溃,并出现超过超时的情况。增加
PessimisticLockException
中的超时不起作用,因为它只会在再次崩溃之前尝试更长的时间。现在奇怪的部分是,应用程序崩溃后,实体似乎出现了问题
phpyMyAdmin
因为我无法自己更新特定字段,因为我在这里也遇到超时。但是,当我在数据库中创建一个新实体并在使用 websocket 之前自己更新 phpMyAdmin 中的字段时,它确实可以工作并且不会崩溃。我多次尝试删除和创建数据库,但总是出现相同的错误。
其他人有解决此问题的经验吗?我该如何解决它?如果可能的话,我可以通过Teamviewer
等展示确切的经验,以便更好地理解问题。我使用跟踪调试检查了更新查询,但发送的数据并不奇怪。 Websocket方法代码:
phpMyAdmin
错误:
@MessageMapping("/entity/update/{bordId}")
@SendTo("/entity/updated/{bordId}")
public Pair<BordRowItem, Integer> updateBordRowItem(String message) {
JSONObject object = new JSONObject(message);
EntityManager entityManager = entityManagerFactory.createEntityManager();
BordRowItem item;
ArrayList<BordRowItemEvent> events = new ArrayList<>();
Optional<BordRowItem> optionalBordRowItem = bordRowItemRepository.findById(object.getInt("id"));
if(!optionalBordRowItem.isPresent()) {
return null;
}
entityManager.getTransaction().begin();
item = optionalBordRowItem.get();
if(object.has("description")) {
String msg = (item.getDescription() != null && !item.getDescription().isEmpty()) ? " van '" + item.getDescription() + "'" : "";
BordRowItemEvent event = new BordRowItemEvent("changed" + msg + " to '" + object.getString("description") + "'");
event.setBordRowItem(item);
events.add(event);
item.setDescription(object.getString("description"));
}
for(BordRowItemEvent event : events) {
if (!ObjectUtils.isEmpty(event) && !entityManager.contains(event)) {
entityManager.persist(event);
entityManager.flush();
}
}
bordRowItemRepository.save(item);
entityManager.flush();
entityManager.getTransaction().commit();
entityManager.close();
return new Pair<>(item, item.getBordRow().getId());