Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How EntityManager flush

 
Harsha Hegde
Greenhorn
Posts: 17
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have an application using JPA. When I use EntityManager and createNamedQuery and fire a getResult on this I see a weird behavior
Sometimes EntityManager's flush take lot of time to execute. Wanted to understand why this is so? What is happening under-the-hood?
Regards,
Harsha
 
James Sutherland
Ranch Hand
Posts: 553
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default in JPA a flush() is triggered before any query execution. This is done to ensure the database reflects any changes made so it return the correct query results.

flush has to determine if any managed objects changed, and then write these changes to the database, so depending on how many managed objects and changes, this can take a while.

Normally a flush is not required, and sometimes even not desired, so it is best to turn it off using the EntityManager or Query setFlushMode() API.

See,
http://en.wikibooks.org/wiki/Java_Persistence/Querying#Flush_Mode

In EclipseLink you can disable the auto flush entirely using the persistence unit property,

"eclipselink.persistence-context.flush-mode"="COMMIT"
 
Harsha Hegde
Greenhorn
Posts: 17
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks a lot, it worked, there has been significant improvement in the performance
Regards,
Harsha
 
Harsha Hegde
Greenhorn
Posts: 17
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,
Followup to the above post, i also tried the Query.setFlushMode(). The performance improvement seem to be better than EntityManager.setFlushMode() what could be the reason?
Regards,
harsha
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic