aspose file tools*
The moose likes Object Relational Mapping and the fly likes How EntityManager flush Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How EntityManager flush" Watch "How EntityManager flush" New topic
Author

How EntityManager flush

Harsha Hegde
Greenhorn

Joined: Jan 12, 2009
Posts: 17

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

Joined: Oct 01, 2007
Posts: 553
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"


TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Harsha Hegde
Greenhorn

Joined: Jan 12, 2009
Posts: 17

Hi,
Thanks a lot, it worked, there has been significant improvement in the performance
Regards,
Harsha
Harsha Hegde
Greenhorn

Joined: Jan 12, 2009
Posts: 17

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How EntityManager flush