-1

I'm having trouble with ehcache trying to cache a table which is bigger than the storage I set to it. Although the application do not fail (because it end up performing the query in the database and returning the data) my log is getting full of ClassCastException.

I don't want to change the settings because it will happen again, I'd like to catch the ClassCastException but it did not work. (I tried filtering the exception with a seam component and at a specific point where the exception is thown)

Versions are: Seam 2.2.2, Hibernate 3.3.3 and ehCache 1.5

ERROR [user:] [DiskStore.java/get] – com.milestone.model.PersonItemCache: 
Could not read disk store element for key com.milestone.model.PersonItem#438480. 
Error was net.sf.ehcache.Element cannot be cast to net.sf.ehcache.Element
java.lang.ClassCastException: net.sf.ehcache.Element cannot be cast to net.sf.ehcache.Element
at net.sf.ehcache.store.DiskStore.loadElementFromDiskElement(DiskStore.java:302)
at net.sf.ehcache.store.DiskStore.get(DiskStore.java:257)
at net.sf.ehcache.Cache.searchInDiskStore(Cache.java:1202)
at net.sf.ehcache.Cache.get(Cache.java:803)
at org.hibernate.cache.EhCache.get(EhCache.java:80)
at org.hibernate.cache.ReadWriteCache.put(ReadWriteCache.java:178)
at org.hibernate.cache.impl.bridge.EntityAccessStrategyAdapter.putFromLoad(EntityAccessStrategyAdapter.java:68)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:179)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:877)
at org.hibernate.loader.Loader.doQuery(Loader.java:752)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2019)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:59)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:587)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:83)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1744)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:366)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108)
at org.hibernate.collection.AbstractPersistentCollection.readElementExistence(AbstractPersistentCollection.java:164)
at org.hibernate.collection.PersistentBag.contains(PersistentBag.java:262)
at com.milestone.person.PersonItemController.addAsFans(PersonItemController.java:141)
  • The `Error was net.sf.ehcache.Element cannot be cast to net.sf.ehcache.Element` is pretty damning. As it is obviously false it seems likely that the only way the cast can fail is if there is a version issue - i.e. the two objects may be of the same class they are different because they come from different versions of ehcache. – OldCurmudgeon Dec 10 '14 at 17:21
  • I'm digging through this possibility but I could only reproduce this error (it was only in prod enviroment) by changing the settings to a lower storage. It is beacause when the cache data file gets too big the application can't get it as an ehCache element. That's why I'm trying to clear the cache file as this exception is thrown. –  Dec 10 '14 at 17:44

1 Answers1

2

Your exception is ClassCastException: net.sf.ehcache.Element cannot be cast to net.sf.ehcache.Element This is an indication of a bigger problem. You most probably have 2 ehcace core or related jars in classpath. Catching the exception is not a solution. The exception should not happen in the first place.

Nazgul
  • 1,892
  • 1
  • 11
  • 15
  • Thanks, it solved my problem. I was thinking of this error as the real problem and not as a consequence. As I said above, I could only reproduce it changing the settings but it was not the real problem. In fact it worked until now this way so I did not think it was a problem from resources. –  Dec 10 '14 at 18:04