• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

frnds come up with solution

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do we use Servlet Destroy Method though there is Finalize method? finalise method also for resource deallocation right?
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 288
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic