2

I am using DN3 and GAE 1.7.4. I use JPA2 which according to the documentations by default has Level2 cache enabled.

Here is my question:

If I run a query that returns some objects, would these objects be put in the cache automatically by their ID?

If I run em.find() with an id of an object which has already been loaded with another query createQuery().getResultList() would it be available in the cache?

Do I need to run my em.find() or query in a transaction in order for the cache to kick in?

I need some clarification on how this cache works and how I could do my queries/finds/persists in order to make the best use of the cache.

Thanks

danial
  • 4,058
  • 2
  • 32
  • 39

2 Answers2

3

From Google App Engine: Using JPA with App Engine

Level2 Caching is enabled by default. To get the previous default behavior, set the persistence property datanucleus.cache.level2.type to none. (Alternatively include the datanucleus-cache plugin in the classpath, and set the persistence property datanucleus.cache.level2.type to javax.cache to use Memcache for L2 caching.

As for your doubts, this depends on your query as well as DataNucleus and GAE Datastore adapter implementation specifics. As Carol McDonald suggested I believe that the best path to find the answers for your questions is with JPA2 Cache interface... More specifically the contains method.

Run your query, get access to the Cache interface through the EntityManagerFactory and see if the Level 2 cache contains the desired entity.

Enabling DataNucleus logs will also give you good hints about what is happening behind the scenes.

Community
  • 1
  • 1
Anthony Accioly
  • 21,918
  • 9
  • 70
  • 118
3

After debugging in development local GAE mode I figured level 2 cache works. No need for transaction begin/commit. The result of my simple query on primary keys as well as em.find() would be put in the cache by their primary keys.

However the default cache timeout in local development server is like a few seconds, I had to add this:

    <property name="datanucleus.cache.level2.timeout" value="3600000" />

to persistence.xml.

danial
  • 4,058
  • 2
  • 32
  • 39