19

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

Nav
  • 4,450
  • 10
  • 53
  • 84

1 Answers1

25

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();
ziesemer
  • 27,712
  • 8
  • 86
  • 94
  • 1
    I also change flush mode to commit instead of auto but nothing happens – Nav Jan 14 '12 at 17:02
  • 8
    Most JPA implementations will cache operations within the JVM (within the EntityManager). `flush()` merely forces these operations to be sent to the database, etc. - but does not imply a commit. http://stackoverflow.com/questions/4275111/correct-use-of-flush-in-jpa-hibernate has some additional details / discussion that may be helpful to you. – ziesemer Jan 14 '12 at 17:03
  • 5
    Setting the flush mode to commit is thinking about this backwards. This tells the EntityManager to only flush on commit. It doesn't mean to commit on flush. – ziesemer Jan 14 '12 at 17:04
  • 5
    This is a subtle detail, but flush mode of commit does not mean flush ONLY on commit. It means ALWAYS flush on commit; but flush could also happen at other times, depending on the JPA provider. – SteveT Aug 31 '12 at 18:46