我有两个应用程序使用相同的数据库和相同的模式。应用程序A将读写表A。应用程序B将只读取表A。
如果我在两个应用程序中都配置了二级缓存。应用程序B将不会检索应用程序A之前更新表A中的记录的更新值。
我希望将相同的hibernate配置配置为与多个应用程序共享相同的缓存(在本例中,应用程序B可以获得更新值)
我读了一些堆栈溢出,但仍然不适合我。
我使用hibernate 5并使用Ehcache进行缓存管理。
你知道我该怎么实现它吗?我可以有推荐人吗?
下面是我的两个应用程序中的一些hibernate属性
Sprint hibernate配置:
 <bean id="mySessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource" />
    <property name="annotatedClasses">
        <list>
            ...
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <!-- Debug -->
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.format_sql">false</prop>
            <prop key="hibernate.hbm2ddl.auto">none</prop>
            <!-- Level two caching -->
            <prop key="hibernate.cache.provider_class">=net.sf.ehcache.hibernate.EhCacheProvider</prop>
            <prop key="hibernate.cache.use_second_level_cache">true</prop>
            <prop key="hibernate.cache.use_query_cache">false</prop>
            <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
            <prop key="net.sf.ehcache.configurationResourceName">../conf/ehcache.xml</prop>
    </property>
</bean>ehcache.xml:
<?xml version="1.0" encoding="UTF-8"?>
     <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd"
         updateCheck="true" monitoring="autodetect"
         dynamicConfig="true" >
     <diskStore path="java.io.tmpdir"/>
     <!-- Default cache settings -->
     <defaultCache
          maxElementsInMemory="100000"
          eternal="true"
          timeToIdleSeconds="300"
          timeToLiveSeconds="300"
          overflowToDisk="false"
          maxElementsOnDisk="10000000"
          diskPersistent="false"
          diskExpiryThreadIntervalSeconds="300"
          memoryStoreEvictionPolicy="LRU">
    </defaultCache>
发布于 2021-08-25 18:09:06
您可以考虑使用分布式缓存,如Hazelcast。事实上,Hazelcast内置了与Hibernate的集成,可以很容易地插入二级缓存
https://stackoverflow.com/questions/55062085
复制相似问题