📄 multiplexingcacheinstancemanager.java
字号:
* {@inheritDoc} */ public Cache getTimestampsCacheInstance() { if (jbcTsCache != null && CacheHelper.isClusteredInvalidation(jbcTsCache)) { throw new CacheException("Clustered invalidation not supported for timestamps cache"); } return jbcTsCache; } /** * {@inheritDoc} */ public void start(Settings settings, Properties properties) throws CacheException { try { // We need our tm, so get it now and avoid doing other work // if there is a problem TransactionManagerLookup tml = settings.getTransactionManagerLookup(); TransactionManager tm = (tml == null ? null : tml.getTransactionManager(properties)); // We only build caches if *none* were passed in. Passing in // caches counts as a clear statement of exactly what is wanted boolean buildCaches = jbcEntityCache == null && jbcCollectionCache == null && jbcTsCache == null && jbcQueryCache == null; // Set up the cache factory if (buildCaches && jbcFactory == null) { // See if the user configured a multiplexer stack if (channelFactory == null) { String muxStacks = PropertiesHelper.getString(CHANNEL_FACTORY_RESOURCE_PROP, properties, DEF_JGROUPS_RESOURCE); if (muxStacks != null) { channelFactory = new JChannelFactory(); channelFactory.setMultiplexerConfig(muxStacks); } } String factoryRes = PropertiesHelper.getString(CACHE_FACTORY_RESOURCE_PROP, properties, DEF_CACHE_FACTORY_RESOURCE); jbcFactory = new CacheManagerImpl(factoryRes, channelFactory); ((CacheManagerImpl) jbcFactory).start(); selfCreatedFactory = true; } if (settings.isSecondLevelCacheEnabled()) { if (buildCaches) { entityConfig = PropertiesHelper .getString(ENTITY_CACHE_RESOURCE_PROP, properties, DEF_ENTITY_RESOURCE); jbcEntityCache = jbcFactory.getCache(entityConfig, true); // Default to collections sharing entity cache if there is one collectionConfig = PropertiesHelper.getString(COLLECTION_CACHE_RESOURCE_PROP, properties, entityConfig); if (entityConfig.equals(collectionConfig)) { jbcCollectionCache = jbcEntityCache; } else { jbcCollectionCache = jbcFactory.getCache(collectionConfig, true); } } if (jbcEntityCache != null) { configureTransactionManager(jbcEntityCache, tm, false); jbcEntityCache.start(); } if (jbcCollectionCache != null) { configureTransactionManager(jbcCollectionCache, tm, false); jbcCollectionCache.start(); } } else { jbcEntityCache = null; jbcCollectionCache = null; } if (settings.isQueryCacheEnabled()) { if (buildCaches) { // Default to sharing the entity cache if there is one String dfltQueryResource = (entityConfig == null ? DEF_QUERY_RESOURCE : entityConfig); queryConfig = PropertiesHelper.getString(QUERY_CACHE_RESOURCE_PROP, properties, dfltQueryResource); if (queryConfig.equals(entityConfig)) { jbcQueryCache = jbcEntityCache; } else if (queryConfig.equals(collectionConfig)) { jbcQueryCache = jbcCollectionCache; } else { jbcQueryCache = jbcFactory.getCache(queryConfig, true); } // For Timestamps, we default to a separate config tsConfig = PropertiesHelper.getString(TIMESTAMP_CACHE_RESOURCE_PROP, properties, DEF_TS_RESOURCE); if (tsConfig.equals(queryConfig)) { jbcTsCache = jbcQueryCache; } else if (tsConfig.equals(entityConfig)) { jbcTsCache = jbcEntityCache; } else if (tsConfig.equals(collectionConfig)) { jbcTsCache = jbcCollectionCache; } else { jbcTsCache = jbcFactory.getCache(tsConfig, true); } } if (jbcQueryCache != null) { configureTransactionManager(jbcQueryCache, tm, false); jbcQueryCache.start(); // TODO: I considered validating the presence of the TS cache here, // but decided to defer unti getQueryCacheInstance() in case the // cache is never actually used } if (jbcTsCache != null) { configureTransactionManager(jbcTsCache, tm, true); jbcTsCache.start(); // TODO: I considered validating TS cache config here, // but decided to defer unti getTimestampsCacheInstance() in case the // cache is never actually used } } else { jbcTsCache = null; jbcQueryCache = null; } } catch (CacheException ce) { throw ce; } catch (Throwable t) { throw new CacheException("Unable to start region factory", t); } } /** * {@inheritDoc} */ public void stop() { releaseCaches(); if (selfCreatedFactory) { ((CacheManagerImpl) jbcFactory).stop(); } } /** * Injects the given TransactionManager into the cache. * * @param cache the cache. cannot be <code>null</code> * @param tm the transaction manager Hibernate recognizes * May be <code>null</code> * @param allowNull whether we accept a null transaction manager in the cache * if <code>tm</code> is not <code>null</code> * * @throws CacheException if <code>cache</code> is already started and is * configured with a different TransactionManager * than the one we would inject */ private void configureTransactionManager(Cache cache, TransactionManager tm, boolean allowNull) { Configuration cacheConfig = cache.getConfiguration(); TransactionManager cacheTm = cacheConfig.getRuntimeConfig().getTransactionManager(); if (!safeEquals(tm, cacheTm)) { if (cache.getCacheStatus() != CacheStatus.INSTANTIATED) { // We can't change the TM on a running cache; just check // if the cache has no TM and we're OK with that if (!allowNull && cacheTm == null) { throw new CacheException("JBoss Cache is already started with no transaction manager configured"); } else { log.debug("JBoss Cache is already started with a transaction manager (" + cacheTm + ") that is not equal to our own (" + tm + ")"); } } else { // Configure the cache to use our TM cacheConfig.getRuntimeConfig().setTransactionManager(tm); if (tm == null) { // Make sure JBC doesn't look one up cacheConfig.setTransactionManagerLookupClass(null); } } } } /** * Notify cache factory that we are no longer using the caches. */ private void releaseCaches() { // This method should be implemented assuming it's valid to // do start/stop/start -- leave state appropriate for another start if (jbcEntityCache != null && entityConfig != null) { try { jbcFactory.releaseCache(entityConfig); jbcEntityCache = null; // Make sure we don't re-release the same cache if (entityConfig.equals(collectionConfig)) collectionConfig = null; if (entityConfig.equals(queryConfig)) queryConfig = null; if (entityConfig.equals(tsConfig)) tsConfig = null; entityConfig = null; } catch (Throwable t) { log.info("Unable to release entity cache instance", t); } } if (jbcCollectionCache != null && collectionConfig != null) { try { jbcFactory.releaseCache(collectionConfig); jbcCollectionCache = null; if (collectionConfig.equals(queryConfig)) queryConfig = null; if (collectionConfig.equals(tsConfig)) tsConfig = null; collectionConfig = null; } catch (Throwable t) { log.info("Unable to stop collection cache instance", t); } } if (jbcQueryCache != null && queryConfig != null) { try { jbcFactory.releaseCache(queryConfig); jbcQueryCache = null; if (queryConfig.equals(tsConfig)) tsConfig = null; queryConfig = null; } catch (Throwable t) { log.info("Unable to stop query cache instance", t); } } if (jbcTsCache != null && tsConfig != null) { try { jbcFactory.releaseCache(tsConfig); jbcTsCache = null; tsConfig = null; } catch (Throwable t) { log.info("Unable to stop timestamp cache instance", t); } } } private boolean safeEquals(Object a, Object b) { return (a == b || (a != null && a.equals(b))); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -