Merge pull request #90 from lukasz-antoniak/HHH-5025

HHH-5025 - Support caching audit queries using ehcache's DiskStore
This commit is contained in:
Adam Warski 2011-06-01 22:37:50 -07:00
commit 0a55292b7c
4 changed files with 86 additions and 1 deletions

View File

@ -14,6 +14,9 @@ dependencies {
testCompile( libraries.junit ) testCompile( libraries.junit )
testCompile( project(':hibernate-testing') ) testCompile( project(':hibernate-testing') )
testCompile( libraries.jpa ) testCompile( libraries.jpa )
testCompile( project(':hibernate-ehcache') ) {
exclude group: 'org.slf4j'
}
testRuntime( libraries.h2 ) testRuntime( libraries.h2 )
testRuntime( libraries.javassist ) testRuntime( libraries.javassist )
} }

View File

@ -38,7 +38,9 @@ import org.hibernate.usertype.UserType;
* A hibernate type for the {@link RevisionType} enum. * A hibernate type for the {@link RevisionType} enum.
* @author Adam Warski (adam at warski dot org) * @author Adam Warski (adam at warski dot org)
*/ */
public class RevisionTypeType implements UserType { public class RevisionTypeType implements UserType, Serializable {
private static final long serialVersionUID = -1053201518229282688L;
private static final int[] SQL_TYPES = { Types.TINYINT }; private static final int[] SQL_TYPES = { Types.TINYINT };
public int[] sqlTypes() { public int[] sqlTypes() {

View File

@ -0,0 +1,66 @@
package org.hibernate.envers.test.integration.cache;
import org.hibernate.MappingException;
import org.hibernate.cache.internal.EhCacheProvider;
import org.hibernate.cfg.Environment;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.envers.RevisionType;
import org.hibernate.envers.query.criteria.RevisionTypeAuditExpression;
import org.hibernate.envers.test.AbstractSessionTest;
import org.hibernate.envers.test.Priority;
import org.hibernate.envers.test.entities.StrTestEntity;
import org.hibernate.testing.TestForIssue;
import org.junit.Assert;
import org.junit.Test;
import java.net.URISyntaxException;
/**
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
public class HibernateSecLvlQueryCache extends AbstractSessionTest {
private static final String QUERY_CACHE_REGION = "queryCacheRegion";
@Override
protected void initMappings() throws MappingException, URISyntaxException {
config.addAnnotatedClass(StrTestEntity.class);
config.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true");
config.setProperty(Environment.USE_QUERY_CACHE, "true");
config.setProperty(Environment.CACHE_PROVIDER, EhCacheProvider.class.getName());
config.setProperty(Environment.CACHE_PROVIDER_CONFIG, "ehcache-test.xml");
}
@Test
@Priority(10)
public void initData() {
// Revision 1
getSession().getTransaction().begin();
StrTestEntity ste = new StrTestEntity("data");
getSession().persist(ste);
getSession().getTransaction().commit();
// Evicting old query cache.
getSession().getSessionFactory().getCache().evictQueryRegion(QUERY_CACHE_REGION);
}
@Test
@TestForIssue(jiraKey="HHH-5025")
public void testSecLvlCacheWithRevisionTypeDiskPersistent() throws InterruptedException {
// Cached query that requires serializing RevisionType variable when persisting to disk.
getAuditReader().createQuery().forEntitiesAtRevision(StrTestEntity.class, 1)
.add(new RevisionTypeAuditExpression(RevisionType.ADD, "="))
.setCacheable(true).setCacheRegion(QUERY_CACHE_REGION).getResultList();
// Waiting for cached data to persist to disk.
Thread.sleep(1000);
Assert.assertTrue(getQueryCacheSize() > 0);
}
private int getQueryCacheSize() {
// Statistics are not notified about persisting cached data failure. However, cache entry gets evicted.
// See DiskWriteTask.call() method (net.sf.ehcache.store.compound.factories.DiskStorageFactory).
SessionFactoryImplementor sessionFactoryImplementor = (SessionFactoryImplementor) getSession().getSessionFactory();
return sessionFactoryImplementor.getQueryCache(QUERY_CACHE_REGION).getRegion().toMap().size();
}
}

View File

@ -0,0 +1,14 @@
<ehcache>
<diskStore path="java.io.tmpdir" />
<!-- Fail safe. -->
<defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120"
overflowToDisk="true" maxElementsOnDisk="10000000" diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />
<!-- Mandatory persist cached objects to disk (checks whether they are serializable).
This cache region is used by HibernateSecLvlQueryCache test case. -->
<cache name="queryCacheRegion" eternal="false" diskPersistent="true"
maxElementsInMemory="1" maxElementsOnDisk="10000" overflowToDisk="true"
timeToIdleSeconds="3600" timeToLiveSeconds="3600" />
</ehcache>