Search code examples
javaserializationcrashobjectinputstream

Java Serialization - Recovering serialized file after process crash


I have a following usecase.

  • A process serializes certain objects to a file using BufferedOutputStream.
  • After writing each object, process invokes flush()
  • The use case is that if the process crashes while writing an object, I want to recover the file upto the previous object that has been written successfully.

How can I deserialize such file? How will Java behave while deserializing such file.

  • Will it successfully deserialize upto the object that were written successfully before crash?
  • While reading the last partially written object, what will be the behavior. How can I detect that?

Update1 -

  • I have tried to simulate process crash via manually killing the process while objects are being written. I have tried around 10-15 times.Each time i am able to deserialize the file and file does not has any partial object.

I am not sure if my test is exhaustive enough and therefore need further advice.

Update2 - Adam had pointed a way which could simulate such test using truncating the file randomly. Following is the behavior observed for trying out around 100 iterations -

  • From the truncated file ( which should be equivalent to the condition of file when a process crashes), Java can read upto last complete object successfully.
  • Upon reaching the last partially written object, Java does not throw any StreamCorruptedException or IOException. It simply throws EOFException indicated EOF and ignores the partial object.

Solution

  • Yeah, testing such scenario manually (by killing the process) may be difficult. I would suggest writing a test case, where you :

    1. Serialize a set of objects and write them to a file .
    2. Open the file and basically truncate it at random position.
    3. Try to load and deserialize (and see what happens)
    4. Repeat 1. to 3. with several other truncate positions.

    This way you are sure that you are loading a broken file and that your code handles it properly.