Re: serialization issue

From: Tomi NA (hefes..mail.com)
Date: Sat May 20 2006 - 05:24:39 EDT

  • Next message: Tomi NA: "Re: serialization issue - SOLVED"

    On 5/18/06, Andrus Adamchik <andru..bjectstyle.org> wrote:

    > A simple trick would be to use thread-bound DC and create
    > "readResolve" method in a custom DO superclass that reattaches itself
    > to the thread context (or rather returns a substitute object local to
    > the current DC):
    >
    > private Object readResolve() throws ObjectStreamException {
    > DataContext dc = DataContext.getThreadDataContext();
    > return dc == null ? this : dc.localObject(getObjectId(), this);
    > }

    This brought me closer to the solution, but I'm not there yet.
    Here's what I did.
    My dc is in a public static field instead of the current thread - I'm
    not sure I can depend on always being in the same thread and I'd like
    to use the same dc during the runtime of the app.
    I've extended CayenneDataObject along the lines you suggested:

    public class SerializableCayenneDataObject extends CayenneDataObject {

        public SerializableCayenneDataObject() {
        }

        private Object readResolve() throws ObjectStreamException {
         DataContext dc = Misc.context; // not from the current thread
         return dc == null ? this : dc.localObject(getObjectId(), this);
       }
    }

    I've regenerated the generated superclasses so that they extend the
    SerializableCayenneDataObject instead of CayenneDataObject.
    Alas, the breakpoint I placed inside the readResolve method never gets
    called and I still get an object with null values for all fields
    instead of the object I stored.

    I tested then if the readResolve code would work anyway and included
    it into the readExternal() deserialization method. This is what it
    looks like:

        public void readExternal(ObjectInput in) throws IOException,
    ClassNotFoundException {
            try {
                MyEntityDataObject medo = (MyEntityDataObject)in.readObject();
                medo = (MyEntityDataObject)
    Misc.dataContext.localObject(medo.getObjectId(), medo);
                System.out.println(medo.getName());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        public void writeExternal(ObjectOutput out) throws IOException {
            out.writeObject((MyEntityDataObject)MyEntityDataObject.dohvatiPopis(Misc.dataContext).get(0));
        }

    Anyway, this works. I get a medo object with the information I stored
    using writeExternal. What I really need now is a way to make the
    DataContext.localObject call transparent i.e. have it called
    internally in my dataObject by extending my custom DataObject. Can
    anyone tell me what I did wrong?



    This archive was generated by hypermail 2.0.0 : Sat May 20 2006 - 05:25:13 EDT