Question

The JPA 2.0 specification (JSR 317) says:

The persistence context is not synchronized with the result of the bulk update or delete.

Exactly what is a bulk update?

In my view, there is [or rather should be] a difference between a "bulk update" and a "regular update". I would like to believe that a bulk update is one that update multiple entities. Thus there should be an update not categorized as "bulk", an update operation that only targets one single entity. And in this context, whether that operation updates the first-level cache or not, is quite important.

The specification never address this issue or give a clear cut definition of the expression "bulk update". Nor can I find any other source on the topic. I tried my best to test an application managed resource-local entity manager (with EclipseLink 2.3.2.v20111125-r10461) and can confirm that the persistence context is indeed out of date even if we execute an update query that only target one single entity. Nor did the JPA provider invoke my entity listener I had attached to the test class.

I assumed after reading the JPA 2.0 specification that this would be the case only for "bulk updates", that is, update queries that target multiple entities with affected rows being larger than 1. But with the previously described test results in mind I am beginning to think that there is no such thing as a "regular update" and all update queries is "bulk updates".

Moral of the story, if that is true, is to only thrust the contents of the caches when we do EntityManager#find, EntityManager#merge, EntityManager#remove and JPQL select queries (Query#getResultList and Query#getSingleResult). But we should not thrust the caches if we have executed an JPQL update or delete query (Query#executeUpdate). It should be added for the benefit of Java EE newcomers that other application processes and direct database calls (JDBC) can invalidate the caches too. Whether this be true or not, there's another thing I cannot understand:

What is the rationale behind this design, why shouldn't the JPA provider synchronize the persistence context and/or his second-level cache? My first guess would be performance reasons, but then again, I have no idea.

If you know anything, give it to me!!

Was it helpful?

Solution

"BULK UPDATE" is clearly an update using a Query such as "UPDATE MyEntity SET field1 = val1 WHERE field2 < 345" (just as BULK DELETE is a DELETE query). A normal update is where you update a field of a managed entity directly, using a setter.

Rationale : well the whole point of bulk update is to not have to load objects into memory, or work out if any of the affected objects are already managed. You'll likely find that most JPA implementations will evict possibly affected objects out of the L2 cache on performing a BULK UPDATE.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top