Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

EPractice Labs final exam question 16

 
Bartender
Posts: 1924
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
About this question:


When the stateful session beans get passivated, the following activities should be done:
...
c. Non transient ,non serializable variables have to be set to null in the ejbPassivate() method.

The correct answer is c.



I think setting non-serializable variables null in ejbPassivate() or @PrePassivate method is a common practice, but not a must.
Also, the specification does not say non transient variables have to set to null.


//On p.71 of the specification:
The objects that are assigned to the instance's non-transient fields.... after PrePassivate method complete must be :
a Serializable object
a null
....


It means non-transient variables must be a serializable object or null in order to be serialized when the stateful bean is passivated.
But it does not mean non-transient have to be set to null in PrePassivate method.
 
Ranch Hand
Posts: 51
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Himai,

If an object is not serializable then it will give exception. The best way to test it is to try it by keeping a non-serializable object in your stateful session bean and let it be passivated and destroyed. I tried it once and i was able to see exception in the server log file.


So technically it is a must to make non-serializable object to 'null'
 
Himai Minh
Bartender
Posts: 1924
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really? The container will throw an exception if the object is not serializable?
According to the spec, the container may be able to serialize some non-serializable objects. Or, the container will use externalization to serialize the non-serializable object.
Setting non-serializable object to null before the stateful bean passivated is just a good practice, but not a must.
 
Kaxhif Khan
Ranch Hand
Posts: 51
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


According to the spec, the container may be able to serialize some non-serializable objects. Or, the container will use externalization to serialize the non-serializable object.
Setting non-serializable object to null before the stateful bean passivated is just a good practice, but not a must.



Its getting confusing for me, here is what i have done





Here is stack trace from the server log file


Caused by: java.io.IOException
at com.sun.ejb.EJBUtils.serializeObjectFields(EJBUtils.java:746)
at com.ejb._BookServiceImpl_Serializable.writeObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1495)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1431)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1177)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1547)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1508)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1431)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1177)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
at com.sun.ejb.base.io.IOUtils.serializeObject(IOUtils.java:95)
... 49 more
Caused by: java.io.NotSerializableException: com.domain.Employee
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
at com.sun.ejb.EJBUtils.serializeObjectFields(EJBUtils.java:744)
... 64 more





I got this behavior with GlassFish 3.1.2 but i was able to run same example correctly with GlassFish 4. I am a bit double minded which server to use.
 
Himai Minh
Bartender
Posts: 1924
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the spec, p.73:


While the container is not required to use the Serialization protocol for the Java programming language to store the state of a passivated session instance, it must achieve the equivalent result.



You can refer to this:
http://www.enthuware.com/forum/viewtopic.php?f=4&t=861
Pay attention to what the admin wrote:


This is basic programming stuff.
1. Serialization doesn't necessarily require implementing Serializable. One can use Externalizable also or have a custom serialization mechanism.



Also, refer to this thread:
https://coderanch.com/t/649564/java-EJB-SCBCD/certification/container-serializes-EntityManager-SessionContext-serializable

Pay attention to Frits' reply:



how can the container serialize those fields which do not implement Serializable ?




Good question for the EJB-container developers. However in the relation to the exam this knowledge is not needed.



Glassfish mandates the objects like Employee to be serializable. But the spec does not mandate it.
 
Kaxhif Khan
Ranch Hand
Posts: 51
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Glassfish mandates the objects like Employee to be serializable. But the spec does not mandate it.



That's the point, specification allowed containers to choose different implementation for e.g. glassfish gives an error but other containers may not.

If one got a question in exam like this, what answer he/she should choose ?
 
eat bricks! HA! And here's another one! And a tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic