EHC ❯ Transient attributes are not ignored
-
Bug
-
Status: Closed
-
2 Major
-
Resolution: Fixed
-
ehcache-core
-
-
ljacomet
-
Reporter: damienc
-
October 11, 2013
-
0
-
Watchers: 4
-
November 04, 2013
-
November 04, 2013
Attachments
Description
(Newbie issue maybe… ) When I add JavaBean in EhCache Cache, I get a java.io.NotSerializedException due to a particular attribute on my JavaBean. Indeed, it’s not serializable… but I set it as transient attribute in order to not include it in serialization process… It seems that EhCache does not ignore transient attribute. We have searched a long time about this issue, and have not found any good solution.
Is there a “usual” way to ignore some attributes in EhCache serialization process ?
Comments
Louis Jacomet Jacomet 2013-10-14
damien cuvillier 2013-10-15
OK. Here is required data * JavaBean * Service * EH Cache XML Config * Log Trace
public class EntityBean implements Serializable {
private static final long serialVersionUID = -3289049540217053566L;
@XStreamAsAttribute
private Integer id;
@XStreamAsAttribute
private String name;
@XStreamOmitField
@Autowired
protected transient ReaderFactory readerFactory;
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- Configuration du CACHE ehCache.
COnfiguration avec Spring possible également :
https://code.google.com/p/ehcache-spring-annotations/wiki/UsingTriggersRemove
Non utilisé (pour l'instant) car un comportement générique pour Restlet a été implémenté -->
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false">
<!--
| Please see http://ehcache.sourceforge.net/documentation/configuration.html for
| detailed information on how to configure caches in this file
+-->
<!-- Location of persistent caches on disk -->
<diskStore path="java.io.tmpdir/EhCacheSpringAnnotationsDataStoreApp" />
<defaultCache eternal="false" maxElementsInMemory="1000"
overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU"/>
<cache name="RESTLET_RESOURCE" eternal="false" maxElementsInMemory="500"
overflowToDisk="true" diskPersistent="true" timeToIdleSeconds="0"
timeToLiveSeconds="1" memoryStoreEvictionPolicy="LRU"
/>
<cache name="DATA_SERVICE" eternal="false" maxElementsInMemory="500"
overflowToDisk="true" diskPersistent="true" timeToIdleSeconds="0"
timeToLiveSeconds="1" memoryStoreEvictionPolicy="LRU"
/>
</ehcache>
My code works if I disable diskPersistent for DATA_SERVICE config. But I need this diskPersistent feature.
/** Récupère la liste complète des entités.
*
* @return Liste des BO
*/
public final List<EntityBean> getAll() {
String cacheKey = "ALL";
List<EntityBean> cachedEntities = this.getListFromCache(cacheKey);
if (cachedEntities == null) {
List<EntityModel> entities = this.getEntityDao().getAll();
cachedEntities = getMapper().convertModelToBean(entities);
this.addCacheResult(cacheKey, cachedEntities);
}
return cachedEntities;
}
public final List<TBO> getListFromCache(final String cacheKey) {
try {
return (List<TBO>) this.getCache().get(beanClassName.getName() + cacheKey).getObjectValue();
} catch (ClassCastException cce) {
logger.fatal("Unable to get Collection from Cache with key "
+ beanClassName.getName() + cacheKey
+ ". Cached data is not a java collection");
return null;
} catch (NullPointerException npe) {
return null;
}
}
12:06:44 ERROR Disk Write of fr.stratonit.datastore.services.io.reader.EntityReaderALL failed (it will be evicted instead): (net.sf.ehcache.store.compound.factories.DiskStorageFactory$DiskWriteTask.call:418)
java.io.NotSerializableException: fr.stratonit.datastore.services.io.reader.ReaderFactory
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1181)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1541)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1506)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1429)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1175)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1541)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1506)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1429)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1175)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
at java.util.ArrayList.writeObject(ArrayList.java:710)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1429)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1175)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1541)
at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:439)
at net.sf.ehcache.Element.writeObject(Element.java:791)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1429)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1175)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
at net.sf.ehcache.util.MemoryEfficientByteArrayOutputStream.serialize(MemoryEfficientByteArrayOutputStream.java:97)
at net.sf.ehcache.store.compound.factories.DiskStorageFactory.serializeElement(DiskStorageFactory.java:326)
at net.sf.ehcache.store.compound.factories.DiskStorageFactory.write(DiskStorageFactory.java:305)
at net.sf.ehcache.store.compound.factories.DiskStorageFactory$DiskWriteTask.call(DiskStorageFactory.java:411)
at net.sf.ehcache.store.compound.factories.DiskPersistentStorageFactory$PersistentDiskWriteTask.call(DiskPersistentStorageFactory.java:560)
at net.sf.ehcache.store.compound.factories.DiskPersistentStorageFactory$PersistentDiskWriteTask.call(DiskPersistentStorageFactory.java:546)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:292)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
Louis Jacomet Jacomet 2013-10-15
Can you give the version of Ehcache you are using? It says 2.7.4 in the Jira, but the logged message does not contain “(it will be evicted instead)” for quite some time.
damien cuvillier 2013-10-15
I use 2.7.2
My maven depency :
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<type>pom</type>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>com.googlecode.ehcache-spring-annotations</groupId>
<artifactId>ehcache-spring-annotations</artifactId>
<version>1.2.0</version>
</dependency>
Louis Jacomet Jacomet 2013-10-16
I attached a maven project with at simple test using Ehcache and a version of the class you provided showing that everything works fine.
It seems there is something else in your system causing the issue. Your stack trace is too deep in serialization calls for a collection of simple elements.
Also, you are not using version 2.7.2 but 2.4.5.
This is because in your maven dependency declaration you have a {{
Can you extend the attached project so that it fails? Without that, there is not much thing we can do on our side.
Can you provide a code sample that shows the issue, including the cache configuration? And the full stack trace for this error?
Ehcache serialization does not do any magic that would break transient behavior.