Calling servlet's destroy method
Asked Answered
P

8

19

As per the link http://www.xyzws.com/Servletfaq/when-is-destroy-of-servlets-called/20, one of the reason of calling destroy method is when the servlet hasn't got a request in a long time.

I was thinking there could be some pages that don't get called for a long time. So, does that mean destroy will be called and they will be no longer used?

Actually, I was asked this question in interview and he told me that destroy method will only be called when server is shut down.

Appreciate any help on this.

Petulia answered 18/11, 2012 at 3:41 Comment(1)
I don't think it's on you to kill a servlet. Shouldn't be your responsibility, that's why you're in a servlet container and a servlet is a managed resourceLarry
P
10

AFAIK,

In java servlet, destroy() is not supposed to be called by the programmer. But, if it is invoked, it gets executed. The implicit question is, will the servlet get destroyed? No, it will not. destroy() method is not supposed to and will not destroy a java servlet.

The meaning of destroy() in java servlet is, the content gets executed just before when the container decides to destroy the servlet. But if you invoke the destroy() method yourself, the content just gets executed and then the respective process continues. With respective to this question, the destroy() gets executed and then the servlet initialization gets completed.

destroy() method is invoked first, then Servlet is removed from the container and then eventually garbage collected. destroy() method generally contains code to free any resources like JDBC connection that will not be garbage collected.

Pavement answered 19/3, 2013 at 5:39 Comment(0)
I
5

Couple of cases :

  1. when the container shuts down or the application shuts down;
  2. when the container decides that there is a shortage of memory;
  3. when this servlet hasn't got a request in a long time.

As per the Doc

Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. This method is only called once all threads within the servlet's service method have exited or after a timeout period has passed. After the servlet container calls this method, it will not call the service method again on this servlet.

Invertase answered 19/11, 2014 at 12:48 Comment(0)
W
3

From the specification (The Servlet Interface -> Servlet Life Cycle -> End of Service):

Once the destroy method is called on a servlet instance, the container may not route other requests to that instance of the servlet. If the container needs to enable the servlet again, it must do so with a new instance of the servlet’s class.

The specification is referring to a particular instance of the servlet that is being destroyed.

So, does that mean destroy will be called and they will be no longer used?

It doesn't mean that the servlet whose instance is destroyed is never used. It's only that particular instance that is not used as it would have been garbage collected. When a request comes in for that servlet, the container would create a new instance of that servlet and performs all the life-cycle actions till serving the request, again.

Whet answered 18/11, 2012 at 3:57 Comment(0)
I
1

From Servlet Specification:-

destroy() method is called by the servlet container to indicate to a servlet that the servlet is being taken out of service. This method is only called once all threads within the servlet's service method have exited or after a timeout period has passed. After the servlet container calls destroy() method, it will not call the service method again on "THIS SERVLET".

The servlet container is not required to keep a servlet loaded for any particular period of time. A servlet instance may be kept active in a servlet container for a period of milliseconds, for the lifetime of the servlet container (which could be a number of days, months, or years), or any amount of time in between.

When the servlet container determines that a servlet should be removed from service, it calls the destroy method of the Servlet interface to allow the servlet to release any resources it is using and save any persistent state. For example, the container may do this when it wants to conserve memory resources, or when it is being shut down.

Before the servlet container calls the destroy method, it must allow any threads that are currently running in the service method of the servlet to complete execution, or exceed a server-defined time limit.

Once the destroy method is called on a servlet instance, the container may not route other requests to that instance of the servlet.

If the container needs to enable the servlet again, it must do so with a new instance of the servlet's class.

Invention answered 27/12, 2018 at 5:37 Comment(0)
D
0

This is one of those classic spec vs implementation things. Servlet containers like Tomcat are allowed by the specification to maintain a pool of Servlet objects, creating and destroying them as load goes up or down. In this kind of setup, destroy would be called when the particular instance was no longer needed. It would even be legal to create a new instance for each request, calling init immediately before the request is processed and destroy straight after.

However in practice virtually every Servlet container maintains exactly one instance of each Servlet. Since Servlets are required to be thread safe this is also perfectly ok. In this scenario destroy is only called when the context is shutdown.

I can't think of many cases where you would want to pool the Servlets, but it is allowed by the spec.

Djambi answered 18/11, 2012 at 3:52 Comment(0)
E
0

It totally depends on the implementation of the servlet container you are using. If your servlet container is very conservative for saving resources, it may destroy the servlet that is not in service for long time.

In page 22 of servlet spec 2.5, you will find that, "When the servlet container determines that a servlet should be removed from service, it calls the destroy method of the Servlet interface to allow the servlet to release any resources it is using and save any persistent state. For example, the container may do this when it wants to conserve memory resources, or when it is being shut down."

Ethno answered 18/11, 2012 at 4:14 Comment(0)
F
0

Servlet.destroy()'s javadoc says: "Called by the servlet container to indicate to a servlet that the servlet is being taken out of service". It doesn't specify what situations would cause a servlet to be "taken out of service", it is simply an event to which you can react to if you need to. For example you might want to close some resources you have open, or persist some state to storage.

The most obvious reason for this is that the servlet container is being shut down, but a container could equally well decide that it's time to unload a servlet because it's not been used in a month and is simply consuming resources. That behaviour is entirely up to the implementation, and it is not something you (as a servlet designer) should be concerned about.

Friedly answered 16/5, 2014 at 15:43 Comment(0)
B
0

Servlet's destroy method is a callback. These are the functions called by the container when a particular event is about to happen (in this case servlet about to be destroyed). Thus the whole purpose is if you want to do something (eg: cleaning up the resource) you can do those under this callback.

Breannabreanne answered 31/5, 2017 at 12:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.