【开源中国 APP 全新上线】“动弹” 回归、集成大模型对话、畅读技术报告”
我在本地部署了两个tomcat服务server1和server2,但是我在server1中查询或者修改单条数据的时候,server2中的缓存能做相应的变化,但是当在server1做查询所有集合、添加、删除操作的时候,server2的缓存没任何变化,具体配置文件如下hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ehcheTest?useUnicode=true&characterEncoding=UTF-8&mysqlEncoding=utf-8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Enable the second-level cache -->
<property name="cache.use_second_level_cache">true</property>
<property name="cache.use_query_cache">true</property>
<property name="net.sf.ehcache.configurationResourceName">ehcache.xml</property>
<property name="hibernate.cache.region.factory_class">
net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.cache.region_prefix"></property>
<mapping resource="com/nokia/test/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
server1 中的ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<diskStore path="java.io.tmpdir"/>
<!--调用ehcache2的RMI-->
<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,rmiUrls=//192.168.10.167:40002/userIdcache"/>
<!--RMI监听40001端口-->
<cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=192.168.10.167,port=40001,socketTimeoutMillis=2000"/>
<defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120"
timeToLiveSeconds="120" overflowToDisk="true" diskSpoolBufferSizeMB="30"
maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
</defaultCache>
<cache name="userIdcache"
maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="1800" timeToLiveSeconds="0"
overflowToDisk="true" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" diskPersistent="true"
diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU">
<!--监听配置-->
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true,replicatePuts=true,replicateUpdates=true,replicatePutsViaCopy=true,replicateUpdatesViaCopy=true,replicateRemovals=true" />
</cache>
</ehcache>
server2中的配置文件和server1中只有这两个端口不同
<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,rmiUrls=//192.168.10.167: 40001 /userIdcache"/>
<!--RMI监听40001端口-->
<cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=192.168.10.167,port= 40002 ,socketTimeoutMillis=2000"/>
model的配置文件如下
<hibernate-mapping package="com.nokia.test">
<class name="Student" table="student">
<cache usage="read-write" region="userIdcache"/>
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="address" type="string"/>
<property name="name" type="string"/>
</class>
</hibernate-mapping>
查询单个对象代码如下 (可以同步缓存):
Session session = MySessionFactory.getSession();
String id = request.getParameter("stuId");
if(id != null) {
Student stu = (Student) session.get(Student.class, Long.valueOf(id));
request.setAttribute("student", stu);
}
session.close();
查询全部对象时(不能同步缓存):
Session session = MySessionFactory.getSession();
Query query = session.createQuery(" from Student");
query.setCacheable(true);
List<Student> result = query.list();
session.close();
删除时也不能同步:
String id = request.getParameter("id");
Student student = new Student();
student.setId(Long.valueOf(id));
Session session = MySessionFactory.getSession();
Transaction transaction = session.getTransaction();
transaction.begin();
session.delete(student);
transaction.commit();
session.close();
请高手指点一下,这是什么原因?