ClassNotFoundException with EHCache-Atomikos
I'm trying to use EHCache's transactional abilities together with Atomikos and Apache Tomcat (for usage with JMS and Hibernate but that's just to assure the sceptics I really need JTA).
I'm currently working on a small POC where I'm trying to put an instance of MyValue
which implements Serializable
and when I try to commit the transaction I get the __ with ClassNotFoundException
as the root (The stack trace is at the end).
When I jar the MyValue
file and put it under %TOMCAT_HOME%\lib I get no exception.
It's important to add that due to an instruction I saw on the atomikos tutorial both Atomikos's jars and EHCache jars are under %TOMCAT_HOME%\lib.
This is the quote:
When the Atomikos transaction manager is installed globally in Tomcat, you now must also install your JDBC driver at the same global location (ie: into the TOMCAT_HOME/lib folder). If you don't do that, you will get a NoClassDefFoundErrors or a ClassNotFoundException or even a ClassCastException during your web application deployment.
This is not a limitation of Atomikos nor of Tomcat but of the J2EE class loading design that both Tomcat and Atomikos must follow.
The problem is that I cannot have all my value classes under that folder, of course.
Have I misenterperted the instruction?
Has anyone encountered this issue?
EHCache.xml
<cache name="myCache"
maxElementsInMemory="1000"
eternal="true"
overflowToDisk="false"
diskPersistent="false"
memoryStoreEvictionPolicy="LRU"
transactionalMode="xa"
/>
Stack trace
net.sf.ehcache.CacheException: When configured copyOnRead or copyOnWrite, a Store will only accept Serializable values
at net.sf.ehcache.store.compound.SerializationCopyStrategy.copy(SerializationCopyStrategy.java:47)
at net.sf.ehcache.store.compound.Segment.potentiallyCopy(Segment.java:176)
at net.sf.ehcache.store.compound.Segment.create(Segment.java:342)
at net.sf.ehcache.store.compound.Segment.put(Segment.java:400)
at net.sf.ehcache.store.compound.CompoundStore.put(CompoundStore.java:132)
at net.sf.ehcache.transaction.StorePutCommand.execute(StorePutCommand.java:43)
at net.sf.ehcache.transaction.xa.VersionAwareWrapper.execute (VersionAwareWrapper.java:68)
at net.sf.ehcache.transaction.xa.EhcacheXAResourceImpl.prepareInternal(EhcacheXAResourceImpl.java:224)
at net.sf.ehcache.transaction.xa.TransactionXARequestProcessor$XARequestCallable.call(TransactionXARequestProcessor.java:150)
at net.sf.ehcache.transaction.xa.TransactionXARequestProcessor$XARequestCallable.call(TransactionXARequestProcessor.java:117)
at net.sf.ehcache.transaction开发者_开发知识库.xa.XAThreadPool$MultiRunner.run(XAThreadPool.java:115)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.ClassNotFoundException: com.myCompany.testJTA.MyValue
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:604)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1575)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1496)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1732)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1947)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:480)
at net.sf.ehcache.Element.readObject(Element.java:796)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1849)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at net.sf.ehcache.store.compound.SerializationCopyStrategy.copy (SerializationCopyStrategy.java:45)
... 16 more
It's important to add that due to an instruction I saw on the atomikos tutorial both Atomikos's jars and EHCache jars are under %TOMCAT_HOME%\lib
Classes from a web application (i.e. loaded by a WebappX classloader) aren't visible from the Common classloader (see the Apache Tomcat 6.0 - Class Loader HOW-TO ), which is why you get a CNFE.
Now, the question is, why do you put the EHCache JAR in the Common classloader? I don't think you need to. Package it at your web application level.
References
- Apache Tomcat 6.0 - Class Loader HOW-TO
精彩评论