disable caching in jpa.pdf
TRANSCRIPT
-
8/13/2019 Disable caching in JPA.pdf
1/4
Disable caching in JPA (eclipselink)
up vote
13down
vote
favorit
e
6
I want to use JPA (eclipselink) to get data from my database. The database is
changed by a number of other sources and I therefore want to go back to the
database for every find I execute. I have read a number of posts on disabling the
cache but this does not seem to be working. Any ideas?
I am trying to execute the following code:
EntityManagerFactory entityManagerFactory =
Persistence.createEntityManagerFactory("default");EntityManager em =
entityManagerFactory.createEntityManager();
MyLocation one =em.createNamedQuery("MyLocation.findMyLoc").getResultList().get(0);
MyLocation two =em.createNamedQuery("MyLocation.findMyLoc").getResultList().get(0);
System.out.println(one==two);
one==two is true while I want it to be false.
I have tried adding each/all the following to my persistence.xml
I have also tried adding the @Cache annotation to the Entity itself:
@Cache(type=CacheType.NONE, // Cache nothingexpiry=0,alwaysRefresh=true
)
Am I misunderstanding something?
Thank you,
James
javajpaentityeclipselink
http://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselinkhttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselinkhttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselinkhttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselinkhttp://stackoverflow.com/questions/tagged/javahttp://stackoverflow.com/questions/tagged/jpahttp://stackoverflow.com/questions/tagged/jpahttp://stackoverflow.com/questions/tagged/entityhttp://stackoverflow.com/questions/tagged/entityhttp://stackoverflow.com/questions/tagged/eclipselinkhttp://stackoverflow.com/questions/tagged/eclipselinkhttp://stackoverflow.com/questions/tagged/eclipselinkhttp://engine.adzerk.net/r?e=eyJhdiI6NDI3LCJhdCI6NCwiY20iOjE4Nzk2LCJjaCI6MTE3OCwiY3IiOjY4MDEwLCJkaSI6IjU1ZmQ4Y2I4ZGM4ZTRlMDJhOWZhYjY3NzU2YjczOTEwIiwiZG0iOjEsImZjIjo5NTU2MiwiZmwiOjQ3NTU2LCJrdyI6ImphdmEsanBhLGVudGl0eSxlY2xpcHNlbGluayIsIm1rIjoiamF2YSIsIm53IjoyMiwicnYiOjAsInByIjo3NjUsInN0Ijo4Mjc3LCJ6biI6NDMsInVyIjoiaHR0cDovL3d3dy5hc3Bvc2UuY29tL2phdmEvdG90YWwtY29tcG9uZW50LmFzcHg_dXRtX3NvdXJjZT1zdGFja292ZXJmbG93JnV0bV9tZWRpdW09YmFubmVyJnV0bV9jYW1wYWlnbj1zbyUyQmphdmElMkJ0b3AifQ&s=ZSm2Lf0RHHzM19qIU0RUgCsb_pghttp://stackoverflow.com/questions/tagged/eclipselinkhttp://stackoverflow.com/questions/tagged/entityhttp://stackoverflow.com/questions/tagged/jpahttp://stackoverflow.com/questions/tagged/javahttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselinkhttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselinkhttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink -
8/13/2019 Disable caching in JPA.pdf
2/4
share|improve this
question
edited May 11 '10 at
10:09
asked May 11 '10 at
8:59
James
93052241
Please change the title ("chaching" is not "caching") KartochMay 11 '10 at
9:03
James in your comment to my answer, was the caching off ( ) when you tested it?
JustinMay 11 '10 at 13:04
Sorry just noticed this, yes the caching was off. I am still having this issue and
am no closer to a solution. JamesMay 27 '10 at 11:39
4 Answers
activeoldestvotes
up vote
11
down
vote
accept
ed
This behavior is correct, otherwise if you change object one and object two with
different values you will have problems when persisting them. What is
happening is the call to load object two updates the entity loaded in the first
call. They must point to the same object since they ARE the same object. This
ensures that dirty data cannot be written.
If you call em.clear() between the two calls, entity one should become detached
your check will return false. There is however no need to do that, eclipse link is
infact updating your data to the latest which I would guess is what you want
since it frequently changes.
On a side note if you wish to update this data using JPA you will need to be
obtainingpessimistic lockson the Entity so that the underlying data cannot
change in the DB.
You will need to disable the query cache as well your cache options were just
removing the object cache from play not the query cache, that is why you arenot getting the new results:
In your code:
em.createNamedQuery("MyLocation.findMyLoc").setHint(QueryHints.CACHE_USAGE, CacheUsage.DoNotCheckCache).getResultList().get(0);
Or in persistence.xml:
share|improve thisanswer edited May 11 '10 at13:20 answered May 11 '10 at9:26
http://stackoverflow.com/q/2809275http://stackoverflow.com/posts/2809275/edithttp://stackoverflow.com/posts/2809275/edithttp://stackoverflow.com/posts/2809275/edithttp://stackoverflow.com/posts/2809275/edithttp://stackoverflow.com/posts/2809275/revisionshttp://stackoverflow.com/users/88003/jameshttp://stackoverflow.com/posts/2809275/revisionshttp://stackoverflow.com/users/88003/jameshttp://stackoverflow.com/users/88003/jameshttp://stackoverflow.com/users/88003/jameshttp://stackoverflow.com/users/199148/kartochhttp://stackoverflow.com/users/199148/kartochhttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2847764_2809275http://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2847764_2809275http://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2847764_2809275http://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2847764_2809275http://stackoverflow.com/users/321061/justinhttp://stackoverflow.com/users/321061/justinhttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2849383_2809275http://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2849383_2809275http://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2849383_2809275http://stackoverflow.com/users/88003/jameshttp://stackoverflow.com/users/88003/jameshttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2973045_2809275http://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2973045_2809275http://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2973045_2809275http://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink?answertab=active#tab-tophttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink?answertab=oldest#tab-tophttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink?answertab=oldest#tab-tophttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink?answertab=votes#tab-tophttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink?answertab=votes#tab-tophttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink?answertab=votes#tab-tophttp://en.wikibooks.org/wiki/Java_Persistence/Locking#JPA_2.0_Lockinghttp://en.wikibooks.org/wiki/Java_Persistence/Locking#JPA_2.0_Lockinghttp://en.wikibooks.org/wiki/Java_Persistence/Locking#JPA_2.0_Lockinghttp://stackoverflow.com/a/2809450http://stackoverflow.com/posts/2809450/edithttp://stackoverflow.com/posts/2809450/edithttp://stackoverflow.com/posts/2809450/edithttp://stackoverflow.com/posts/2809450/edithttp://stackoverflow.com/posts/2809450/revisionshttp://stackoverflow.com/posts/2809450/revisionshttp://stackoverflow.com/posts/2809450/revisionshttp://stackoverflow.com/users/88003/jameshttp://stackoverflow.com/posts/2809450/revisionshttp://stackoverflow.com/posts/2809450/revisionshttp://stackoverflow.com/posts/2809450/edithttp://stackoverflow.com/posts/2809450/edithttp://stackoverflow.com/a/2809450http://en.wikibooks.org/wiki/Java_Persistence/Locking#JPA_2.0_Lockinghttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink?answertab=votes#tab-tophttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink?answertab=oldest#tab-tophttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink?answertab=active#tab-tophttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2973045_2809275http://stackoverflow.com/users/88003/jameshttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2849383_2809275http://stackoverflow.com/users/321061/justinhttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2847764_2809275http://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2847764_2809275http://stackoverflow.com/users/199148/kartochhttp://stackoverflow.com/users/88003/jameshttp://stackoverflow.com/users/88003/jameshttp://stackoverflow.com/users/88003/jameshttp://stackoverflow.com/posts/2809275/revisionshttp://stackoverflow.com/posts/2809275/revisionshttp://stackoverflow.com/posts/2809275/edithttp://stackoverflow.com/posts/2809275/edithttp://stackoverflow.com/q/2809275 -
8/13/2019 Disable caching in JPA.pdf
3/4
Justin
1,534718
That helps thank you. I will have to look at pessimistic locks as the data will
be changing. How then if I add a Thread.sleep(10000) between the queries
and in that time manually change an attribute of MyLocation in the database
does two (and therefore one) not reflect this change? JamesMay 11 '10 at
11:42
added a link to some info about pessimistic locks JustinMay 11 '10 at 13:08
I have tried the described change in the code and persistence.xml and still
don't get the value I changed directly. Any ideas? JamesMay 12 '10 at 9:28
up
vote
3
dow
n
vote
See,
http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Cac
hing
For the same EntityManager JPA always requires that one==two, so this is
correct, not matter your caching options (this is the L1 cache, or transactionalcache, which enforces your transaction isolation and maintains object identity).
To force the query to refresh (and revert any changes you have made) you can use
the query hint "eclipselink.refresh"="true". Or probably better, use a new
EntityManager for each query/request, or call clear() on your EntityManager.
Is the correct way to disable the shared cache (L2 cache). Please remove all your
other settings as they are not correct, and can cause issues.
EclipseLink does not maintain a query cache by default, so those settings will
have no affect. CacheUsage is also not correct, do not use this (it is for in-memory
querying).
share|improve this answer
answered Dec 5 '12 at 14:12
James
13.7k1718
http://stackoverflow.com/users/321061/justinhttp://stackoverflow.com/users/321061/justinhttp://stackoverflow.com/users/321061/justinhttp://stackoverflow.com/users/88003/jameshttp://stackoverflow.com/users/88003/jameshttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2848720_2809450http://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2848720_2809450http://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2848720_2809450http://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2848720_2809450http://stackoverflow.com/users/321061/justinhttp://stackoverflow.com/users/321061/justinhttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2849423_2809450http://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2849423_2809450http://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2849423_2809450http://stackoverflow.com/users/88003/jameshttp://stackoverflow.com/users/88003/jameshttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2857005_2809450http://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2857005_2809450http://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2857005_2809450http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Cachinghttp://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Cachinghttp://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Cachinghttp://stackoverflow.com/a/13725132http://stackoverflow.com/posts/13725132/edithttp://stackoverflow.com/posts/13725132/edithttp://stackoverflow.com/users/416206/jameshttp://stackoverflow.com/users/416206/jameshttp://stackoverflow.com/users/416206/jameshttp://stackoverflow.com/users/416206/jameshttp://engine.adzerk.net/r?e=eyJhdiI6NDE0LCJhdCI6NCwiY20iOjg0NywiY2giOjExNzgsImNyIjoxMDc2OSwiZGkiOiIxNWMxZDkzNDJmZTM0MzU2YjU3NTVjNjJiNDM3NzBmNCIsImRtIjoxLCJmYyI6MTY4ODYsImZsIjoyNDQ0LCJrdyI6ImphdmEsanBhLGVudGl0eSxlY2xpcHNlbGluayIsIm53IjoyMiwicnYiOjAsInByIjoxNTY4LCJzdCI6ODI3Nywiem4iOjQ0LCJ1ciI6Imh0dHA6Ly9jYXJlZXJzLnN0YWNrb3ZlcmZsb3cuY29tL2pvYnMvdGVsZWNvbW11dGUifQ&s=dYGTjxIhG88VIwzNsJu0kgT4jFghttp://stackoverflow.com/users/321061/justinhttp://stackoverflow.com/users/416206/jameshttp://stackoverflow.com/users/416206/jameshttp://stackoverflow.com/users/416206/jameshttp://stackoverflow.com/posts/13725132/edithttp://stackoverflow.com/a/13725132http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Cachinghttp://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Cachinghttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2857005_2809450http://stackoverflow.com/users/88003/jameshttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2849423_2809450http://stackoverflow.com/users/321061/justinhttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2848720_2809450http://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment2848720_2809450http://stackoverflow.com/users/88003/jameshttp://stackoverflow.com/users/321061/justinhttp://stackoverflow.com/users/321061/justinhttp://stackoverflow.com/users/321061/justin -
8/13/2019 Disable caching in JPA.pdf
4/4
1
Note that "eclipselink.refresh"="true" is buggy in current versions of
EclipseLink (bugs.eclipse.org/bugs/show_bug.cgi?id=398074); also, it is
EclipseLink-specific and not part of JPA. So I'd advise to just get a fresh
EntityManager whenever you want fresh data - that's how it is supposed to
work with JPA.sleskeJan 17 at 0:51
up
vote
2
downvote
If manually modifying the attributes of the object, for example MyLocation. The
above trick (CACHE_USAGE=CacheUsage.DoNotCheckCache, or
eclipselink.query-results-cache=false) does not seem to work as I tried.
So i tried to set another hint which is "eclipselink.refresh", to "true". then it
works. I mean the manually changed attributes get retrieved.
So as i understand, the above trick only ensure the it gets the correct objects.
However, if the objects have been cached already, eclipselink just returns them
without checking the freshness of the contents of the objects. Only when the hint
"eclipselink.refresh" is set to "true", will these objects get refreshed to reflect thelatest attribute values.
share|improve this answer
answered Aug 11 '10 at 0:22
Robin
543
1
Hava you set this in your persistence.xml? If I add this hint to the query it
works. But it does not work if I define it only in my persistence.xml.
WaxolunistNov 29 '12 at 12:28
up vote 2
down vote
final Query readQuery = this.entityManager.createQuery(selectQuery);
readQuery.setParameter(paramA, valueA);
// Update the JPA session cache with objects that the query returns. Hence
the entity objects //in the returned collection always updated.
readQuery.setHint(QueryHints.REFRESH, HintValues.TRUE);
entityList = readQuery.getResultList();
This works for me.
share|improve this answer
https://bugs.eclipse.org/bugs/show_bug.cgi?id=398074https://bugs.eclipse.org/bugs/show_bug.cgi?id=398074https://bugs.eclipse.org/bugs/show_bug.cgi?id=398074http://stackoverflow.com/users/43681/sleskehttp://stackoverflow.com/users/43681/sleskehttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment19984476_13725132http://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment19984476_13725132http://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment19984476_13725132http://stackoverflow.com/a/3454465http://stackoverflow.com/posts/3454465/edithttp://stackoverflow.com/posts/3454465/edithttp://stackoverflow.com/users/416740/robinhttp://stackoverflow.com/users/416740/robinhttp://stackoverflow.com/users/416740/robinhttp://stackoverflow.com/users/534858/waxolunisthttp://stackoverflow.com/users/534858/waxolunisthttp://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment18686766_3454465http://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment18686766_3454465http://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment18686766_3454465http://stackoverflow.com/a/13709765http://stackoverflow.com/posts/13709765/edithttp://stackoverflow.com/posts/13709765/edithttp://stackoverflow.com/posts/13709765/edithttp://stackoverflow.com/users/416740/robinhttp://stackoverflow.com/posts/13709765/edithttp://stackoverflow.com/a/13709765http://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment18686766_3454465http://stackoverflow.com/users/534858/waxolunisthttp://stackoverflow.com/users/416740/robinhttp://stackoverflow.com/users/416740/robinhttp://stackoverflow.com/users/416740/robinhttp://stackoverflow.com/posts/3454465/edithttp://stackoverflow.com/a/3454465http://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink#comment19984476_13725132http://stackoverflow.com/users/43681/sleskehttps://bugs.eclipse.org/bugs/show_bug.cgi?id=398074