Search in sources :

Example 26 with CacheException

use of org.hibernate.cache.CacheException in project hibernate-orm by hibernate.

the class StorageAccessTests method testOnTheFlyCreationDisallowed.

@Test
public void testOnTheFlyCreationDisallowed() {
    // first, lets make sure that the region name we think is non-existent really does not exist
    final CacheManager cacheManager = JCacheHelper.locateStandardCacheManager();
    assertThat(cacheManager.getCache(NON_CACHE_NAME), nullValue());
    // and now let's try to build the standard testing SessionFactory, without pre-defining caches
    try (SessionFactoryImplementor sessionFactory = TestHelper.buildStandardSessionFactory(false)) {
        fail();
    } catch (ServiceException expected) {
        assertTyping(CacheException.class, expected.getCause());
        assertThat(expected.getMessage(), CoreMatchers.equalTo("Unable to create requested service [" + org.hibernate.cache.spi.CacheImplementor.class.getName() + "]"));
        assertThat(expected.getCause().getMessage(), CoreMatchers.startsWith("On-the-fly creation of JCache Cache objects is not supported"));
    } catch (CacheException expected) {
        assertThat(expected.getMessage(), CoreMatchers.equalTo("On-the-fly creation of JCache Cache objects is not supported"));
    }
}
Also used : ServiceException(org.hibernate.service.spi.ServiceException) CacheException(org.hibernate.cache.CacheException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) CacheManager(javax.cache.CacheManager) Test(org.junit.Test)

Example 27 with CacheException

use of org.hibernate.cache.CacheException in project hibernate-orm by hibernate.

the class RegionFactoryInitiator method resolveRegionFactory.

@SuppressWarnings({ "unchecked", "WeakerAccess" })
protected RegionFactory resolveRegionFactory(Map configurationValues, ServiceRegistryImplementor registry) {
    final Properties p = new Properties();
    p.putAll(configurationValues);
    final Boolean useSecondLevelCache = ConfigurationHelper.getBooleanWrapper(AvailableSettings.USE_SECOND_LEVEL_CACHE, configurationValues, null);
    final Boolean useQueryCache = ConfigurationHelper.getBooleanWrapper(AvailableSettings.USE_QUERY_CACHE, configurationValues, null);
    // 2) USE_SECOND_LEVEL_CACHE is FALSE and USE_QUERY_CACHE is null
    if (useSecondLevelCache != null && useSecondLevelCache == FALSE) {
        if (useQueryCache == null || useQueryCache == FALSE) {
            return NoCachingRegionFactory.INSTANCE;
        }
    }
    final Object setting = configurationValues.get(AvailableSettings.CACHE_REGION_FACTORY);
    final StrategySelector selector = registry.getService(StrategySelector.class);
    final Collection<Class<? extends RegionFactory>> implementors = selector.getRegisteredStrategyImplementors(RegionFactory.class);
    if (setting == null && implementors.size() != 1) {
        // if either are explicitly defined as TRUE we need a RegionFactory
        if ((useSecondLevelCache != null && useSecondLevelCache == TRUE) || (useQueryCache != null && useQueryCache == TRUE)) {
            throw new CacheException("Caching was explicitly requested, but no RegionFactory was defined and there is not a single registered RegionFactory");
        }
    }
    final RegionFactory regionFactory = registry.getService(StrategySelector.class).resolveStrategy(RegionFactory.class, setting, (RegionFactory) null, new StrategyCreatorRegionFactoryImpl(p));
    if (regionFactory != null) {
        return regionFactory;
    }
    if (implementors.size() == 1) {
        final RegionFactory registeredFactory = selector.resolveStrategy(RegionFactory.class, implementors.iterator().next());
        configurationValues.put(AvailableSettings.CACHE_REGION_FACTORY, registeredFactory);
        configurationValues.put(AvailableSettings.USE_SECOND_LEVEL_CACHE, "true");
        return registeredFactory;
    } else {
        LOG.debugf("Cannot default RegionFactory based on registered strategies as `%s` RegionFactory strategies were registered", implementors);
    }
    return NoCachingRegionFactory.INSTANCE;
}
Also used : RegionFactory(org.hibernate.cache.spi.RegionFactory) CacheException(org.hibernate.cache.CacheException) Properties(java.util.Properties) StrategySelector(org.hibernate.boot.registry.selector.spi.StrategySelector)

Example 28 with CacheException

use of org.hibernate.cache.CacheException in project wildfly by wildfly.

the class ManagedEmbeddedCacheManagerProvider method getEmbeddedCacheManager.

@Override
public EmbeddedCacheManager getEmbeddedCacheManager(Properties properties) {
    Properties settings = new Properties();
    String container = properties.getProperty(CACHE_CONTAINER, DEFAULT_CACHE_CONTAINER);
    settings.setProperty(HibernateSecondLevelCache.CONTAINER, container);
    if (!Boolean.parseBoolean(properties.getProperty(SHARED, DEFAULT_SHARED))) {
        HibernateSecondLevelCache.addSecondLevelCacheDependencies(properties, null);
        settings.setProperty(HibernateSecondLevelCache.CACHE_TYPE, HibernateSecondLevelCache.CACHE_PRIVATE);
        // Find a suitable service name to represent this session factory instance
        String name = properties.getProperty(AvailableSettings.SESSION_FACTORY_NAME);
        if (name != null) {
            settings.setProperty(HibernateSecondLevelCache.NAME, name);
        }
        settings.setProperty(HibernateSecondLevelCache.CACHES, String.join(" ", HibernateSecondLevelCache.findCaches(properties)));
    }
    try {
        return new JipiJapaCacheManager(Notification.startCache(Classification.INFINISPAN, settings));
    } catch (CacheException e) {
        throw e;
    } catch (Exception e) {
        throw new CacheException(e);
    }
}
Also used : CacheException(org.hibernate.cache.CacheException) Properties(java.util.Properties) CacheException(org.hibernate.cache.CacheException)

Aggregations

CacheException (org.hibernate.cache.CacheException)28 Properties (java.util.Properties)11 URL (java.net.URL)4 Configuration (net.sf.ehcache.config.Configuration)4 EmbeddedCacheManager (org.infinispan.manager.EmbeddedCacheManager)4 CacheManager (javax.cache.CacheManager)3 Element (net.sf.ehcache.Element)3 NonStopCacheException (net.sf.ehcache.constructs.nonstop.NonStopCacheException)3 DatabaseRedisClient (com.publiccms.common.redis.DatabaseRedisClient)2 MalformedURLException (java.net.MalformedURLException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 CachingProvider (javax.cache.spi.CachingProvider)2 InitialContext (javax.naming.InitialContext)2 NamingException (javax.naming.NamingException)2 CacheManager (net.sf.ehcache.CacheManager)2 Test (org.junit.Test)2 RedissonClient (org.redisson.api.RedissonClient)2 Config (org.redisson.config.Config)2 Enumeration (java.util.Enumeration)1