Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Rules for the Bean class

 
Krishnan Y N
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

I am preparing for the SCBCD exam.. i have read both in the specs and in the HFE book that u should not have a finalize() method in your class.. Can anybody tell me why exactly this restriction is in place..

TIA
Krish
 
Severin Stoeckli
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where in spec? in which class?

Severin
 
Keerthi P
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally, you put clean-up code to clean up non-memory related resources in the finalize() method so that when the object is about to be garbage collected, those resources are gracefully released. Examples for non-memory related resources include File, Socket, etc.

In case of EJBs, the life-time of a bean instance is controlled by the container. i.e, the container decides when to instantiate, activate, passivate, remove and associate an instance to EJBObject (to client). So you cannot rely on the code that you may put in the finalize() method. In other words, you may want the finalizer to run when a bean is no longer needed by a client .i.e, the client has invoked remove(), but the container may move the instance back to pool, in which case the finalizer will not be run.

OK. How do we discard an instance in Java?

In plain Java , we assign someObject = null. The object now becomes eligible for GC, finalizer runs and the instance is killed and memory is recovered.

But in EJB world, assigning null doesn't simply work. The client must invoke the remove() method on the remote object, so that the container can free the instance. But this does not mean that the bean instance associate with the EJBobject will be GCed. The container may move the instance back to pool. So there is no guarantee that finalizer will be run when an EJB instance is discarded (removed) by a client.
 
Krishnan Y N
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Keerthi,

Thx for the reply.. understood why finalize should be be present...

Cheerz
krish
 
Krishnan Y N
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Sorry for the typo, i wanted to say "understood why finalize() should not be there".

So in case we see a Bean Class with a finalize() method would that be treated as a class that is not obeying the EJB spec ?

Cheerz
krish
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic