【问题标题】:Refresh with JPA connection使用 JPA 连接刷新
【发布时间】:2012-06-14 13:03:07
【问题描述】:

我正在使用 EJB3,但在刷新我的 EntityManager 时遇到了问题。

问题是我的应用程序必须准备好供许多人使用,但如果一个人更新同一日期,EntityManager 不会阻塞该行并且它看不到其他人在 x 分钟后所做的更改。

当我执行“选择”、“插入”、“更新”或“删除”时,有人知道如何刷新 EntityManager 吗?

【问题讨论】:

  • 防止最后一次写入获胜?还是在这样做之前?

标签: java jpa orm ejb-3.0


【解决方案1】:

这是一个并发问题,您应该阅读JPA Specification 第 3.4 节关于锁定和并发的内容。这包括乐观和悲观锁定以及版本化属性的使用。

另外,第 3.2.5 节介绍了如何刷新实体实例。

【讨论】:

    【解决方案2】:

    尝试刷新实体。例如:

    public void create(MyEntity entity) {
        getEntityManager().persist(entity);
        getEntityManager().flush();
        getEntityManager().refresh(entity);
    }
    
    public void edit(MyEntity entity) {
        getEntityManager().merge(entity);
        getEntityManager().flush();
        getEntityManager().refresh(entity);
    }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-17
      • 1970-01-01
      • 2019-05-31
      相关资源
      最近更新 更多