-
1. Re: The hiberate cache for entity or query does not work for a while once the record is changed by another wildfly instance in the cluster
pferraro Jul 25, 2017 4:11 PM (in response to wwang2016)Can you post your JPA persistence.xml or, if using hibernate directly, your hibernate.cfg.xml or hibernate.properties?
-
2. Re: The hiberate cache for entity or query does not work for a while once the record is changed by another wildfly instance in the cluster
wwang2016 Jul 25, 2017 4:23 PM (in response to pferraro)<?xml version="1.0" encoding="UTF-8"?>
<!--
JBoss, Home of Professional Open Source
Copyright 2015, Red Hat, Inc. and/or its affiliates, and individual
contributors by the @authors tag. See the copyright.txt in the
distribution for a full listing of individual contributors.Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="primary">
<jta-data-source>java:jboss/datasources/KitchensinkEarQuickstartDS</jta-data-source>
<mapping-file>META-INF/queries.xml</mapping-file>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties><property name="jboss.entity.manager.factory.jndi.name" value="java:jboss/MessagingEntityManagerFactory" />
<property name="jboss.entity.manager.jndi.name" value="java:/MessagingEntityManager" /><property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value= "true"/><property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.cache.use_minimal_puts" value="true" />
<property name="hibernate.cache.infinispan.statistics" value="true" /></properties>
</persistence-unit>
</persistence> -
3. Re: The hiberate cache for entity or query does not work for a while once the record is changed by another wildfly instance in the cluster
wwang2016 Jul 25, 2017 4:24 PM (in response to wwang2016)The queries.xml
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence/orm http://xmlns.jcp.org/xml/ns/persistence/orm_2_1.xsd"
version="2.1">
<!-- THIS FILE SHOULD ONLY CONTAIN EJBQL QUERIES. FOR NATIVE QUERIES,
UPDATE ALL NECCESSARY QUERIES-NATIVE.XML FILES -->
<named-query name="Booking.getAllBookings">
<query>
SELECT b from Booking b order by b.checkinDate
</query>
<hint name="org.hibernate.cacheable" value="true"/>
<hint name="org.hibernate.cacheRegion" value="query.booking"/>
</named-query>
<named-query name="Booking.getBookingById">
<query>
SELECT b from Booking b WHERE b.id = :bookingId
</query>
<hint name="org.hibernate.cacheable" value="true"/>
<hint name="org.hibernate.cacheRegion" value="query.booking"/>
</named-query>
<named-query name="Hotel.queryForHotels">
<query>
SELECT h from Hotel h
WHERE lower(h.name) like :pattern
or lower(h.city) like :pattern
or lower(h.zip) like :pattern
or lower(h.address) like :pattern
</query>
<hint name="org.hibernate.cacheable" value="true"/>
<hint name="org.hibernate.cacheRegion" value="query.hotel"/>
</named-query>
<named-query name="Booking.getAllBookingsByUserName">
<query>
SELECT b from Booking b WHERE b.user.username = :userName order by b.checkinDate
</query>
<hint name="org.hibernate.cacheable" value="true"/>
<hint name="org.hibernate.cacheRegion" value="query.booking"/>
</named-query>
</entity-mappings> -
4. Re: The hiberate cache for entity or query does not work for a while once the record is changed by another wildfly instance in the cluster
pferraro Jul 25, 2017 4:42 PM (in response to wwang2016)Can you also post your "hibernate" cache-container configuration from the infinispan subsystem?
-
5. Re: The hiberate cache for entity or query does not work for a while once the record is changed by another wildfly instance in the cluster
wwang2016 Jul 26, 2017 8:22 AM (in response to pferraro)<subsystem xmlns="urn:jboss:domain:infinispan:4.0">
<cache-container name="server" aliases="singleton cluster" default-cache="default" module="org.wildfly.clustering.server">
<transport lock-timeout="60000"/>
<replicated-cache name="default" mode="SYNC">
<transaction mode="BATCH"/>
</replicated-cache>
</cache-container>
<cache-container name="web" default-cache="dist" module="org.wildfly.clustering.web.infinispan">
<transport lock-timeout="60000"/>
<distributed-cache name="dist" mode="ASYNC" l1-lifespan="0" owners="2">
<locking isolation="REPEATABLE_READ"/>
<transaction mode="BATCH"/>
<file-store/>
</distributed-cache>
<distributed-cache name="concurrent" mode="SYNC" l1-lifespan="0" owners="2">
<file-store/>
</distributed-cache>
</cache-container>
<cache-container name="ejb" aliases="sfsb" default-cache="dist" module="org.wildfly.clustering.ejb.infinispan">
<transport lock-timeout="60000"/>
<distributed-cache name="dist" mode="ASYNC" l1-lifespan="0" owners="2">
<locking isolation="REPEATABLE_READ"/>
<transaction mode="BATCH"/>
<file-store/>
</distributed-cache>
</cache-container>
<cache-container name="hibernate" default-cache="local-query" module="org.hibernate.infinispan" statistics-enabled="true">
<transport lock-timeout="60000"/>
<local-cache name="local-query" statistics-enabled="true">
<eviction strategy="LRU" max-entries="10000"/>
<expiration max-idle="100000"/>
</local-cache>
<invalidation-cache name="entity" mode="SYNC" statistics-enabled="true">
<transaction mode="NON_XA"/>
<eviction strategy="LRU" max-entries="10000"/>
<expiration max-idle="100000"/>
</invalidation-cache>
<replicated-cache name="timestamps" mode="ASYNC" statistics-enabled="true"/>
</cache-container>
</subsystem>
-
6. Re: The hiberate cache for entity or query does not work for a while once the record is changed by another wildfly instance in the cluster
pferraro Jul 26, 2017 11:29 AM (in response to wwang2016)Everything looks in order - and the observed behavior is as I would expect. I think the confusion comes from the interaction between the query and entity caches. When your code runs a query, it returns the ids from which to fetch from the entity cache. If the entity cache does not contain an entry for the specific id (e.g. because it was previously invalidated), it has to run an additional select statement to retrieve it.
BTW - it seems you have a Booking.getBookingById query which is probably redundant with a normal Booking entity get.
-
7. Re: The hiberate cache for entity or query does not work for a while once the record is changed by another wildfly instance in the cluster
wwang2016 Jul 26, 2017 12:04 PM (in response to pferraro)Hi Paul,
The problem is that it does not just run the query one time to eventually populate the cache (after a period of time). It ran the query every time I visited the object until a time period later.
This behavior would reverse if I update the object in wildfly instance#2, and visiting the object in instance#2 will not print out the query, but it will print out the query every time when I visit the object in instance#1 until a period of time later.
It looks like the wildfly instance receiving a notification about the modification of an object did not build the cache until a later time. However, the wildfly instance sending notification about the modification of an object does not have the problem.
BTW, the Booking.getBookingById was deliberately written to test query cache, and not entity cache for booking. I also had an example of hotel entity cache that was query by EntityManager.find(id, Hotel.class), and I got the same
issue (delayed caching of data)
Is there any configuration setting to resolve this issue?