I've a spring web app running in a Tomcat server. In this there is a piece of code,in one of the Spring beans, which waits for a database connection to become available. My scenario is that while waiting for a database connection, if the Tomcat is shutdown, I should stop waiting for a DB connection and break the loop.
private Connection prepareDBConnectionForBootStrapping() { Connection connection = null; while (connection == null && !Thread.currentThread().isInterrupted()) { try { connection = getConnection(); break; } catch (MetadataServerException me) { try { if (!Thread.currentThread().isInterrupted()) { Thread.sleep(TimeUnit.MINUTES.toMillis(1)); } else { break; } } catch (InterruptedException ie) { logger.error("Thread {} got interrupted while wating for the database to become available.", Thread.currentThread().getName()); break; } } } return connection; } The above piece of code is executed by one of the Tomcat's thread and it's not getting interrupted when shutdown is invoked. I also tried to achieve my scenario by using spring-bean's destroy method, but to my surprise the destroy method was never called. My assumption is that Spring application context is not getting fully constructed - since, I've the above waiting loop in the Spring bean - and when shutdown is invoked corresponding context close is not getting invoked.
Any suggestions?
getConnection()isn't just blocking until there is one?