views:

247

answers:

3

Is there a way to disable caching of serialized object in Java?

I have this scenario:

  1. I have an object which is Serializable, I am serializing it, deserializing it, values are OK.
  2. On the same object, I am changing some value, I am serializing it, deserializing it, values are NOT OK, values are same as the first initially loaded.

Seems like the serializator is caching the values, or not?

Thanks

Copied this example from "fredrik" and adopted to my case:

public class SerialDeserial {
 public static void main(String[] args) {
  try {
   ChangingObject obj = new ChangingObject();
   obj.foo=1;
   // Write it
   ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("test.foo"));
   os.writeObject(obj);
   os.flush();os.close();

   // Read the object
   ObjectInputStream is = new ObjectInputStream(new FileInputStream("test.foo"));
   ChangingObject objDummy = (ChangingObject)is.readObject();
   System.out.println("objDummy.foo is "+objDummy.foo);

   // Change it
   obj.foo=2;
   // Write it
   os = new ObjectOutputStream(new FileOutputStream("test.foo"));
   os.writeObject(obj);
   os.flush();os.close();

   // Read the object
   is = new ObjectInputStream(new FileInputStream("test.foo"));
   objDummy = (ChangingObject)is.readObject();
   System.out.println("objDummy.foo is "+objDummy.foo); // this returns "1" insted of "2"


  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

class ChangingObject implements Serializable {
 public int foo;
}
A: 

Serializer has no cache.

You should show a minimal example how to repeat you case.

stepancheg
A: 

stepancheg is right, are you sure you're not rereading the first serialized object?

The following sample works. If you can create something similar that doesn't, please post it here. import java.io.*;

public class SerialDeserial {
  public static void main(String[] args) {
    try {
      ChangingObject obj = new ChangingObject();
      obj.foo=1;
      // Write it
      ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("test.foo"));
      os.writeObject(obj);
      os.flush();os.close();

      // Read the object
      ObjectInputStream is = new ObjectInputStream(new FileInputStream("test.foo"));
      obj = (ChangingObject)is.readObject();
      System.out.println("obj.foo is "+obj.foo);

      // Change it
      obj.foo=2;
      // Write it
      os = new ObjectOutputStream(new FileOutputStream("test.foo"));
      os.writeObject(obj);
      os.flush();os.close();

      // Read the object
      is = new ObjectInputStream(new FileInputStream("test.foo"));
      obj = (ChangingObject)is.readObject();
      System.out.println("obj.foo is "+obj.foo);


    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

class ChangingObject implements Serializable {
  public int foo;
}

Edit: If I take your altered (well, not really) example I still get a correct output (with 2 instead of as the second output). You suggestion to do reset should make no difference as you are reopening the file and starting from the beginning both when reading and writing. I think your problems lies elsewhere (what OS are you using?, which file system? etc.) It should just work, sorry.

Fredrik
+2  A: 

ObjectOutputStream.reset.

You can also write the object with writeUnshared, but that is shallow so referenced objects will still be shared.

Of course, immutable objects are a win as usual.

Tom Hawtin - tackline
Agree, but he says he gets the error even with the sample source posted and then it is a new Stream and it should have no state kept.
Fredrik
You're probably being too literal - although I don't know if the mistake was in the output or input side of things.
Tom Hawtin - tackline