Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / November 2004

Tip: Looking for answers? Try searching our database.

Shutdownhook not working

Thread view: 
Jonck - 22 Nov 2004 15:46 GMT
Hi all,
A web application (using servlets) that I wrote connects to a database
and stores the headers and data sent along with the HttpServletRequest.
To make the interaction faster, when the servlet is initialized I create
a pool of objects, each of which maintains a Connection to a MySQL
database. I use a shutdownhook to close the connections, as I was told
that this was the safest way. In the init() part of the servlet I put
the following code, to close the connection:

Runtime runTime = Runtime.getRuntime();
ShutDownObject hook = new ShutDownObject(processors);
runTime.addShutdownHook(new Thread(hook));

The shutdown object is as follows:

public class ShutDownObject implements Runnable {
    private Vector toolboxPool;
    /** Creates a new instance of ShutDownObject */
    public ShutDownObject(Vector pool) {
        toolboxPool = pool;
}    }
   
    public void run() {
        Iterator poolIterator = toolboxPool.iterator();
        while (poolIterator.hasNext()) {
            try {
                ((ToolboxUnit) poolIterator.next()).getDbConnection().disconnect();
}             } catch (SQLException e) {
                System.out.println(e.getMessage());
}            }
}        }
}    }
}

So as you can see in the run() section I iterate my pool of db
connections and close them. But for some reason, whenever I restart the
servlet the connections seem not to close. The way I'm checking this is
in the MySQL terminal I check the number of connected threads:
mysql> show status like "Threads_connected";

If, for example, I create a pool of 10 objects, after restarting the
servlet the number of threads connected will be 10 larger than before
the servlet was restarted.

Could anyone tell me what I'm doing wrong?

Thanks very much, Jonck
G - 22 Nov 2004 16:31 GMT
" I restart the servlet the connections seem not to close"
How do you restart ?
Did you shut down the JVM ? ShutdownHook are only invoked when JVM shuts
down and, if the JVM shuts down, db connections should be closed
implicitelly, so you shouldn't see connections in the database console ...

Try to place a breakpoint in you method ...

> Hi all,
> A web application (using servlets) that I wrote connects to a database
[quoted text clipped - 43 lines]
>
> Thanks very much, Jonck
Murray - 22 Nov 2004 23:07 GMT
> " I restart the servlet the connections seem not to close"
> How do you restart ?
[quoted text clipped - 3 lines]
>
> Try to place a breakpoint in you method ...

I think G is on the right track. If you're just restarting your servlet, or
Tomcat is reloading your context, this is not the same as restarting the
JVM. It's just the ClassLoader loading and unloading your classes.

Add some logging statements so you know if/when your code is being executed.
Jonck - 23 Nov 2004 15:17 GMT
>> " I restart the servlet the connections seem not to close"
>> How do you restart ?
[quoted text clipped - 12 lines]
> Add some logging statements so you know if/when your code is being
> executed.

Ah yes, that is indeed the case, the VM is not exiting, it's just Tomcat
reloading the context, good one, thanks G and Murray!
Would either of you happen to know if there is an event that occurs when
the servlet is unloaded that I could use to trigger the disconnect
thread? I read through the API text but could not find anything related
to this.

Thanks very much, Jonck
G - 23 Nov 2004 21:54 GMT
Try to implement the destroy method of you servlet.

From J2EE doc : javax.servlet.GenericServlet.destroy()

     void destroy()
               Called by the servlet container to indicate to a servlet
that the servlet is being taken out of service.

You just have to hope that tomcat call it when switching of servlets.

You can also implement the Object.finalize() system ...

>>> " I restart the servlet the connections seem not to close"
>>> How do you restart ?
[quoted text clipped - 21 lines]
>
> Thanks very much, Jonck
Murray - 24 Nov 2004 01:13 GMT
> Ah yes, that is indeed the case, the VM is not exiting, it's just Tomcat
> reloading the context, good one, thanks G and Murray!
[quoted text clipped - 4 lines]
>
> Thanks very much, Jonck

You can do as G suggested, though preferably the destroy() option.

Have you considered using an existing connection pool library like Commons
DBCP? You configure these as a DataSource in Tomcat's server.xml, and you
don't need to worry about closing connection etc. It's all done for you.
Unless you have very specific requirements, I don't see why you'd want to
reinvent the wheel ...
Jonck - 24 Nov 2004 14:10 GMT
> You can do as G suggested, though preferably the destroy() option.
>
[quoted text clipped - 3 lines]
> all done for you.  Unless you have very specific requirements, I don't
> see why you'd want to  reinvent the wheel ...

Actually I did try to use an existing library some time ago, but I found
the documentation lacking and was having a hard time setting it up and
getting it all to work. Therefore I just went ahead and coded my own. It
was a good and fun exercise in thread programming, I think I learned a
lot while coding it. But generally speaking you are certainly right, no
use reinventing the wheel.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.