Ehcache并发修改异常使用+Spring+Struts应用

Ehcache concurrent modification exception using + Spring + Struts application

在我的应用程序中,ehcache 配置如下。

AppDataRegion.java

//import statements.

public class AppDataRegion{

//Variable for region identifier.
private String appRegionId;

// constructor sets the appRegionId;
//obtained from the system current time.
public AppDataRegion(){
appRegionId = String.valueOf(System.currentTimeMillis());
}

//Variable for cachemanager
// injected by spring DI from context xml
Private CacheManager appCacheMngr;

//necessary getter / setter methods for variable cachemanager
//necessary getter / setter methods for variable appRegionId

//method to create a region in cache using the appRegionId value
private createRegion(){
  if (!appCacheMngr.cacheExists(appRegionId)){
    try {
        appCacheMngr.addCache(appRegionId);
    catch (exc){
    //exception handled
    }
   }
}

public Cache getRegion(){
if(appCacheMngr == null || !appCacheMngr.cacheExists(appRegionId)){
createRegion();
}
return appCacheMangr.getCache(appRegionId);
}
private createCustRegion(){
    try{
    Cache custCache = appCacheMngr.getCache(“custdiskCache”);
    If(null == custCache.addCache(custCache);
    }catch (exp){
    //handled the exceptions
}
retrun appCacheMngr.getCache(“custdiskCache”);
}
}

Spring配置

<bean id="appDataStoreService" class="com.app.cache.AppDataStoreService" >      
        <property name="appDataStoreRegion" ref="appDataStoreRegion"/>
     </bean>

     <bean id="appDataStoreRegion" class="com.app.cache.AppDataStoreRegion">        
        <property name="appcacheManager" ref="cacheManager"/>
     </bean>

<bean id="cacheManager" class="net.sf.ehcache.CacheManager" factory-method="create">       
        <constructor-arg index="0" type="java.net.URL" value="classpath:ehcache-app.xml" />
    </bean>

//应用数据存储区域有一个服务层。

public class AppDataStoreService{

//datastoreregion variable declaration
private AppDataStoreRegion appDataStoreRegion;


public void storeCustObjInCache(String id, Object obj){
    Cache region = appDataStoreRegion.getCustRegion();
    If(region != null && id !=null && region.isElementInMemory(id)){
    region.remove(id);
}
Element ele = new Element(id, obj);
If(region != null) region.put(ele);
}
}

在 DTO 对象中填充数据后,在应用程序中我调用 appDataStoreService class 的 storeCustObjInCache() 方法将内容写入磁盘。

ehcache 配置为单例而不是实例。

Web 应用程序使用 struts (2.3.20) 框架进行 Web 流,使用 Spring 框架 (4.1.2) 进行 DI 对象管理。

另外JSP为了UI我们迭代使用bean对象,它有列表来显示内容。

我们从 1.2.2 迁移到 ehcache 2.9.0,只是在 spring xml 上下文中更改了 jar 和配置。迁移后,我们开始经常收到以下异常。

net.sf.ehcache.CacheException: Failed to serialize element due to ConcurrentModificationException. This is frequently the result of inappropriately sharing thread unsafe object (eg. ArrayList, HashMap, etc) between threads at net.sf.ehcache.store.disk.DiskStorageFactory.serializeElement(DiskStorageFactory.java:405) at net.sf.ehcache.store.disk.DiskStorageFactory.write(DiskStorageFactory.java:385) at net.sf.ehcache.store.disk.DiskStorageFactory$DiskWriteTask.call(DiskStorageFactory.java:477) at net.sf.ehcache.store.disk.DiskStorageFactory$PersistentDiskWriteTask.call(DiskStorageFactory.java:1071) at net.sf.ehcache.store.disk.DiskStorageFactory$PersistentDiskWriteTask.call(DiskStorageFactory.java:1055) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1(ScheduledThreadPoolExecutor.java:180) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: java.util.ConcurrentModificationException at java.util.ArrayList.writeObject(ArrayList.java:766) at sun.reflect.GeneratedMethodAccessor8008.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988) at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1496) at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432) at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178) at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548) at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509) at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432) at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178) at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548) at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:441) at net.sf.ehcache.Element.writeObject(Element.java:875)

我的理解是,当 ehcache 试图写入磁盘时,另一个线程正在修改序列化 list.I 无法找出哪个线程及其可能性。

在 spring.

中是否有一个示例说明如何像这样以编程方式使用而无需注释的情况下使用 ehcache

以及关于如何识别导致此问题的线程的任何见解

你有两个选择:

  1. 找到缓存后改变任何对象的所有代码路径
  2. 确保缓存的内容是不可变的

如果您能以这种方式重构您的应用程序,我会赞成选项 2。

这个问题绝对不是由连接不同参与者对象的方式引起的,顺便说一句,在提供的示例中没有使用注释。