title: Tomcat/MySQL/Java servlet application getting 500 error after some hours of inactivity (using JNDI Connection Pool)
body: <p>I have written a Java Servlet/JSP web app that uses MySQL to hold the login authentication. The web container is Tomcat 7.0.47, and I'm using JNDI connection pooling for the database connectivity. I've carefully configured the <code>&lt;Resource...&gt;</code> tags in my Tomcat's context.xml and during the day when there is plenty of activity the application's login functionality never fails. But after an overnight break, the very first login attempt very often throws a 500 error in the browser. On the second attempt and subsequent attempts in the same day, the login succeeds. </p>
  
  <p><strong>I've tried:</strong></p>
  
  <p>1: updating the MySQL server to the latest version (running in Windows Server 2008)</p>
  
  <p>2: Updating the mysql-connector-java-5.1.27-bin.jar (the latest at time of writing)</p>
  
  <p>3: removing the mysql-connector-java-5.1.27-bin.jar file from the build path (as I've already included it in the Tomcat lib directory)</p>
  
  <p>4: Added the attribute <code>closeMethod = "close"</code> to the  tag in Tomcat context.xml</p>
  
  <p>5: Added attribute <code>minIdle = "1"</code> to the  tag in Tomcat context.xml</p>
  
  <p>6: I have <code>removeAbandoned = "true"</code> configured in context.xml</p>
  
  <p>7: Changing all my application code to Java 7's try-with-resources to guarantee closing of all resources in the application code</p>
  
  <p>(4, 5 and 6 are just post-Googling stabs in the dark really, and they didn't work)</p>
  
  <p>Every morning I'm the first to log in to my application: I get the 500, then for the rest of the day, everything is fine. I've been looking at this for weeks now but am out of ideas!</p>