roc230 / spymemcached

Automatically exported from code.google.com/p/spymemcached
0 stars 0 forks source link

DeSerializing issue #155

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What version of the product are you using? 2.5.jar 
On what operating system? Linux 64 bit

Tell me more...

I put the memcached-2.5.jar under /tomcat/lib/
It worked fine for puting the object to the cache. But got the following error 
when trying to get the object. The DATA model object is located in the same web 
application but a different package. What went to wrong? Any idea on solving 
this issue? Thanks a lot. 

------------------------------------------------------
Instance: org.psisb.kb.spymemcache.KBCache@5ca3ce3f
2010-10-08 21:23:10.236 WARN 
net.spy.memcached.transcoders.SerializingTranscoder:  Caught CNFE decoding 690 
bytes of data
java.lang.ClassNotFoundException: org.kb.ws.summary.model.Data
        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.readObject(ObjectInputStream.java:351)
        at net.spy.memcached.transcoders.BaseSerializingTranscoder.deserialize(BaseSerializingTranscoder.java:100)
        at net.spy.memcached.transcoders.SerializingTranscoder.decode(SerializingTranscoder.java:66)
        at net.spy.memcached.transcoders.TranscodeService$1.call(TranscodeService.java:36)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
        at java.util.concurrent.FutureTask.run(FutureTask.java:138)
        at net.spy.memcached.transcoders.TranscodeService$Task.run(TranscodeService.java:83)
        at net.spy.memcached.transcoders.TranscodeService$Task.get(TranscodeService.java:69)
        at net.spy.memcached.internal.GetFuture.get(GetFuture.java:38)
        at net.spy.memcached.MemcachedClient.get(MemcachedClient.java:917)
        at net.spy.memcached.MemcachedClient.get(MemcachedClient.java:939)

Original issue reported on code.google.com by taowend...@gmail.com on 9 Oct 2010 at 1:46

GoogleCodeExporter commented 9 years ago
i use grails 1.3.7,memcached-2.5.jar,
domain class implements Serializable.
it will show this warn ..

Original comment by young.ji...@gmail.com on 24 Mar 2011 at 2:12

GoogleCodeExporter commented 9 years ago
Hi,
I had this issue in the past and I found a solution. The occurs because the 
memcached client it is loaded using a classloader and the serialized object 
class is loaded using another classloader. To fix this you must pass to the 
memcahed client connector factory a custom transcoder.

public class CustomSerializingTranscoder extends SerializingTranscoder{

    @Override
    protected Object deserialize(byte[] bytes) {
        final ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
        ObjectInputStream in = null;
        try {
            ByteArrayInputStream bs = new ByteArrayInputStream(bytes);
            in = new ObjectInputStream(bs) {
                @Override
                protected Class<?> resolveClass(ObjectStreamClass objectStreamClass) throws IOException, ClassNotFoundException {
                    try {
                        return currentClassLoader.loadClass(objectStreamClass.getName());
                    } catch (Exception e) {
                        return super.resolveClass(objectStreamClass);
                    }
                }
            };
            return in.readObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            closeStream(in);
        }
    }

    private static void closeStream(Closeable c) {
        if (c != null) {
            try {
                c.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Original comment by octavr@gmail.com on 27 Oct 2011 at 8:06

GoogleCodeExporter commented 9 years ago
Nope, did not work for me. I changed 
Object value = cache.get(key) ;
To
Object value = cache.get(key, SuperCache.customTranscoder) ;
And below is the code for CustomSerializingTranscoder
What am I doing wrong. The exception is crazy, its in completely different part 
of the code.

import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;

import net.spy.memcached.transcoders.SerializingTranscoder;

public class CustomSerializingTranscoder extends SerializingTranscoder{

    @Override
    protected Object deserialize(byte[] bytes) {
        final ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
        ObjectInputStream in = null;
        try {
            ByteArrayInputStream bs = new ByteArrayInputStream(bytes);
            in = new ObjectInputStream(bs) {
                @Override
                protected Class<ObjectStreamClass> resolveClass(ObjectStreamClass objectStreamClass) throws IOException, ClassNotFoundException {
                    try {
                        return (Class<ObjectStreamClass>) currentClassLoader.loadClass(objectStreamClass.getName());
                    } catch (Exception e) {
                        return (Class<ObjectStreamClass>) super.resolveClass(objectStreamClass);
                    }
                }
            };
            return in.readObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            closeStream(in);
        }
    }

    private static void closeStream(Closeable c) {
        if (c != null) {
            try {
                c.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Original comment by mcon...@gmail.com on 30 Nov 2011 at 5:44

GoogleCodeExporter commented 9 years ago
I had the same issue on Glassfish 2.1 
and the comment 2 worked like a charm!

Thank you

Original comment by simao...@gmail.com on 11 Jan 2013 at 3:31

GoogleCodeExporter commented 9 years ago
To save you some time searching about how you will apply the custom class in 
comment # 2 ... here's some sample code which shows that you can just add
.setTranscoder(new CustomSerializingTranscoder())
when you're building the client:

MemcachedClient mc =
new MemcachedClient(
new ConnectionFactoryBuilder()
.setTranscoder(new CustomSerializingTranscoder()) // use this line
.setProtocol(ConnectionFactoryBuilder.Protocol.BINARY)
...

Original comment by pulkitsi...@gmail.com on 5 Feb 2013 at 10:05

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
If you are using spymemcached alone (without MSM), just place the 
spymemcached.jar in the WEB-INF/lib folder inside your webapp, and not in 
/$CATALINA_HOME/lib/ and it will be fine.

Solution 2 + 5 works, but if possible just moving the jar is preferable.

Original comment by tomcat.r...@gmail.com on 28 Aug 2013 at 8:17

GoogleCodeExporter commented 9 years ago
#2 worked for me!

Original comment by thysmich...@gmail.com on 1 Mar 2014 at 4:22

GoogleCodeExporter commented 9 years ago
#2 answer is correct. I fix this bug follow his instruction. Thank you very 
much~^0^~

Original comment by davidjef...@gmail.com on 17 Apr 2014 at 3:46

GoogleCodeExporter commented 9 years ago
i had the same issue and comment #7 worked for me Thanks

Original comment by dj8ap...@gmail.com on 31 Aug 2014 at 7:28

GoogleCodeExporter commented 9 years ago
Nope, didn't work for me. I use 1 memcached with multi project

Original comment by ntric...@gmail.com on 9 Oct 2014 at 4:04