Results 1 to 19 of 19
  1. #1
    Cbani is offline Member
    Join Date
    Jan 2010
    Posts
    90
    Rep Power
    0

    Default Java Serialization

    The Default Mechanism of serialization
    Let's start with the basics. To persist an object in Java, we must have a persistent object. An object is marked serializable by implementing the java.io.Serializable interface, which signifies to the underlying API that the object can be flattened into bytes and subsequently inflated in the future.

    Let's look at a persistent class we'll use to demonstrate the serialization mechanism:

    10 import java.io.Serializable;
    20 import java.util.Date;
    30 import java.util.Calendar;
    40 public class PersistentTime implements Serializable
    50 {
    60 private Date time;
    70
    80 public PersistentTime()
    90 {
    100 time = Calendar.getInstance().getTime();
    110 }
    120
    130 public Date getTime()
    140 {
    150 return time;
    160 }
    170 }


    From this above description and code snippet what i would like to know is what actually happens when i make my class serializable; though there is no read or write object is happening..to be frank in my experience i have done the same lot of time to prevent my application giving me a ConcurrentModificationException. But not yet know what exactly technically happens...
    Would request to have a simple ellaborated explanation...

  2. #2
    Cbani is offline Member
    Join Date
    Jan 2010
    Posts
    90
    Rep Power
    0

    Default

    any quick replies?

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Nothing is happening really if you are not serializing the objects.
    Serialization has nothing to do with ConcurrentModificationException.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

  5. #5
    Cbani is offline Member
    Join Date
    Jan 2010
    Posts
    90
    Rep Power
    0

    Default

    then why is that said as a best practice...
    while writing a POJO
    1. Private attributes
    2. setters & getters
    3. should implement serializable

    so, if i miss the third point while writing a pojo; there should not be any impact on my application?

  6. #6
    Cbani is offline Member
    Join Date
    Jan 2010
    Posts
    90
    Rep Power
    0

    Default

    masijade,

    Discover the secrets of the Java Serialization API
    Above is the link from where i was referring and got the doubt...

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Where did you get this "best practices" list from? Those might be "best practices" when using them in Web/Enterprise Applications, and not bad ideas otherwise, but non of them are "required", in any case.

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by Cbani View Post
    masijade,

    Discover the secrets of the Java Serialization API
    Above is the link from where i was referring and got the doubt...
    What "doubt"? If you read that entire document you will know everything you "need" to know about serialisation (which of course has nothing to do with ConcurrentModification exceptions, maybe you meant "synchronisation"?).

  9. #9
    Cbani is offline Member
    Join Date
    Jan 2010
    Posts
    90
    Rep Power
    0

    Default

    Where did you get this "best practices" list from? Those might be "best practices" when using them in Web/Enterprise Applications, and not bad ideas otherwise, but non of them are "required", in any case.

    So, then why is it required to do so in a web application?

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by Cbani View Post
    Quote Originally Posted by masijade View Post
    Where did you get this "best practices" list from? Those might be "best practices" when using them in Web/Enterprise Applications, and not bad ideas otherwise, but non of them are "required", in any case.
    So, then why is it required to do so in a web application?
    Uhm

    Quote Originally Posted by masijade View Post
    but non of them are "required", in any case.
    "best practices" are suggestions (recommendations) not requirements. But, in any case, because they are needed if you wish to use your class as a Bean.

  11. #11
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    It's not required in a web app, but anything that you want to persist across servers (ie something stuck in a session that might get transferred between servers in a distributed architecture) needs to be serliazable, since the object will be serialized to be sent between the servers. Consequently it is usually a good idea to make your POJOs (or Beans or whatever terminology to want for your business data model) serializable.

  12. #12
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Do read the Serialization article fully.
    No one said it is required in web applications. In some applications objects are transmitted between clients and servers. For this to work the objects are Serialized first to make the transmission possible. In Enterprise environments, Entities must be serializable too.

    Objects that must be Serializable are explicitly stated in the environment specifications that govern how those applications are created.

  13. #13
    Cbani is offline Member
    Join Date
    Jan 2010
    Posts
    90
    Rep Power
    0

    Default

    for me its a web application and i am passing the object as method arguments. Also when we write a POJO to represent a table in hibernate; in both the cases we make the beans/pojo serialized. Now as nowhere we do a write/read object, still we make it serialized. so what is the use here. as previously mentioned its required for some session related stuffs while transffering across servers, if thats the case please ellaborte.

    Sorry for asking the same question three times.

  14. #14
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by Cbani View Post
    for me its a web application and i am passing the object as method arguments. Also when we write a POJO to represent a table in hibernate; in both the cases we make the beans/pojo serialized. Now as nowhere we do a write/read object, still we make it serialized. so what is the use here. as previously mentioned its required for some session related stuffs while transffering across servers, if thats the case please ellaborte.

    Sorry for asking the same question three times.
    Learn to ask better questions. If it has to do with why hibernate objects need to be serialisable, I would ask the hibernate developers, as for beans, it is because they may be serialised under varying conditions (depending on your application infrastructure).

  15. #15
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by Cbani View Post
    for me its a web application and i am passing the object as method arguments. Also when we write a POJO to represent a table in hibernate; in both the cases we make the beans/pojo serialized. Now as nowhere we do a write/read object, still we make it serialized. so what is the use here. as previously mentioned its required for some session related stuffs while transffering across servers, if thats the case please ellaborte.

    Sorry for asking the same question three times.
    You don't Serialize yourself but hibernate does. Like I said above, the environment tells you which objects must be serializable so that the container and the other libraries can use your classes appropriately.

    P.S It is better to read about these things before trying to write hibernate applications.

  16. #16
    Cbani is offline Member
    Join Date
    Jan 2010
    Posts
    90
    Rep Power
    0

    Default

    Learn to ask better questions. If it has to do with why hibernate objects need to be serialisable, I would ask the hibernate developers, as for beans, it is because they may be serialised under varying conditions (depending on your application infrastructure).

    i just stated an example. my intention is abt POJOs. you better dont reply to any of the threads if you cant.

  17. #17
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Also, if you really think you need to know all possible cases under which something might be serialised, then for Hibernate, ask the hibernate developers, for your web container/application server, read it's docu (and maybe ask its developers) and for enterprise applications in general, read the JEE specifications.

  18. #18
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by Cbani View Post
    Quote Originally Posted by masijade View Post
    Learn to ask better questions. If it has to do with why hibernate objects need to be serialisable, I would ask the hibernate developers, as for beans, it is because they may be serialised under varying conditions (depending on your application infrastructure).
    i just stated an example. my intention is abt POJOs. you better dont reply to any of the threads if you cant.
    I can, of course, but seemingly don't know how to ask them. What, exactly, are you asking, here? What, exactly, do you want to know?

  19. #19
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    Quote Originally Posted by Cbani View Post
    for me its a web application and i am passing the object as method arguments. Also when we write a POJO to represent a table in hibernate; in both the cases we make the beans/pojo serialized. Now as nowhere we do a write/read object, still we make it serialized. so what is the use here. as previously mentioned its required for some session related stuffs while transffering across servers, if thats the case please ellaborte.

    Sorry for asking the same question three times.
    IN the first case, if they aren't held in session, then they don't have to be serialised...but expect to be bitten if you think like that because, for sure, someone will, someday, try and stick that bean in a session on a distributed system...and it'll fail.

    In the second case (Hibernate), it's because Hibernate expects classes it works with to be serializable. That's all you need to know. From that, you can guess that Hibernate may need to write these things somewhere...sometime. Who cares?

Similar Threads

  1. Serialization
    By vijay24805 in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-10-2009, 09:16 PM
  2. Java serialization
    By paul in forum Advanced Java
    Replies: 3
    Last Post: 04-10-2009, 08:58 PM
  3. about serialization
    By bishnu in forum New To Java
    Replies: 0
    Last Post: 12-19-2008, 09:13 AM
  4. Need help using serialization
    By xcallmejudasx in forum New To Java
    Replies: 0
    Last Post: 12-02-2008, 08:23 PM
  5. Replies: 0
    Last Post: 04-04-2008, 02:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •