大家好,我对 hazelcast 5.3、hibernate 5.6 和 spring boot 2.7 有一个大问题,当调用可缓存方法时,我在从缓存检索数据时遇到无会话错误,我的问题出在我的 hazelcast 配置中,但我不这样做知道错误来自哪里有人可以帮忙吗? 这是我的 hazelcast 配置
@Configuration
public class HazelCastConfiguration {
@Bean
public Config hazelcastConfig() {
Config config = new Config();
config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(true);
config.setClassLoader(Thread.currentThread().getContextClassLoader());
final UserCodeDeploymentConfig distCLConfig = config.getUserCodeDeploymentConfig();
distCLConfig.setEnabled(true)
.setClassCacheMode(UserCodeDeploymentConfig.ClassCacheMode.ETERNAL)
.setProviderMode(UserCodeDeploymentConfig.ProviderMode.LOCAL_CLASSES_ONLY);
config.addMapConfig(rajouterConfig("produits",5000,0));
return config;
}
private MapConfig rajouterConfig(String nomCache, int taille, int tempsDeVie){
return new MapConfig()
.setName(nomCache)
.setTimeToLiveSeconds(tempsDeVie)
.setEvictionConfig(new EvictionConfig()
.setSize(taille)
.setMaxSizePolicy(MaxSizePolicy.PER_NODE)
.setEvictionPolicy(EvictionPolicy.LRU));
}
@Bean
public HazelcastInstance hazelcastInstance() {
return Hazelcast.newHazelcastInstance(hazelcastConfig());
}
@Bean
public CacheManager cacheManager() {
return new HazelcastCacheManager(hazelcastInstance());
}`
我的 spring 方法只是一个简单的可缓存方法,如下所示:
@Cacheable(value=CACHE_MANAGER, keyGenerator = TrMethodAndParamsKeyGenerator.TR_KEY_GENERATOR_NAME , unless="#result == null or #result.size()==0") public List<ProfilQuestionEntite> recupererParModele(String profilModeleQuestion, String codeCategorie) {...}
}
删除我的 hazelcast 配置类时它可以工作,但我想在我的项目中使用 hazelcast,我的配置出现问题,我找不到问题所在 错误是我在第二次通话时遇到的错误:
no Session; nested exception is org.hibernate.LazyInitializationException: could not initialize proxy [com.e.#2] - no Session
解决方案就是创建一个 type 的 bean
@Bean
public Config cacheConfig() {...}
这将自动将 hazelcast 作为主缓存。 我们还需要删除两个豆子:
@Bean
public HazelcastInstance hazelcastInstance() {
return Hazelcast.newHazelcastInstance(hazelcastConfig());
}
@Bean
public CacheManager cacheManager() {
return new HazelcastCacheManager(hazelcastInstance());
}`