Destroy method not being called

Hi,

I have a Servlet running on the oc4j server.

When i kill the OC4J i expect the "destroy" method to be called.The destroy method is having a "System.out.println" statement.

Yet i dont see any o/p when the OC4J is coming down.

Please tell me if i am expecting sth which i should not be expecting or is it a OC4J bug.

TIA,

Gaurav

[373 byte] By [stuckwithJAVAa] at [2007-10-3 0:54:37]
# 1

The Servlet spec doesn't say much about this:

SRV.2.3.4 End of Service

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.

I would assume that destroy() would be called as part of shutdown. You probabaly want to check the OC4J documentation to see how it handles this event.

bckrispia at 2007-7-14 17:49:58 > top of Java-index,Java Essentials,Java Programming...
# 2

> I have a Servlet running on the oc4j server.

> When i kill the OC4J i expect the "destroy" method to

> be called.The destroy method is having a

> "System.out.println" statement.

> Yet i dont see any o/p when the OC4J is coming down.

>

> Please tell me if i am expecting sth which i should

> not be expecting or is it a OC4J bug.

There are any number of ways that an application server can stop and it will guarantee that the destroy method is NOT called.

And nothing in the spec is going to be able to change that.

So do yourself a favor and create a design that doesn't rely on that.

jschella at 2007-7-14 17:49:58 > top of Java-index,Java Essentials,Java Programming...
# 3

> There are any number of ways that an application

> server can stop and it will guarantee that the

> destroy method is NOT called.

>

> And nothing in the spec is going to be able to change

> that.

>

> So do yourself a favor and create a design that

> doesn't rely on that.

But if the server is shut down normally, shouldn't destroy() be called on all loaded servlets as part of the process?

Shutdown hooks are the preferred method of doing clean-up before a jvm exits, but there are still many things that can happen that prevent this from executing (including kill -9). Does this mean that we shouldn't use them?

bckrispia at 2007-7-14 17:49:58 > top of Java-index,Java Essentials,Java Programming...
# 4
Exactly thatz what i am wondering.
stuckwithJAVAa at 2007-7-14 17:49:58 > top of Java-index,Java Essentials,Java Programming...
# 5
tricky one thisPerhaps there are limitations as to what can be done in the destroy method. Perhaps destroy is blocked for I/O
r035198xa at 2007-7-14 17:49:58 > top of Java-index,Java Essentials,Java Programming...
# 6

> But if the server is shut down normally,

> shouldn't destroy() be called on all loaded servlets

> as part of the process?

And what if it is?

What are you going to be doing in there that you should do only when you normally shut down but which shouldn't be done if there is a different type of shutdown?

jschella at 2007-7-14 17:49:58 > top of Java-index,Java Essentials,Java Programming...
# 7

Let us say i would like to have some debug messages message which i wished to be logged whether the server is shut-sown "normally" or "abnormally"?Where else do i write it if not in the "destroy()" method.

Also the debug message are not being logged.

So how do we believe or verify that the "destroy()" message will always be called.

stuckwithJAVAa at 2007-7-14 17:49:58 > top of Java-index,Java Essentials,Java Programming...