Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

java - Disable caching in JPA (eclipselink)

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

<property name="eclipselink.cache.shared.default" value="false"/>
<property name="eclipselink.cache.size.default" value="0"/>
<property name="eclipselink.cache.type.default" value="None"/>

I have also tried adding the @Cache annotation to the Entity itself:

@Cache(
  type=CacheType.NONE, // Cache nothing
  expiry=0,
  alwaysRefresh=true
)

Am I misunderstanding something?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 obtaining pessimistic locks on 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 are not getting the new results:

In your code:

em.createNamedQuery("MyLocation.findMyLoc").setHint(QueryHints.CACHE_USAGE, CacheUsage.DoNotCheckCache).getResultList().get(0);

Or in persistence.xml:

<property name="eclipselink.query-results-cache" value="false"/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...