Tuesday, March 4, 2008

QueryHint and TopLink Essentials

Since I am using TopLink Essentials (I tend to try and keep this updated - currently using version 2.1 build 22), I was finding a few query issues where I was getting some stale objects for certain queries from the cache. This seems possible, since I am performing a lot of queries in background tasks (executed as threads) using ThreadLocal entity managers, however all the entity managers should be using a shared EntityManagerFactory. I decided to send Query Hints with the query to force a refresh on called queries. Since I have provider information stored in the ServerFactory class, I added a new method createQuery( EntityManager em, String query ) to not only create the query, but also set the QueryHint and FlushMode. It turned out, I was setting the FlushMode on almost every query as it was, so this at least gives me some more control over this. The EntityManager is used to create the query, and also for getting the persistence provider. To force a refresh, I am using: query.setHint("toplink.refresh", "true"); when the provider is TopLink Essentials. Currently, for other providers I am not doing anything special. A great blog on this is posted by Wonseok Kim on Java.net: http://weblogs.java.net/blog/guruwons/archive/2006/09/understanding_t.html

1 comment:

Rodrigo said...

Great tip sir. I'll try this out.
Thanks