• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between serializing an object and saving its state in the Database

 
Nittin singla
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a basic Question..What is the difference between serializing an object i.e extending serializable interface , then saving its state in some .ser file and saving the state of an object on Database??
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

In the case of serialisation, the whole object (its whole graph) is saved as a single file. If that object is a list, it is still one file. You can reconstruct that object from that single file. But you can't readily use that for anything else.
Storing the state in a database (unless you store the whole object as a BLOB of its serialised state) means you would have to use constructors and set methods to reconstruct it from the database. But its state inthe database can readily be used by anything else which uses the database.

Anybody else?
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:In the case of serialisation, the whole object (its whole graph) is saved as a single file. If that object is a list, it is still one file. You can reconstruct that object from that single file. But you can't readily use that for anything else.

I'd replace "file" with "sequence of bytes". You can use an in-memory byte[], use sockets, or as you said store the serialized object in the database as a BLOB. A file is only one form of storing a sequence of bytes.
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:I'd replace "file" with "sequence of bytes". You can use an in-memory byte[], use sockets, or as you said store the serialized object in the database as a BLOB. A file is only one form of storing a sequence of bytes.


Are they really an array of bytes or an array of octets? Sure, if we all are using Intel X86 architectures, it works. But if you have another architecture on the other end of your socket or network connection, then it gets more complex.

The short answer is if you save it into a DBMS package, it worries about a lot of the marshalling, conversion, big-endian, etc. stuff. If you just serialize it, you get to worry about all that stuff.

Also, there is a difference between storing the array of bytes/octets in a blob in the DBMS and actually storing the fields is columns of the DBMS. With the first, you can have serious problems when the format of the data changes, field types change, additional fields are added, etc. If you use the DBMS to manage that, you can write code that is robust against many reasonable changes to the data, and even get both upward and backward compatibility.

Java Serialized objects have real issues when you change the format of the complex object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic