Your browser does not support our blog javascript

serializable

search for more blogs here

 
Home - Take this blog! - Get your Author's Pass Here - Submit Comments Below

Serialization in Java/J2EE - Demystified

Posted by ~Ray @ 2008-11-13 12:19:43


Serialization:Serialization is the process of persisting the state of an object. In Java serializing an object involves encoding its state in a structured way within a byte array. Once an object is serialized the byte array can be manipulated in various ways; it can be written to a file sent over a network or RMI or persisted within a database as a BLOB. The serialization process encodes enough information about the object type within the byte stream allowing the original object to be easily recreated upon deserialization at a later point in time. Serialization and Deserialization is same as marshaling and unmarshaling in C/C++. Where to keep serialized object:Serialization is a highly versatile mechanism it should be addressed by storing the state in relational or object databases. It does not have transaction management and concurrency control. Nor does it provide any typical database features like indexed access caching and a query language. Serializing static variables: Variables declared as static members are not considered part of the state of an object because they are shared by all instances of that class. Classes which need to preserve the value of static members during serialization should save and restore these values explicitly using private void readObject(ObjectInputStream) and private void writeObject(ObjectOutputStream). Advantages and Disadvantages:The advantages of serialization are: My subclass implements Serializable but my superclass doesn't. Both subclass and superclass contain instance variables that need to be saved as part of the state of the subclass. Will serialization save the superclass fields for me?When you serialize an object the serialization mechanism works by chaining up the inheritance hierarchy saving the sate of each Serializable superclass in turn. When serialization reaches the first non-serializable superclass the serialization stops. When deserializing the state of this first non-serializable superclass is restored not from the stream but by invoking that class' no-argument constructor. If the no-argument constructor is not adequate for your purposes you must customize the serialization of your subclass with writeObject() and readObject() in order to write out and restore any information from the non-serializable superclass that you find necessary. Externalizable instead of Serializable:By implementing Externalizable yourself you can win performance at the cost of flexibility and extra code to maintain. If you implement Externalizable yourself you stream the data directly without the need for reflection which is used in the case of SerializableRead and write serialized objects to and from a database:If your RDBMS supports them you can store serialized objects as BLOBs. These are JDBC 2.0 features but take a look at java sql. Blob. ResultSet and PreparedStatement for more information. Collections like Vector or a Hashtable:Collections like Vector and Hashtable both implements Serializable and have been designed for serialization. One thing to keep in mind is to watch out for is that in order to serialize a collection like Vector or Hashtable you must also be able to serialize all of the objects contained in these collections. Otherwise the collection would not be able to be completely restored. Your program will throw a NotSerializableException unless all objects stored in the Vector or Hashtable are also serializable. Other way to save the state of an object of a class which does not implement Serializable or ExtenalizableWrite the value of each and every instance variable into the persistent storage. If there are 50 variables you will have to store each of them individually. If some of the variables are object references you will have to follow each reference and save the state of that object as well. You can do that but it would be a proprietary solution and each class that wanted to read your object would also have to know all about your proprietary format. Role of serialization in RMI:RMI uses serialization as its basic and only mechanism for sending objects across a network. If an object implements java rmi. Remote then the object's stub is serialized and sent to the client. If the object implements java io. Serializable then the object itself is serialized and sent. Role of serialization in EJB:A big part of EJB is that it is a framework for underlying RMI: remote method invocation. You're invoking methods remotely from one JVM space 'A' on objects which are in JVM space 'B' -- possibly running on another machine on the network. To make this happen all arguments of each method call must have their current state plucked out of JVM 'A' memory flattened into a byte stream which can be sent over a network connection and then deserialized for reincarnation on the other end in JVM 'B' where the actual method call takes place. If the method has a return value it is serialized up for streaming back to JVM 'A.' Thus the requirement that all EJB methods arguments and return values must be serializable. The easiest way to do this is to make sure all your classes implement java io. Serializable.[ADVERTHERE]Related article:
http://java-polis.blogspot.com/2007/11/serialization-in-javaj2ee-demystified.html


0 Comments:


No comments have been posted yet!

From:   Website:
Subject:   Code:
Message:


   

 


 

 

 





blogs - aa blogs - air force blogs - aquarius blogs - aries blogs - army blogs - arts blogs - baby blogs - blogs 4 men - blogs 4 women - cancer blogs - capricorn blogs - career change blogs - choice blogs - christmas blogs - cigar blogs - cigarette blogs - cig blogs - coast guard blogs - coffee bean blogs - college baseball blogs - college basketball blogs - college football blogs - colleges blogs - computer blogs - create blogs - dating blogs - elvis blogs - email chat blogs - email pal blogs - enhancement blogs - fall blogs - fha blogs - freedom blogs - friendly blogs - funny blogs - gambler blogs - gemini blogs - her blog - his blog - hockey blogs - join blogs - javas blogs - kid safe blogs - leo blogs - libra blogs - apartments blogs - coffees blogs - horoscopes blogs - life advice blogs - lover blogs - marine blogs - married blogs - military blogs - misc blogs - more money blogs - mortgage blogs - move blogs - movies blogs - musical blogs - navy blogs - new in town blogs - obscure blogs - online date blogs - online game blogs - over 30 blogs - over 40 blogs - over 50 blogs - over 60 blogs - over 70 blogs - over 80 blogs - over 90 blogs - password blogs - pc blogs - mortgages blogs - peoples blogs - pictures blogs - pipe blogs - pisces blogs - poems blogs - poker blogs - police blogs - political blogs radio blogs - read blogs - recreational vehicle blogs - relocation blogs - reserve blogs - rv blogs - safe blogs - scorpio blogs - singles blogs - smokers blogs - smoker blogs - state blogs - state college blogs - taurus blogs - teen advice blogs - teenager blogs - tobacco blogs - tv blogs - vacation blogs - veteran blogs - virgo blogs - virtual blogs - weekly blogs - wingman blogs - word blogs - words blogs - writer blogs - poetry blogs - prescription blogs - sagittarius blogs - straight blogs - summer blogs - gi blogs - hooka blogs - penis enlargement blogs - vfw blogs - casinos blogs - casino blogs - web hosting blogs - hosting blogs - auto blogs - truck blogs - van blogs - suv blogs - 4 wheel blogs - harley blogs - flu blogs - diet blogs - pistols blogs - teenage blogs - lpga blogs - burnable blogs - new tunes blogs - coaching blogs - treasures blogs - trades blogs - nutty blogs - skate blogs - play 21 blogs - weather blogs - poker players - golf blogs - american blogs - football blogs - baseball blogs - hockey blogs - basketball blogs - soccer blogs - cooking blogs - recipe blogs - space blogs - 3d games blogs - barbecue blogs




blogs home