Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the significance of destroy() method in servlets lifecycle ?

 
Rohan Kayan
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the significance of destroy() method in servlets lifecycle ?
When there is a finalise() method with every java object.
 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rohan,

I would say that when the container calls the servlet's destroy method to make the servlet object elligible for garbage collection be it automatic through the scheduler or through finalize().

Regards,
Darya
 
Rohan Kayan
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
But it is still not clear , to make any object eligible for Garbage Collection we need only to set all the live reference of that object equal to null .Then what is the use of destroy method?


Rohan
 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question to you Rohan ,

who do you think calls the servlet's destroy() method ? And why ?

Regards,
Darya
 
Gouri Bargi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

As per the servlet specs, the destroy method must be called by the container when a servlet is removed from service. If any resources are acquired in the init() method, this is the method where where we should release them, the typical example being database connections.

The finalize method is called before an object is garbage collected. But unlike the destroy method, there is no guarantee that this method will be called before garbage collection.

- Gouri
 
Rohan Kayan
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for Reply , Now I got that .
 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rohan,

to answer my question to you for you . The Container is doing that for you. The same way like the Container creates the servlet for you it destroys it for you.

The Container is doing the whole lifecycle of a servlet from life to death.

So why do you want to reinvent the wheel

Regards,
Darya
 
Rohan Kayan
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Darya Akbari:
Rohan,

to answer my question to you for you . The Container is doing that for you. The same way like the Container creates the servlet for you it destroys it for you.

The Container is doing the whole lifecycle of a servlet from life to death.

So why do you want to reinvent the wheel

Regards,



Darya

Hi Darya Akbari ,
I don't want to reinvent the wheel . I just wanna know is there any significance of having a separate method when there is already a finalise method for every objects . But I got the reason behind this , as finalise is called by GC and even then its execution is not confirmed . So container needed another method method called destroy for all the cleaning job

.


Rohan
 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rohan,

I think you still didn't get it . At least your last explanation seems to me that way (forgive me when I am wrong ).

The destroy method is called by the container NOT BECAUSE THE GC DIDN'T DO ITS JOB OR BECAUSE TO KILL THE LAST SERVLET REMNANTS FROM MEMORY but because the container decides to do so in case of an container shut down for example.

Whether and when the finalize or GC works is under the control of your JVM.
The container just make his servlet objects (and all related objects) available (elligible) for GC without concern of whether your JVM start GC or not.

Regards,
Darya
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic