Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClassCastException when using Timestamp within DataSerializable #6759

Closed
GhostInAMachine opened this issue Nov 17, 2015 · 5 comments

Comments

Projects
None yet
4 participants
@GhostInAMachine
Copy link

commented Nov 17, 2015

Running the code below results in ClassCastException when data is being read during deserialization. Checked with 3.6-EA.

public class TimestampIDS {

    public static void main(String[] args) {
        HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(new Config());
        IMap<Integer, Container> timestamps = hazelcastInstance.getMap("timestamps");
        timestamps.put(1, new Container(new Timestamp(1337)));
        Container c = timestamps.get(1);
        System.out.println(c.getTimestamp());
    }

    public static class Container implements DataSerializable {

        public Container() {
        }

        public Container(Timestamp timestamp) {
            this.timestamp = timestamp;
        }

        private Timestamp timestamp;

        public Timestamp getTimestamp() {
            return timestamp;
        }

        public void writeData(ObjectDataOutput out) throws IOException {
            out.writeObject(timestamp);
        }

        public void readData(ObjectDataInput in) throws IOException {
            timestamp = in.readObject();
        }
    }
}

@gurbuzali gurbuzali added this to the 3.6 milestone Nov 17, 2015

@pveentjer

This comment has been minimized.

Copy link
Member

commented Nov 19, 2015

I just tried it with master and it runs fine. From your example it isn't clear which package Timestamp comes from; so I used java.sql.Timestamp.

Can you rerun on master?

@pveentjer

This comment has been minimized.

Copy link
Member

commented Nov 19, 2015

Ahh..

Apparently it is resolved in 3.6 master. when I switch to 3.5.x I get the following exception:

Exception in thread "main" com.hazelcast.nio.serialization.HazelcastSerializationException: Problem while reading DataSerializable, namespace: 0, id: 0, class: 'com.hazelcast.Main$Container', exception: java.util.Date cannot be cast to java.sql.Timestamp
    at com.hazelcast.nio.serialization.DataSerializer.read(DataSerializer.java:124)
    at com.hazelcast.nio.serialization.DataSerializer.read(DataSerializer.java:39)
    at com.hazelcast.nio.serialization.StreamSerializerAdapter.read(StreamSerializerAdapter.java:41)
    at com.hazelcast.nio.serialization.SerializationServiceImpl.toObject(SerializationServiceImpl.java:276)
    at com.hazelcast.spi.impl.NodeEngineImpl.toObject(NodeEngineImpl.java:200)
    at com.hazelcast.map.impl.MapServiceContextImpl.toObject(MapServiceContextImpl.java:311)
    at com.hazelcast.map.impl.proxy.MapProxySupport.toObject(MapProxySupport.java:1135)
    at com.hazelcast.map.impl.proxy.MapProxyImpl.get(MapProxyImpl.java:82)
    at com.hazelcast.Main.main(Main.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Timestamp
    at com.hazelcast.Main$Container.readData(Main.java:45)
    at com.hazelcast.nio.serialization.DataSerializer.read(DataSerializer.java:111)
    ... 13 more

@asimarslan I believe you have been working in this area. Do you know exactly which PR fixed this bug and if it is going to be backported?

@asimarslan

This comment has been minimized.

Copy link
Member

commented Nov 20, 2015

@pveentjer the related PR is #6735
But it very difficult to backport this as it breaks binary compatibility of serialization.
The simplest solution is move Date.class to Constant serializers

@pveentjer

This comment has been minimized.

Copy link
Member

commented Nov 23, 2015

@asimarslan what should we do with this ticket? I'm fine if it can't be backported and since it is already solved in master, perhaps we should close this ticket?

@asimarslan

This comment has been minimized.

Copy link
Member

commented Nov 23, 2015

This issue is clone of #6455 and it is fixed in 3.6 so let's just close this one.

@asimarslan asimarslan closed this Nov 23, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.