Skip to content

Commit

Permalink
Fix: Property "hibernate.cache.region_prefix" is not applied
Browse files Browse the repository at this point in the history
  • Loading branch information
ghillert committed Mar 18, 2024
1 parent be878c6 commit 6f0ae40
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.hibernate.cache.spi.access.AccessType;
import org.hibernate.cache.spi.support.DomainDataStorageAccess;
import org.hibernate.cache.spi.support.RegionFactoryTemplate;
import org.hibernate.cache.spi.support.RegionNameQualifier;
import org.hibernate.cache.spi.support.StorageAccess;
import org.hibernate.cfg.Environment;
import org.hibernate.engine.spi.SessionFactoryImplementor;
Expand Down Expand Up @@ -272,7 +273,7 @@ public long nextTimestamp() {
* @return a NamedCache for the argument name
*/
protected NamedCache<?, ?> ensureNamedCache(String cacheName) {
return this.coherenceSession.getCache(cacheName);
return this.coherenceSession.getCache(RegionNameQualifier.INSTANCE.qualify(cacheName, getOptions()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
package com.oracle.coherence.hibernate.cache.v53;

import com.oracle.coherence.hibernate.cache.v53.access.CoherenceDomainDataRegionImpl;
import com.oracle.coherence.hibernate.cache.v53.access.CoherenceStorageAccessImpl;
import com.oracle.coherence.hibernate.cache.v53.support.Foo;
import com.tangosol.net.CacheFactory;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.stat.CacheRegionStatistics;
import org.hibernate.stat.Statistics;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.AfterClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Gunnar Hillert
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class CacheRegionPrefixTests extends BaseCoreFunctionalTestCase {

private Long idOfSavedItem = null;

@AfterClass
public static void after() {
CacheFactory.shutdown();
}

@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {Foo.class};
}

@Override
protected void configure(Configuration cfg) {
super.configure(cfg);
cfg.setProperty(Environment.CACHE_REGION_PREFIX, "foobar");
cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true");
cfg.setProperty(Environment.USE_QUERY_CACHE, "true");
cfg.setProperty(Environment.CACHE_REGION_FACTORY, CoherenceRegionFactory.class.getName());
cfg.setProperty("com.oracle.coherence.hibernate.cache.cache_config_file_path", "tests-hibernate-second-level-cache-config.xml");
}

@Test
public void test_01_addItem() {
final Statistics statistics = this.sessionFactory().getStatistics();

final CoherenceDomainDataRegionImpl region = (CoherenceDomainDataRegionImpl) this.sessionFactory().getCache().getRegion("foo");
final CoherenceStorageAccessImpl coherenceStorageAccess = (CoherenceStorageAccessImpl) region.getCacheStorageAccess();

assertThat(coherenceStorageAccess.getDelegate().getElementCountInMemory()).isEqualTo(0);

final Session session = openSession();
session.beginTransaction();
final Foo itemToSave = new Foo("bar");
this.idOfSavedItem = (Long) session.save(itemToSave);
session.flush();
session.getTransaction().commit();

final CacheRegionStatistics itemStatistics = statistics.getDomainDataRegionStatistics("foo");

assertThat(itemStatistics.getPutCount()).isEqualTo(1);
assertThat(itemStatistics.getHitCount()).isEqualTo(0);
assertThat(itemStatistics.getMissCount()).isEqualTo(0);

assertThat(coherenceStorageAccess.getDelegate().getElementCountInMemory()).isEqualTo(1);

final CoherenceRegionFactory coherenceRegionFactory = (CoherenceRegionFactory) region.getRegionFactory();
final com.tangosol.net.Session coherenceSession = coherenceRegionFactory.getCoherenceSession();

assertThat(coherenceSession.getCache("foobar.foo").size()).isEqualTo(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.hibernate.cache.spi.access.AccessType;
import org.hibernate.cache.spi.support.DomainDataStorageAccess;
import org.hibernate.cache.spi.support.RegionFactoryTemplate;
import org.hibernate.cache.spi.support.RegionNameQualifier;
import org.hibernate.cache.spi.support.StorageAccess;
import org.hibernate.cfg.Environment;
import org.hibernate.engine.spi.SessionFactoryImplementor;
Expand Down Expand Up @@ -280,7 +281,7 @@ public long nextTimestamp() {
* @return a NamedCache for the argument name
*/
protected NamedCache<?, ?> ensureNamedCache(String cacheName) {
return this.coherenceSession.getCache(cacheName);
return this.coherenceSession.getCache(RegionNameQualifier.INSTANCE.qualify(cacheName, getOptions()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
package com.oracle.coherence.hibernate.cache.v6;

import com.oracle.coherence.hibernate.cache.v6.access.CoherenceDomainDataRegionImpl;
import com.oracle.coherence.hibernate.cache.v6.access.CoherenceStorageAccessImpl;
import com.oracle.coherence.hibernate.cache.v6.support.Foo;
import com.tangosol.net.CacheFactory;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.stat.CacheRegionStatistics;
import org.hibernate.stat.Statistics;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.AfterClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Gunnar Hillert
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class CacheRegionPrefixTests extends BaseCoreFunctionalTestCase {

private Long idOfSavedItem = null;

@AfterClass
public static void after() {
CacheFactory.shutdown();
}

@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {Foo.class};
}

@Override
protected void configure(Configuration cfg) {
super.configure(cfg);
cfg.setProperty(Environment.CACHE_REGION_PREFIX, "foobar");
cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true");
cfg.setProperty(Environment.USE_QUERY_CACHE, "true");
cfg.setProperty(Environment.CACHE_REGION_FACTORY, CoherenceRegionFactory.class.getName());
cfg.setProperty("com.oracle.coherence.hibernate.cache.cache_config_file_path", "tests-hibernate-second-level-cache-config.xml");
}

@Test
public void test_01_addItem() {
final Statistics statistics = this.sessionFactory().getStatistics();

final CoherenceDomainDataRegionImpl region = (CoherenceDomainDataRegionImpl) this.sessionFactory().getCache().getRegion("foo");
final CoherenceStorageAccessImpl coherenceStorageAccess = (CoherenceStorageAccessImpl) region.getCacheStorageAccess();

assertThat(coherenceStorageAccess.getDelegate().getElementCountInMemory()).isEqualTo(0);

final Session session = openSession();
session.beginTransaction();
final Foo itemToSave = new Foo("bar");
this.idOfSavedItem = (Long) session.save(itemToSave);
session.flush();
session.getTransaction().commit();

final CacheRegionStatistics itemStatistics = statistics.getDomainDataRegionStatistics("foo");

assertThat(itemStatistics.getPutCount()).isEqualTo(1);
assertThat(itemStatistics.getHitCount()).isEqualTo(0);
assertThat(itemStatistics.getMissCount()).isEqualTo(0);

assertThat(coherenceStorageAccess.getDelegate().getElementCountInMemory()).isEqualTo(1);

final CoherenceRegionFactory coherenceRegionFactory = (CoherenceRegionFactory) region.getRegionFactory();
final com.tangosol.net.Session coherenceSession = coherenceRegionFactory.getCoherenceSession();

assertThat(coherenceSession.getCache("foobar.foo").size()).isEqualTo(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ spring:
hibernate.cache.use_minimal_puts: false
hibernate.cache.region.factory_class: com.oracle.coherence.hibernate.cache.v53.CoherenceRegionFactory
hibernate.cache.use_query_cache: true
hibernate.cache.region_prefix: foobar
com.oracle.coherence.hibernate.cache.cache_config_file_path: hibernate-second-level-cache-config.xml
hibernate:
dialect: org.hibernate.dialect.HSQLDialect
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023, Oracle and/or its affiliates.
* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
Expand Down Expand Up @@ -48,13 +48,14 @@
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class CoherenceHibernateDemoApplicationTests {

private static final String EVENT_ENTITY_CACHE_NAME = "com.oracle.coherence.hibernate.demo.model.Event";
private static final String PERSON_ENTITY_CACHE_NAME = "com.oracle.coherence.hibernate.demo.model.Person";
private static final String CACHE_PREFIX = "foobar.";
private static final String EVENT_ENTITY_CACHE_NAME = CACHE_PREFIX + "com.oracle.coherence.hibernate.demo.model.Event";
private static final String PERSON_ENTITY_CACHE_NAME = CACHE_PREFIX + "com.oracle.coherence.hibernate.demo.model.Person";

private static final String EVENT_PARTICIPANTS_COLLECTION_CACHE_NAME = "com.oracle.coherence.hibernate.demo.model.Event.participants";
private static final String PERSON_EVENTS_COLLECTION_CACHE_NAME = "com.oracle.coherence.hibernate.demo.model.Person.events";
private static final String EVENT_PARTICIPANTS_COLLECTION_CACHE_NAME = CACHE_PREFIX + "com.oracle.coherence.hibernate.demo.model.Event.participants";
private static final String PERSON_EVENTS_COLLECTION_CACHE_NAME = CACHE_PREFIX + "com.oracle.coherence.hibernate.demo.model.Person.events";

private static final String DEFAULT_QUERY_RESULTS_REGION = "default-query-results-region";
private static final String DEFAULT_QUERY_RESULTS_REGION = CACHE_PREFIX + "default-query-results-region";

@Autowired
private MockMvc mvc;
Expand Down Expand Up @@ -135,6 +136,9 @@ void createOneEvent() throws Exception {
assertThat(cacheValue).isInstanceOf(CoherenceRegionValue.class);
assertThat(((CoherenceRegionValue) cacheValue).getValue()).isInstanceOf(StandardCacheEntryImpl.class);

final NamedCache<Object, CoherenceRegionValue> namedCache = CacheFactory.getCache(CACHE_PREFIX + "com.oracle.coherence.hibernate.demo.model.Event");
assertThat(namedCache).hasSize(1);

assertThat(this.testStats.getCoherenceEventCacheSize()).isEqualTo(1);
assertThat(this.testStats.getCoherencePersonCacheSize()).isZero();
assertThat(this.testStats.getCoherenceDefaultQueryResultsRegionCacheSize()).isZero();
Expand Down Expand Up @@ -181,6 +185,9 @@ void getEvent() throws Exception {
assertThat(this.testStats.getCoherenceEventParticipantsCollectionCacheSize()).isZero();
assertThat(this.testStats.getCoherencePersonEventsCollectionCacheSize()).isZero();

final NamedCache<Object, CoherenceRegionValue> namedCache = CacheFactory.getCache(CACHE_PREFIX + "com.oracle.coherence.hibernate.demo.model.Event");
assertThat(namedCache).hasSize(1);

assertThat(this.testStats.getCacheHitCount()).isEqualTo(1);
assertThat(this.testStats.getCacheMissCount()).isZero();
assertThat(this.testStats.getCachePutCount()).isEqualTo(1);
Expand Down Expand Up @@ -379,6 +386,9 @@ void createSecondPerson() throws Exception {
assertThat(this.testStats.getCoherenceEventParticipantsCollectionCacheSize()).isEqualTo(1);
assertThat(this.testStats.getCoherencePersonEventsCollectionCacheSize()).isZero();

final NamedCache<Object, CoherenceRegionValue> namedCache = CacheFactory.getCache(CACHE_PREFIX + "com.oracle.coherence.hibernate.demo.model.Event");
assertThat(namedCache).hasSize(1);

assertThat(this.testStats.getCacheHitCount()).isEqualTo(2);
assertThat(this.testStats.getCacheMissCount()).isEqualTo(1);
assertThat(this.testStats.getCachePutCount()).isEqualTo(7);
Expand Down

0 comments on commit 6f0ae40

Please sign in to comment.