Search in sources :

Example 1 with GeneralDataRegion

use of org.hibernate.cache.spi.GeneralDataRegion in project hibernate-orm by hibernate.

the class AbstractGeneralDataRegionTest method withSessionFactoriesAndRegions.

protected void withSessionFactoriesAndRegions(int num, SFRConsumer consumer) throws Exception {
    StandardServiceRegistryBuilder ssrb = createStandardServiceRegistryBuilder().applySetting(AvailableSettings.CACHE_REGION_FACTORY, TestInfinispanRegionFactory.class.getName());
    Properties properties = CacheTestUtil.toProperties(ssrb.getSettings());
    List<StandardServiceRegistry> registries = new ArrayList<>();
    List<SessionFactory> sessionFactories = new ArrayList<>();
    List<GeneralDataRegion> regions = new ArrayList<>();
    for (int i = 0; i < num; ++i) {
        StandardServiceRegistry registry = ssrb.build();
        registries.add(registry);
        SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        sessionFactories.add(sessionFactory);
        InfinispanRegionFactory regionFactory = (InfinispanRegionFactory) registry.getService(RegionFactory.class);
        GeneralDataRegion region = (GeneralDataRegion) createRegion(regionFactory, getStandardRegionName(REGION_PREFIX), properties, null);
        regions.add(region);
    }
    try {
        consumer.accept(sessionFactories, regions);
    } finally {
        for (SessionFactory sessionFactory : sessionFactories) {
            sessionFactory.close();
        }
        for (StandardServiceRegistry registry : registries) {
            StandardServiceRegistryBuilder.destroy(registry);
        }
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) InfinispanRegionFactory(org.hibernate.cache.infinispan.InfinispanRegionFactory) TestInfinispanRegionFactory(org.hibernate.test.cache.infinispan.util.TestInfinispanRegionFactory) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) ArrayList(java.util.ArrayList) MetadataSources(org.hibernate.boot.MetadataSources) Properties(java.util.Properties) BaseGeneralDataRegion(org.hibernate.cache.infinispan.impl.BaseGeneralDataRegion) GeneralDataRegion(org.hibernate.cache.spi.GeneralDataRegion) InfinispanRegionFactory(org.hibernate.cache.infinispan.InfinispanRegionFactory) RegionFactory(org.hibernate.cache.spi.RegionFactory) TestInfinispanRegionFactory(org.hibernate.test.cache.infinispan.util.TestInfinispanRegionFactory) TestInfinispanRegionFactory(org.hibernate.test.cache.infinispan.util.TestInfinispanRegionFactory) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry)

Example 2 with GeneralDataRegion

use of org.hibernate.cache.spi.GeneralDataRegion in project hibernate-orm by hibernate.

the class AbstractGeneralDataRegionTest method testEvictAll.

/**
	 * Test method for {@link QueryResultsRegion#evictAll()}.
	 * <p/>
	 * FIXME add testing of the "immediately without regard for transaction isolation" bit in the
	 * CollectionRegionAccessStrategy API.
	 */
public void testEvictAll() throws Exception {
    withSessionFactoriesAndRegions(2, (sessionFactories, regions) -> {
        GeneralDataRegion localRegion = regions.get(0);
        GeneralDataRegion remoteRegion = regions.get(1);
        AdvancedCache localCache = ((BaseGeneralDataRegion) localRegion).getCache();
        AdvancedCache remoteCache = ((BaseGeneralDataRegion) remoteRegion).getCache();
        SharedSessionContractImplementor localSession = (SharedSessionContractImplementor) sessionFactories.get(0).openSession();
        SharedSessionContractImplementor remoteSession = (SharedSessionContractImplementor) sessionFactories.get(1).openSession();
        try {
            Set localKeys = localCache.keySet();
            assertEquals("No valid children in " + localKeys, 0, localKeys.size());
            Set remoteKeys = remoteCache.keySet();
            assertEquals("No valid children in " + remoteKeys, 0, remoteKeys.size());
            assertNull("local is clean", localRegion.get(null, KEY));
            assertNull("remote is clean", remoteRegion.get(null, KEY));
            localRegion.put(localSession, KEY, VALUE1);
            assertEquals(VALUE1, localRegion.get(null, KEY));
            remoteRegion.put(remoteSession, KEY, VALUE1);
            assertEquals(VALUE1, remoteRegion.get(null, KEY));
            localRegion.evictAll();
            // This should re-establish the region root node in the optimistic case
            assertNull(localRegion.get(null, KEY));
            localKeys = localCache.keySet();
            assertEquals("No valid children in " + localKeys, 0, localKeys.size());
            // Re-establishing the region root on the local node doesn't
            // propagate it to other nodes. Do a get on the remote node to re-establish
            // This only adds a node in the case of optimistic locking
            assertEquals(null, remoteRegion.get(null, KEY));
            remoteKeys = remoteCache.keySet();
            assertEquals("No valid children in " + remoteKeys, 0, remoteKeys.size());
            assertEquals("local is clean", null, localRegion.get(null, KEY));
            assertEquals("remote is clean", null, remoteRegion.get(null, KEY));
        } finally {
            localSession.close();
            remoteSession.close();
        }
    });
}
Also used : BaseGeneralDataRegion(org.hibernate.cache.infinispan.impl.BaseGeneralDataRegion) GeneralDataRegion(org.hibernate.cache.spi.GeneralDataRegion) Set(java.util.Set) BaseGeneralDataRegion(org.hibernate.cache.infinispan.impl.BaseGeneralDataRegion) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) AdvancedCache(org.infinispan.AdvancedCache)

Aggregations

BaseGeneralDataRegion (org.hibernate.cache.infinispan.impl.BaseGeneralDataRegion)2 GeneralDataRegion (org.hibernate.cache.spi.GeneralDataRegion)2 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 Set (java.util.Set)1 SessionFactory (org.hibernate.SessionFactory)1 MetadataSources (org.hibernate.boot.MetadataSources)1 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)1 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)1 InfinispanRegionFactory (org.hibernate.cache.infinispan.InfinispanRegionFactory)1 RegionFactory (org.hibernate.cache.spi.RegionFactory)1 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)1 TestInfinispanRegionFactory (org.hibernate.test.cache.infinispan.util.TestInfinispanRegionFactory)1 AdvancedCache (org.infinispan.AdvancedCache)1