So I have a servlet that queries the database every minute, so I figure I'll just have one connection open forever (well, until the webapp is stopped). Therefore I'm opening a connection in init() and closing it in destroy(). Thing is, if I look at the DB after stopping the app in tomcat the connection is still open. What's happening?
Here's my code:
public void init() throws ServletException
{
try
{
// Prepare the DB connection
DriverManager.registerDriver(new com.informix.jdbc.IfxDriver());
informixConnection = DriverManager.getConnection(DBURL, DBUsername, DBPassword);
}
catch(SQLException e)
{
throw new UnavailableException("Error connecting to the database");
}
}
public void destroy()
{
try
{
informixConnection.close();
}
catch(Exception e)
{
}
}
There's also going to be a method that actually does a query, and a doGet so a user can get the most recent response, but I haven't finished those yet (although I have tested them and the database connection works).
I've no idea why this wasn't working. I added some logging into the destroy method to confirm it was being called when the app was shut down and suddenly it started working. Weird.
OK, now to go and write it in a not-a-servlet way...