如何解决InfiniSpan 如何获取缓存
我正在尝试通过 Hibernate 会话获取名为“default”(在我的 persistence.xml 中定义)的 InfiniSpan 缓存:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("mytest");
SessionFactory unwrap = emf.unwrap(SessionFactory.class);
CacheImplementor cache = (CacheImplementor) unwrap.getCache();
RegionFactory regionFactory = cache.getRegionFactory();
if (regionFactory instanceof InfinispanRegionFactory) {
InfinispanRegionFactory reg = (InfinispanRegionFactory) regionFactory;
EmbeddedCacheManager cacheManager = reg.getCacheManager();
}
final Cache<String,String> cache2 = cacheManager.getCache("default");
AdvancedCache ac = cache2.getAdvancedCache();
TransactionManager tm = cache2.getAdvancedCache().getTransactionManager();
// Not using EntityManagerFactory here,because otherwise the test-infinispan.xml is already loaded.
EmbeddedCacheManager cacheManager = new DefaultCacheManager("./resources/test-infinispan.xml");
final Cache<String,String> cache = cacheManager.getCache("default");
TransactionManager tm = cache.getAdvancedCache().getTransactionManager();
我在 tm 初始化后使用的代码:
try {
tm.begin();
if (cache.get("test1") != null) {
System.out.println("FOUND IN CACHE: test1");
}
cache.put("test1","test1");
tm.commit();
} catch (Exception e) {
try {
tm.rollback();
} catch (Exception e2) {
}
} finally {
cacheManager.stop();
}
使用#2,我可以获得“默认”缓存并向其中添加项目。事务管理器 tm 不为空,所以没问题。重新启动后,缓存中保存的数据再次可用。
#1 事务管理器 tm 为空。 使用 TransactionManager 的另一个实例,我可以将项目添加到缓存中,但它们不是 重启后可用。我也尝试使用 @Cacheable 区域。
persistence.xml 中使用的属性:
<property name="hibernate.ogm.infinispan.configuration_resource_name" value="test-infinispan.xml" />
<property name="hibernate.search.default.directory_provider" value="infinispan" />
<property name="hibernate.search.infinispan.configuration_resourcename" value="test-infinispan-index.xml" />
<property name="hibernate.cache.use_second_level_cache" value="true"></property>
<property name="hibernate.cache.use_query_cache" value="true"></property>
<property name="hibernate.cache.region.factory_class" value="org.infinispan.hibernate.cache.v53.InfinispanRegionFactory"></property>
<property name="hibernate.cache.inifinispan.statistics" value="true"></property>
<property name="hibernate.cache.default_cache_concurrency_strategy" value="transactional"></property>
<property name="hibernate.search.services.jgroups.configurationFile" value="test-jgroups-tcp-static-cluster.xml" />
<property name="hibernate.search.services.jgroups.clusterName" value="default-test-index-cluster-2" />
EntityManagerFactory 加载持久性单元“mytest”并初始化从persistence.xml、InfiniSpan 配置、InfiniSpan 索引配置开始的整个结构。和 jgroups 配置。
test-infinispan.xml:
<cache-container name="Database-Container" default-cache="default" statistics="false" shutdown-hook="DONT_REGISTER">
<transport stack="default-tcp-static-cluster" cluster="default-test-cluster" />
<replicated-cache name="default" mode="SYNC" remote-timeout="480000">
<locking striping="false" acquire-timeout="120000" concurrency-level="500" isolation="REPEATABLE_READ" />
<transaction locking="pessimistic" auto-commit="false" mode="FULL_XA" transaction-manager-lookup="org.infinispan.transaction.lookup.JBossstandaloneJTAManagerLookup"/>
<indexing index="NONE"></indexing>
<memory>
<object size="-1" />
</memory>
<expiration max-idle="-1" />
<state-transfer enabled="true" timeout="480000" await-initial-transfer="true" />
<persistence passivation="false">
<file-store shared="false" preload="false" fetch-state="true" read-only="false" purge="false" path="./database_data/DEFAULT">
<write-behind modification-queue-size="5" fail-silently="false"/>
</file-store>
</persistence>
</replicated-cache>
</cache-container>
我需要什么才能获得示例 #1 中的“默认”缓存?或者那是不可能的? 或者我是否需要使用区域(使用 @Cacheable 注释),如果需要,我需要什么 为了使缓存(临时?)持久化,以便我可以在重启后重新加载数据。
解决方法
我坚持使用这种方式来获取缓存管理器:
EmbeddedCacheManager cacheManager = new DefaultCacheManager("my-cache.xml");
在单独的 xml 文件中指定缓存。 这看起来正是我需要的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。