<p>I have a tomcat instance setup but the database connection I have configured in <code>context.xml</code> keeps dying after periods of inactivity.</p>

<p>When I check the logs I get the following error:</p>

<p>com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: 
The last packet successfully received from the server was68051 seconds 
ago. The last packet sent successfully to the server was 68051 seconds 
ago, which is longer than the server configured value of 
'wait_timeout'. You should consider either expiring and/or testing 
connection validity before use in your application, increasing the 
server configured values for client timeouts, or using the Connector/J 
connection property 'autoReconnect=true' to avoid this problem.</p>

<p>Here is the configuration in context.xml:</p>

<pre><code>&lt;Resource name="dataSourceName" 
        auth="Container" 
        type="javax.sql.DataSource"
        maxActive="100" 
        maxIdle="30" 
        maxWait="10000" 
        username="username" 
        password="********"
        removeAbandoned = "true"
        logAbandoned = "true"
        driverClassName="com.mysql.jdbc.Driver" 
        url="jdbc:mysql://127.0.0.1:3306/databasename?autoReconnect=true&amp;amp;useEncoding=true&amp;amp;characterEncoding=UTF-8"  /&gt;
</code></pre>

<p>I am using <code>autoReconnect=true</code> like the error says to do, but the connection keeps dying. I have never seen this happen before.</p>

<p>I have also verified that all database connections are being closed properly.</p>

[{'answer_id': 16168, 'author': 'ScArcher2', 'author_id': 1310, 'author_profile': 'https://Stackoverflow.com/users/1310', 'pm_score': 5, 'selected': True, 'text': '<p><a href="http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html" rel="noreferrer">Tomcat Documentation</a></p>\n\n<p>DBCP uses the Jakarta-Commons Database Connection Pool. It relies on number of Jakarta-Commons components:</p>\n\n<pre><code>* Jakarta-Commons DBCP\n* Jakarta-Commons Collections\n* Jakarta-Commons Pool\n</code></pre>\n\n<p>This attribute may help you out.</p>\n\n<pre><code>removeAbandonedTimeout="60"\n</code></pre>\n\n<p>I\'m using the same connection pooling stuff and I\'m setting these properties to prevent the same thing it\'s just not configured through tomcat.\nBut if the first thing doesn\'t work try these.</p>\n\n<pre><code>testWhileIdle=true\ntimeBetweenEvictionRunsMillis=300000\n</code></pre>\n'}, {'answer_id': 16751, 'author': 'abyx', 'author_id': 573, 'author_profile': 'https://Stackoverflow.com/users/573', 'pm_score': 0, 'selected': False, 'text': "<p>I do not know whether the above answer does basically the same thing, but some of our systems use the DB connection about once a week and I've seen that we provide a -Otimeout flag or something of that sort to mysql to set the connection timeout.</p>\n"}, {'answer_id': 74086, 'author': 'Sindri Traustason', 'author_id': 1113, 'author_profile': 'https://Stackoverflow.com/users/1113', 'pm_score': 2, 'selected': False, 'text': '<p>Just to clarify what is actually causing this. MySQL by default terminates open connections after 8 hours of inactivity.  However the database connection pool will retain connections for longer than that.</p>\n\n<p>So by setting timeBetweenEvictionRunsMillis=300000 you are instructing the connection pool to run through connections and evict and close idle ones every 5 minutes.</p>\n'}, {'answer_id': 528106, 'author': 'Ophir', 'author_id': 17634, 'author_profile': 'https://Stackoverflow.com/users/17634', 'pm_score': 1, 'selected': False, 'text': '<p>The removeAbandoned option is deprecated as of DBCP 1.2 (though <a href="http://commons.apache.org/dbcp/apidocs/org/apache/commons/dbcp/AbandonedConfig.html" rel="nofollow noreferrer">still present</a> in the 1.3 branch). <a href="http://mail-archives.apache.org/mod_mbox/commons-user/200604.mbox/%3Cf8b39ace0604061146q36245632t2e65bef637dc0400@mail.gmail.com%3E" rel="nofollow noreferrer">Here</a>\'s a non-official explanation.</p>\n'}]
['https://Stackoverflow.com/questions/15949', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/22/']