• Post Reply Bookmark Topic Watch Topic
  • New Topic

frnds come up with solution

 
Anantha Reddy.N
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do we use Servlet Destroy Method though there is Finalize method? finalise method also for resource deallocation right?
 
Scott Selikoff
author
Bartender
Posts: 4033
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In short, you could probably use one or the other as you wish (as with any java code) but it really matters what your doing with your servlet.

For example, a good pattern might be to open a connection open during the init() for the life of the servlet and close it in the destroy(), but this limits you on selection (or non-selection) of database connections for the servlet.

Ergo, it is usually best to always wrap any database connection statement with a finally block if the connection is not closed elsewhere. Its very easy to write code that exhausts your database connection pool.

While finally blocks are often concerned with database resources in practice, detroy() methods are often concerned with session/servlet cleanup events unrelated to the database.

The real question I have is why do you database code inside a servlet?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do we use Servlet Destroy Method though there is Finalize method?

1. The destroy() method is part of the servlet API - think of it as a "contract" between the servlet container and the programmer. When the servlet container is removing a servlet for any reason, destroy will be called. There is no guarantee that finalize will ever be called.
2. When writing about Java methods - don't use upper case "Destroy" when the real method name is destroy - sounds picky I know, but paying close attention to case is important to Java programmers.
Bill
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anantha, please read this with regard to naming topics.

Also, please be sure to pick the proper forum in which to post. Since this is a servlet rather than a JSP question, it should be in the Servlets forum.

I have moved this post there for you.
 
ak pillai
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stay away from the finalize() method to destroy resources, since you do not guarantee when this method is going to be called by the GC. So use the finally{} block whereever possible and if required to use destroy() method.

Also in non-servlet classes stay away from finalize() method and use finally block or provide your own cleanUp() method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!