jpa FlushModeType COMMIT

你离开我真会死。 提交于 2019-12-06 15:24:42

问题


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

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.

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 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.

The jpa 2.0 spec says:

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.

So what FlushModeType COMMIT really does?

Tks in advance for your help.


回答1:


The javadocs mentions this here for FlushModeType COMMIT

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

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.



来源:https://stackoverflow.com/questions/34108792/jpa-flushmodetype-commit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!