微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – OptimisticLockException当使用JPA merge()

我有一个休息的应用程序,其中一个资源可以更新.以下是负责实现此任务的两种方法

> updateWithRelatedEntities(String,Store):接收通过反序列化PUT请求实体构造的id和新对象Store,在新对象上设置版本(用于乐观锁定),并在事务中调用更新.

public Store updateWithRelatedEntities(String id,Store newStore) {
    Store existingStore = this.get(id);

    newStore.setVersion(existingStore.getVersion());

    em.getTransaction().begin();
    newStore = super.update(id,newStore);
    em.getTransaction().commit();

    return newStore;
}

> update(String,T):进行更新的通用方法.检查ids匹配并执行合并操作.

public T update(String id,T newObj) {
   if (newObj == null) {
    throw new EmptyPayloadException(type.getSimpleName());
   }


   Type superclass = getClass().getGenericSuperclass();

   if (superclass instanceof Class) {
       superclass = ((Class) superclass).getGenericSuperclass();
   }

   Class<T> type = (Class<T>) (((ParameterizedType) superclass).getActualTypeArguments()[0]);

   T obj = em.find(type,id);

   if (!newObj.getId().equals(obj.getId())) {
       throw new IdMismatchException(id,newObj.getId());
   }

   return em.merge(newObj);
}

问题是这个调用:T obj = em.find(type,id);触发数据库中存储对象的更新,这意味着我们在触发合并时获得OptimisticLockException(因为版本现在不同).

为什么会发生这种情况?实现这一点的正确方法是什么?

我不想将属性从newStore复制到existingStore,并使用existingStore进行合并 – 我认为这将解决乐观锁定问题.

代码未在应用程序服务器上运行,并且我没有使用JTA.

编辑:
如果我在调用update之前分离existingStore,T obj = em.find(type,id);不会触发store对象的更新,这样可以解决问题.问题仍然存在 – 为什么当实体不分离时它会触发它?

解决方法

我看不到你的实体从您添加代码,但我相信你失去了一些关键点与乐观锁定> @Version注释版本字段.
如果您的实体上有这个字段,那么容器应该能够执行合并过程而没有问题.请看看
Optimistic Locking也很好的文章 don’t break optimistic locking

原文地址:https://www.jb51.cc/java/125401.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐