This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How EntityManager flush
 
Similar Threads
EntityManager.setFlushMode(COMMIT) Vs. Query.setFlushMode(COMMIT)
will the Query object always take preference over EntityManager on setting flushmode?
J2EE transactions - stateless beans (CMT)
WA #1.....word association
flush() Vs refresh()