Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how we can get JPA EntityManager Flush work

my question is why flush doesn't work :

public void ejbService(){
   Customer c = em.find(Customer.class,1);
   c.setName("newName");
   em.flush();
   //at this point when I query mysql table I can not see "newName"


   thread.sleep(10000);

   c.setName("anotherName");
}

After finishing the method I see "anotherName" in the db also I check it with em.find(Customer.class,1,Lock.None); but still not work

RGDS

like image 604
Nav Avatar asked Jan 14 '12 16:01

Nav


People also ask

What does EntityManager flush () do?

The EntityManager. flush() operation can be used the write all changes to the database before the transaction is committed. By default JPA does not normally write changes to the database until the transaction is committed. This is normally desirable as it avoids database access, resources and locks until required.

How do I set JPA to flush mode?

The way you can configure a query-specific flush mode depends on the FlushModeType you want to set. If you want to use the FlushModeTypes AUTO or COMMIT, which are defined by the JPA specification, you can call the setFlushMode method on your Query or TypedQuery interface. Query q = em.

Does JPA flush commit?

JPA also defines a COMMIT flush mode, which is described as follows: If FlushModeType. COMMIT is set, the effect of updates made to entities in the persistence context upon queries is unspecified. When executing a JPQL query, the persistence context is only flushed when the current running transaction is committed.

How does EntityManager work in JPA?

In JPA, the EntityManager interface is used to allow applications to manage and search for entities in the relational database. The EntityManager is an API that manages the lifecycle of entity instances. An EntityManager object manages a set of entities that are defined by a persistence unit.


1 Answers

You're flushing, but you're not committing - or otherwise ending the transaction / session which is likely configured for auto-commit.

Yes, after calling flush(), the DBMS is now aware of your data - but following ACID standards, no other database sessions will see this data until the DBMS is told to commit it.

Without knowing additional details about the architecture behind the rest of your application, etc., you're probably looking to do something like:

em.getTransaction().commit();
like image 131
ziesemer Avatar answered Sep 29 '22 02:09

ziesemer