English 中文(简体)
JPA 实体仅在采用方法后更新
原标题:JPA entity updated only after method is done

我有附加说明的方法,通过计算参数接收一个实体。 在这种方法中,我试图确定三个变数:

@Inject EntityDao entityDao;

@Asynchronous
public Future<String> doSomething (MyEntity p_myEntity) {

    MyEntity myEntity = entityDao.merge(p_myEntity); // change from detached to attached
    // em.contains(myEntity) returns true

    myEntity.setName("Joe 1"); // newer set in database
    // A System.out.println(myEntity.getName()) does say "Joe 1"

    try {
        Thread.sleep(20*1000);
    } catch () ...etc

    myEntity.setName("Joe 2"); // newer set in database
    // A System.out.println(myEntity.getName()) does say "Joe 2"

    try {
        Thread.sleep(20*1000);
    } catch () ...etc

    myEntity.setName("Joe 3"); // only one set in database

    return new AsyncResult<>("done");
}

Edit: So thanks to PedroKowalski I have a better understanding of the problem and I will reformulate.

在采用上述方法期间,我有两种方式检查是否真心改变:

  • During the sleep() I check the database if the name value is changed
  • On a webpage the list of MyEntity objects are displayed (with the names), this list is being updated every 2 seconds.

上述两种方法都表明,数据库中“联合1”和“联合2”的数值较新。 只有在采用Something(Something)方法之后,最后的名称(Joe 3)才被列入数据库。

因此,我的问题是:为什么没有将“联合1”和“联合2”的数值列入数据库,而只有最后价值才列入数据库?

最佳回答

If you use JTA transactions than the transaction boundary spreads from method s begin to its end.

Therefore, changes made in an active transaction T1 can t be seen in transaction T2. If you think about it, it s quite reasonable. Assume that T2 could operate on data which was changed by T1 but not committed. Upon T1 rollback, every changes made to the entities in T1 must be invalidated. You ve ended in a situation in which T2 operated on invalid data.

因此,你从T1以外的任何交易中都发现了Joe 1(这一价值只在T1中发生变化)。 只有在方法结束时(T1承诺)才能见到Joe 2。

)将数据与基本数据库同步,但没有承诺。 详情请见

在这方面,我可以看到三个解决办法:

  1. 优化锁定可使你免于两种交易T1和T2改变相同数据(同一实体)的情况。 如果你没有锁定,那么,数据库中将只反映最后的已承诺交易变化(因此损失了原交易所作的改动)。 随着锁定,你将获得最后一笔交易的例外,因此不会丢失数据。

  2. 模拟锁定可以在修改时锁定数据。 在这种情况下,在T1完成交易之前,你的T2交易不会在数据上运作。

  3. 归根结底,最简单的情况是(如果可能的话)把你的方法与小丘分开。

HTH.

问题回答

So my question is: why are the values "Joe 1" and "Joe 2" not put in the database and only the last value is put in the database?

  • 这些数据是在胚胎期间写给数据库的。 在进行交易时,其他用户/用户都可以看到数据。

  • 如果交易发生,你不会人工冲入数据库。

  • 交易在你方法的末尾进行,也就是说,当变数发生时,即“Joe3”写给行,也是承诺的。





相关问题
query must begin with SELECT or FROM: delete [delete

I am using JPA in addition to spring(3.0.0.M4). While deleting multiple records using query.executeUpdate() i am getting the following exception. org.springframework.web.util.NestedServletException: ...

Last update timestamp with JPA

I m playing around a bit with JPA(Eclipselink to be specific). The below entity have a timestamp that s supposed to reflect whenever that entity was last updated. What are the strategies for making ...

@IdClass with non primative @Id

I m trying to add a composite primary key to a class and having a bit of trouble. Here are the classes. class User { private long id; ... } class Token { private User user; private ...

Database not dropped in between unit test

Hello good people i came accross a weird behaviour in my test.I m using JPA hibernate annotation with spring. let say i have an Class MyObject and it s property email is marqued @Column(name="EMAIL", ...

Toplink trying to persist null object

I have an object "Instance" with another object "Course" inside. When trying to persist a new Instance object, I get the following error if Course is null: java.lang.IllegalStateException: During ...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

热门标签