Query result caching
mreasy Jun 14, 2012 7:54 AMHi,
I am looking for help or hints regarding the usage of the hibernate query cache in Jboss 7.1.1.
I have entities, for which I activated the 2nd level cache using
<property name="hibernate.cache.use_second_level_cache" value="true"/>
in the persistence.xml. This works - entities themselves getcached.
However when a named query is called, this always goes through to the database, where it creates enormous "row lock contentions" and therefore is poison for throughput.
Therefore, I configured query cache as described in http://docs.jboss.org/jbossas/docs/Clustering_Guide/5/html/ch04s02s03.html and http://docs.jboss.org/hibernate/orm/4.0/manual/en-US/html/performance.html#performance-querycache with the result of the cache being created, but not being used (neither populated, nor hit) by such queries.
This is one of the NamedQueries, which shows the behavior - you can see the hibernate.cacheable hint on the query.
@Entity(name = "EMyEntity") @NamedQueries({ @NamedQuery(name = "EMyEntity.findByPPO", query = "SELECT OBJECT(g) FROM EMyEntity g WHERE g.pk.partnerLink=:partner AND g.pk.portType=:portType AND g.pk.operation=:operation AND g.pk.env=:env ORDER BY g.createdTime DESC", hints = {@QueryHint(name = "parameters", value = "java.lang.String partner, java.lang.String portType, java.lang.String operation, java.lang.String env"), @QueryHint(name="org.hibernate.cacheable", value="true")})}) @Table(name = "tMyTableName") @Cacheable public class EMyEntity
Results always in the DB query (mention the "for update". cVersionJPA is the @Version column) on Oracle:
select cEnv from tMyTableName where cEnv =:1 and cOperation =:2 and cPartnerLink =:3 and cPortType =:4 and cProcessQName =:5 and cVersionJPA =:6 for update
The same happens without a named query, when doing a find-by-primary-key:
public EMyOtherEntity findById(java.lang.String id) throws ObjectNotFoundException { EMyOtherEntity result = em.find(EMyOtherEntity.class, id); // ... em.lock(result, LockModeType.PESSIMISTIC_READ); return result; }
which is even more confusing for me.
Am I missing sth. or any hints where to also look-at?
Best Regards,
Rico