jpa FlushModeType COMMIT [英] jpa FlushModeType COMMIT

查看:102
本文介绍了jpa FlushModeType COMMIT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在FlushModeType.AUTO模式下,持久性上下文在以下时间与数据库同步:

In FlushModeType.AUTO mode, the persistence context is synchronized with the database at the following times:

 before each SELECT operation 
 at the end of a transaction 
 after a flush or close operation on the persistence context

在FlushModeType.COMMIT模式下,意味着它不必刷新 执行查询之前使用持久性上下文,因为您已指示内存中没有更改的数据会影响数据库查询的结果.

In FlushModeType.COMMIT mode, means that it does not have to flush the persistence context before executing a query because you have indicated that there is no changed data in memory that would affect the results of the database query.

我在jboss中以6.0为例:

I have made an example in jboss as 6.0:

@Stateless
public class SessionBeanTwoA implements SessionBeanTwoALocal {

@PersistenceContext(unitName = "entity_manager_trans_unit")
protected EntityManager em;

@EJB
private SessionBeanTwoBLocal repo;

@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void findPersonByEmail(String email) {

    1. List<Person> persons = repo.retrievePersonByEmail(email);
    2. Person person = persons.get(0);
    3. System.out.println(person.getAge());
    4. person.setAge(2);
    5. persons = repo.retrievePersonByEmail(email);
    6. person=persons.get(0);
    7. System.out.println(person.getAge());

}

}

@Stateless
public class SessionBeanTwoB extends GenericCrud implements     SessionBeanTwoBLocal {

@Override
public List<Person> retrievePersonByEmail(String email) {
    Query query = em.createNamedQuery("Person.findAllPersonByEmail");
    query.setFlushMode(FlushModeType.COMMIT);
    query.setParameter("email", email);
    List<Person> persons;
    persons = query.getResultList();
    return persons;
}
}

FlushModeType.COMMIT似乎不起作用.在第1行,从数据库中获取该人的年龄,并在第3行打印35.在第4行,此人在持久上下文中更新,但在第7行中,该人的年龄为2.

FlushModeType.COMMIT does not seem to work. At line 1., the person's age is taken from the database and print 35 at line3. At line 4., the person is updated within the persistent context but in line 7. the person's age is 2.

jpa 2.0规范说:

The jpa 2.0 spec says:

设置了

Type.COMMIT,对持久化上下文中的实体进行查询更新的效果是 未指定.

Type.COMMIT is set, the effect of updates made to entities in the persistence context upon queries is unspecified.

但是在很多书中,它们解释了我在本文开头所写的内容.

But in many books, they explains what I wrote in the beginning of this post.

那么FlushModeType COMMIT到底做什么呢?

So what FlushModeType COMMIT really does?

先谢谢您的帮助.

推荐答案

javadocs 在此处为FlushModeType COMMIT

在事务提交时发生刷新.提供者可以在以下位置刷新 其他时间,但不是必需的.

Flushing to occur at transaction commit. The provider may flush at other times, but is not required to.

因此,如果提供者认为应该这样做,那么即使将其配置为在提交时刷新,它也可以刷新.由于对于AUTO设置,提供程序通常会在不同的时间刷新(这需要昂贵地遍历所有被管理实体-特别是如果数量巨大时,将检查是否需要安排任何数据库更新/删除),以便我们确定是否有没有数据库更改发生,那么我们可以使用COMMIT设置减少对任何更改的频繁检查并节省一些CPU周期.

So if the provider thinks it should then it can flush even though it is configured to flush on commit. Since for AUTO setting, the provider typically flushes at various times ( which requires expensive traversal of all managed entities - especially if the number is huge- to check if any database updates/deletes needs to be scheduled) so if we are sure that there are no database changes happening then we may use COMMIT setting to cut down on frequent checks for any changes and save on some CPU cycles.

这篇关于jpa FlushModeType COMMIT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆