You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by xasima <xa...@gmail.com> on 2010/10/04 13:38:22 UTC

cache queries and data from native queries

hi. I have performed the quering of db2 from Rest service (servlet) using
native queries (openjpa-2.0.1). I need to cache data (entities) using
different eviction algorithms for most of them. I have three questions. 

1) are native queries supposed to be cached (if appropriate configuration in
persistance.xml are done, see code below)
2) are data returned from the native queries supposed to be cached?
3) how can I access the eviction algorithm stored in the EvictionSchedule
properties? I want to introduce caching on another layer (http expiration/
etags) based on the same eviction configuration. Namely, if JPA eviction
schedule is supposed that data needs to be expired only on Monday, please
set up the same http expiration. 

Thanks


code:
Query
	public List<Year> getCycles() {
                 Query query = em.createNamedQuery(...) 
                 return query.getResultList();
        }

Entity
@Entity
@DataCache(name="cycle-cache")
public class Cycle{}


persistence.xml
    		<property name="openjpa.QueryCache" value="true"/>
    		<property name="openjpa.RemoteCommitProvider" value="sjvm"/>
    		<property name="openjpa.DataCache" value="true(Name=cycle-cache,
CacheSize=100, EvictionSchedule='...some eviction rule is here... like 15,45
15 * * 1 ')"/>


-- 
View this message in context: http://openjpa.208410.n2.nabble.com/cache-queries-and-data-from-native-queries-tp5598754p5598754.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: cache queries and data from native queries

Posted by Rick Curtis <cu...@gmail.com>.
Great questions! I'll try my best at giving correct and truthful answers.
I'll start out by giving a little background information.

In simple terms the L2 cache (openjpa.DataCache) is a simple mapping of
Entity identifier(id) to Entity data. This cache on it's own is useful if
you are doing lots of EntityManager.find(EntityClass.class, id) operations.

The L2 cache becomes more interesting when the QueryCache
(openjpa.QueryCache) is also enabled. The QueryCache is essentially a
mapping of JPQL to Entity identifiers. Lets say you execute the JPQL "SELECT
p from Person p" and there are 10 rows in your Person table. As a result of
executing this query, the application gets 10 Person Entities, those 10
Entities are also added to the L2 cache, and an entry gets added to the
QueryCache mapping that query to all 10 Entities. All subsequent executions
of "SELECT p from Person p" will return data from L2 cache as long as all
known identifiers are found.

Now on to your questions....

> 1) are native queries supposed to be cached
Sort of. Assuming the L2 and QueryCache is enabled, the data from native
selects will be cached as long as the return type from the query is an
Entity type(ie: Cycle). Unfortunately the QueryCache doesn't cache native
queries. So if you were to execute "SELECT * FROM Cycle WHERE 1=1", all
Cycle data will be put in the L2 cache, but the Query won't be cached. If
you were to call EntityManager.find(Cycle.class, [id]) we would get the data
from the L2 cache, not the database.

Another note, be careful about issuing updates via native(or straight JPQL
for that matter) queries as the L2 cache will not reflect those changes.

> 3) how can I access the eviction algorithm stored in the EvictionSchedule
properties?
The EvictionSchedule property is a way to blindly purge the cache of all
Entities on a given interval. If you want to perform eviction on some other
criteria, I would access the cache directly and evict when your application
deems it necessary. Here is a snippet of code to access the cache:

    OpenJPAEntityManagerFactory oemf =
org.apache.openjpa.persistence.OpenJPAPersistence.cast(EntityManagerFactory);
    StoreCache cache = oemf.getStoreCache();

One last comment. I see that you are using a named cache in your code.. I'd
be careful using that feature as I'm not sure if it is fully functional. I'd
suggest omitting (name="cycle-cache") from your @DataCache annotation.

Thanks,
RIck

On Mon, Oct 4, 2010 at 6:38 AM, xasima <xa...@gmail.com> wrote:

>
> hi. I have performed the quering of db2 from Rest service (servlet) using
> native queries (openjpa-2.0.1). I need to cache data (entities) using
> different eviction algorithms for most of them. I have three questions.
>
> 1) are native queries supposed to be cached (if appropriate configuration
> in
> persistance.xml are done, see code below)
> 2) are data returned from the native queries supposed to be cached?
> 3) how can I access the eviction algorithm stored in the EvictionSchedule
> properties? I want to introduce caching on another layer (http expiration/
> etags) based on the same eviction configuration. Namely, if JPA eviction
> schedule is supposed that data needs to be expired only on Monday, please
> set up the same http expiration.
>
> Thanks
>
>
> code:
> Query
>        public List<Year> getCycles() {
>                 Query query = em.createNamedQuery(...)
>                 return query.getResultList();
>        }
>
> Entity
> @Entity
> @DataCache(name="cycle-cache")
> public class Cycle{}
>
>
> persistence.xml
>                <property name="openjpa.QueryCache" value="true"/>
>                <property name="openjpa.RemoteCommitProvider" value="sjvm"/>
>                <property name="openjpa.DataCache"
> value="true(Name=cycle-cache,
> CacheSize=100, EvictionSchedule='...some eviction rule is here... like
> 15,45
> 15 * * 1 ')"/>
>
>
>