• Post Reply Bookmark Topic Watch Topic
  • New Topic

servlet init() and drestroy()

 
Tom Nguyen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do we usually implement in the servlet init() and destroy()? I haven been reading around and see that people usually implement connection in the init() and close the connection in the destroy(). However, I wrote a small test and insert a message in the destroy but never seen its output. So I wonder if it is a correct way to implement init() and destroy()?



 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Nguyen wrote:I haven been reading around and see that people usually implement connection in the init() and close the connection in the destroy().

This is a really really really really really bad idea. Do not do this. Ever.

Connections are precious and should be used for very short durations only when needed. (And, in a properly constructed web app, aren't created in the presentation layer in any case.)

I wrote a small test and insert a message in the destroy but never seen its output.

Under what circumstances do you expect to see it?

But to answer the initial question: I use init(), to initialize anything that needs to happen when the servlet is first deployed and before the first request is processed.

And to be honest, in over 15 years of using servlets, I've never used destroy() at all.
 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Under what circumstances do you expect to see it?


Good question!
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And to be honest, in over 15 years of using servlets, I've never used destroy() at all.

Same here. destroy() is run when the web app shuts down, so all resources are released anyway.
 
Tim Holloway
Bartender
Posts: 18415
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The init() method can be used to set up general context. For example life-of-servlet static values loaded from external sources.

In older days, another thing I often used init() for was to launch "engine" threads. Engine threads are threads that process long-running activities and are needed because ordinary servelt request handlers (doGet, doPost, and so forth) are neither permitted to wait for long periods of time or to spawn threads themselves. Since init() runs under a non-request thread that's OK with having arbitrary sub-threads spawned, it's a good place to hang engines. The requests can then queue engine processes and qquery the engine for progress.

Using init() for engines has been discouraged in favor of having a ServletContextListener manage them, and in the latest JEE specs, I'm pretty sure that there's an actual engine management mechanism now available.

I have a number of apps that use destroy(). Not, of course, for database connections. However, if a servlet is managing threads which may be running long processes, this is the place to put logic telling them to shut down. Since the JVM cannot shut down until ALL threads terminate, this ensures that the shutdown wait is kept to a minimum. Some resources, such as the Quartz scheduler need a shutdown command to be issued since it has internal threads to halt. I have an app where the Quartz schedule is managed by a servlet which also controls its parameters.

So essentially, anything that init() sets up that would benefit by explicit termination should consider implementing destroy(). In addition to threads, this might include long-term network socket connections to backend servers. But not database connections!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!