Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

EntityManager.setFlushMode(COMMIT) Vs. Query.setFlushMode(COMMIT)

 
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[P-331 of EJB3 In Action Book] states:
"By default, the database flush mode is set to AUTO. This means that the Entity-Manager performs a flush operation automatically as needed.
In general, this occurs at the end of a transaction for transaction-scoped EntityManagers and when the persistence context is closed for
application-managed or extendedscope EntityManagers. In addition, if entities with pending changes are used in a query, the persistence
provider will flush changes to the database before executing the query.If the flush mode is set to COMMIT, the persistence provider will
only synchronize with the database when the transaction commits.However, you should be careful with this, as it will be your responsibility
to synchronize entity state with the database before executing a query. If you don’t do this and an EntityManager query returns stale
entities from the database, the application can wind up in an inconsistent state."

[P-353 EJB3 In Action Book] states:
"If the Query is set to FlushModeType.COMMIT, the effect of updates made to entities in the persistence context is not defined
by the specification, and the actual behavior is implementation specific".

So setting the flush mode as COMMIT on EntityManager and Query have different results..Right ??
 
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would expect them to have the exact same behavior. i.e. the query will not cause a flush before execution.

If you are using EclipseLink we also provide a persistence unit property to configure this,

"eclipselink.persistence-context.flush-mode"="COMMIT"

In case you don't want the flush by default, as it can be a major performance and even functional issue.
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What i really want to ask is :

Entitymanager - If the flush mode is set to COMMIT, the persistence provider will only synchronize with the database when the transaction commits


Query - If the Query is set to FlushModeType.COMMIT, the effect of updates made to entities in the persistence context is not defined by the JPA specification...

So why this different behaviour in JPA specs ?
 
You got style baby! More than this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!