Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] caching query results for all queries...?

You have to set the hint in the definition of the named query (in annotations
or xml).  Setting the hint at runtime is changing the named query, so is a
new query (you cannot change a shared query).

Setting a query cache on a dynamic query has no effect, unless you
re-execute the same query instance in the same entity manager. 

You can clear the query cache using the JPA Cache API,

((JpaCache)emf.getCache()).clearQueryCache()



Tim Martin-11 wrote:
> 
> mmm, but I think that only refreshes the results for the returned 
> objects  ?  Is that a correct assumption ?
> 
> it wouldnt remove all results for all param values from the cache for 
> that named query - meaning that the caller thats creating the named 
> query would need to know to force a refresh - but it doesnt know as its 
> not responsible for doing the updates...
> 
> our current scenario is :
> static data updated generically in one ejb
> 
> when real business processing logic needs to get a piece of static data 
> it's read by entity specific dao's
> 
> when a static data entity is updated I want to clear the caches (I'm 
> happy to completely clear all caches at this point as it's very
> infrequent).
> so I was hoping to call a short helper function that would do that - 
> initially by calling something like this here which would invalidate 
> everything
> 
> genericUpdateDao.clearQueryCaches(){
>      List<String>  qNames = Helper.getCachedQueryNames();
>      for each qName {
>          Query q = em.createQuery(qName)
>          q.query.setHint("eclipselink.query-results-cache.size",1)
>          q.getResultList();
> }
> 
> 
> 
> On 12/03/2012 15:38, Tom Ware wrote:
>> I suggest you use the refresh query hint.  That should refresh the cache.
>>
>> On 12/03/2012 11:09 AM, Tim Martin wrote:
>>> Is there anyway to force a cache clear - I can see that
>>>
>>> query.setHint(""eclipselink.query-results-cache.size",1)
>>>
>>> and then calling the query with parameters that would never return a 
>>> result
>>> would clear the cache, BUT is there a better way ?
>>>
>>>
>>> On 12/03/2012 13:12, Tom Ware wrote:
>>>> The query cache will not know to refresh when a different persistence
>>>> operation changes one of the results held in the cache.
>>>>
>>>> -Tom
>>>>
>>>> On 12/03/2012 4:58 AM, Tim Martin wrote:
>>>>> Tom - one further question re queryResults caching -
>>>>>
>>>>> if I run queryA which returns object X,Y,Z which are cached
>>>>> and then and update one of those objects, does the query results 
>>>>> cache know or
>>>>> get told enough to clear the results for queryA so re running it 
>>>>> will go to the
>>>>> database instead of returning a stale result ?
>>>>>
>>>>> thanks
>>>>>
>>>>> http://wiki.eclipse.org/Introduction_to_EclipseLink_Queries_%28ELUG%29#How_to_Cache_Query_Results_in_the_Query_CacheHow 
>>>>>
>>>>>
>>>>> to Cache Query Results in the Query Cache
>>>>>
>>>>> In addition to EclipseLink's object cache, EclipseLink also 
>>>>> supports a query
>>>>> cache. There is the following distinction between the two:
>>>>>
>>>>>   * The /object cache/ indexes objects by their primary key, 
>>>>> allowing primary
>>>>>     key queries to obtain cache hits. By using the object cache, 
>>>>> queries that
>>>>>     access the data source can avoid the cost of building the 
>>>>> objects and their
>>>>>     relationships if the object is already present.
>>>>>   * The /query cache/ is distinct from the object cache. The query 
>>>>> cache is
>>>>>     indexed by the query and the query parameters–not the object's 
>>>>> primary key.
>>>>>     This allows for any query executed with the same parameters to 
>>>>> obtain a
>>>>>     query cache hit and return the same result set.
>>>>>
>>>>>
>>>>>
>>>>> On 09/03/2012 14:14, Tom Ware wrote:
>>>>>> The hint can be added to all queries. Obviously it will improve 
>>>>>> performance
>>>>>> more for queries that are executed more often. (named queries are 
>>>>>> a good
>>>>>> candidate here)
>>>>>>
>>>>>> Queries are actually cloned before we apply query hints to them, 
>>>>>> so the
>>>>>> SessionCustomizer option listed below may be what you want for 
>>>>>> certain
>>>>>> queries. (since it will affect the query every time it is looked 
>>>>>> up rather
>>>>>> than just the instance you get from em.createQuery()).
>>>>>>
>>>>>>
>>>>>> BTW: Some doc:
>>>>>>
>>>>>> http://wiki.eclipse.org/Introduction_to_EclipseLink_Queries_(ELUG)#How_to_Cache_Query_Results_in_the_Query_Cache 
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> -Tom
>>>>>>
>>>>>> On 09/03/2012 8:49 AM, Tim Martin wrote:
>>>>>>> Just to check - the hint can only be added to named ? not those 
>>>>>>> built on
>>>>>>> the fly
>>>>>>> (which of course we are doing are best to avoid :)
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> On 09/03/2012 13:45, Tom Ware wrote:
>>>>>>>> There is no setting to do this universally for a whole 
>>>>>>>> persistence unit. It
>>>>>>>> should be possible to do this using EclipseLink-native API in a
>>>>>>>> SessionCustomizer by iterating through the queries defined in 
>>>>>>>> the query
>>>>>>>> manager and calling the required native API to set it up. The 
>>>>>>>> timeouts could
>>>>>>>> be set in the same code.
>>>>>>>>
>>>>>>>> The reason there is no universal setting is that it is not clear 
>>>>>>>> that this
>>>>>>>> would be desirable in most use-cases. EclipseLink already has a 
>>>>>>>> cache that
>>>>>>>> provides quite a bit of performance boost and the query results 
>>>>>>>> cache is
>>>>>>>> intended to provide an additional performance boost for selected 
>>>>>>>> queries.
>>>>>>>> Using the query cache requires an understanding about the degree 
>>>>>>>> to which
>>>>>>>> results may be stale when they come back.
>>>>>>>>
>>>>>>>> -Tom
>>>>>>>>
>>>>>>>> On 09/03/2012 8:16 AM, Tim Martin wrote:
>>>>>>>>> Hi - anyone know if there is a valid property to use in the
>>>>>>>>> persistence.xml to
>>>>>>>>> cache all query results by name and parameter values.
>>>>>>>>> So rather than putting a hint on the queries we can do 
>>>>>>>>> something like
>>>>>>>>>
>>>>>>>>> <property name="eclipselink.query-results-cache" value="true"/>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> I've come across it searching in google - but not come across 
>>>>>>>>> anything
>>>>>>>>> defintive...
>>>>>>>>>
>>>>>>>>> If so - how would we configure expiration of the cache ? 
>>>>>>>>> something simple
>>>>>>>>> like
>>>>>>>>> expire everything every x minutes would do to start with..
>>>>>>>>>
>>>>>>>>> Thanks
>>>>>>>>> Tim
>>>>>>>>>
>>>>>>>>>
> 
> 


-----
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.eclipse.org/forums/index.php?t=thread&frm_id=111&S=1b00bfd151289b297688823a00683aca
EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
Blog:  http://java-persistence-performance.blogspot.com/ Java Persistence
Performance 
-- 
View this message in context: http://old.nabble.com/caching-query-results-for-all-queries...--tp33471720p33544625.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top