Q:

Intermittent error message with JSP<->Mysql connection

I have some jsp pages that connect to a mysql database and I intermittently get the following error message:
"The last packet successfully received from the server was 36,727,995 milliseconds ago.  The last packet sent successfully to the server was 36,733,072 milliseconds ago. 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"
The occurrence pattern is pretty random but it tends to happen most frequently after there has not been much activity on the website, like first thing in the morning.
I found this thread on stackoverflow that mentions the same error:
Tomcat6 can't connect to MySql (The driver has not received any packets from the server)
and the final resolution was to add this line:
permission java.net.SocketPermission "127.0.0.1:3306", "connect";

to 
$TOMCAT-CONFIG/policy.d/04webapps.policy

I'm running tomcat 7 on centOS 5.5 and I don't see a policy.d directory nor a 04webapps.policy file anywhere. Do I need to manually create both of these or is there another way I should be resolving this issue?
Update:
Here's an example of how I query the database in one of my jsp files:
DBFunctions dbf = null;
boolean bException = false;
ArrayList<String[]> arrayListRows1 = new ArrayList<String[]>();
try
{
    dbf = new DBFunctions();
    dbf.db_run_query(query2);
    while (dbf.rs.next()) {
        String [] tmp = new String[6];

        tmp[0] = dbf.rs.getString("fact_data.entity_id");
        tmp[1] = dbf.rs.getString("date_begin");
        tmp[2] = dbf.rs.getString("value");
        tmp[3] = dbf.rs.getString("ticker");
        tmp[4] = dbf.rs.getString("full_name");
        tmp[5] = dbf.rs.getString("name");

        arrayListRows1.add(tmp);

    }
}
catch (SQLException sqle)
{

    out.println(PopulateSpreadsheet.createGoogleError(strReqId,"sql_exception",sqle.getMessage(),"PF ERROR CODE 2eh2-1"));
    bException = true;
}
finally
{
    if (dbf !=null) dbf.closeConnection();
    if (bException == true)
        return;
}

DBFunctions.java sits under the src/ directory of the web application, so it's not technically a jsp page that is directly querying the database.
I will go back and check one more time, but I tried to code all my database queries the same way with the connection being closed immediately after being done with the ResultSet.
Any recommendations on how best to do an inventory on the connection pool to periodically check the state of the connections?
Update: I'm still having issues with this error. I appreciate the suggestions on moving the bulk of my programming logic out of my jsp pages and into a servlet/library, but I don't think that will fix this particular issue. 
Other people have reported seeing this issue and it's because mysql is timing out connections in the tomcat jdbc connection pool. It's odd to me that the tomcat connection pool doesn't employ some kind of keep alive to avoid this issue. I would think that would be a part of every connection pool design.

A:

I was able to fix the problem by adding the following properties to context.xml:
testWhileIdle="true"
testOnBorrow="true"
testOnReturn="false"
validationQuery="SELECT 1"
validationInterval="30000"

These settings result in a connection being tested when it is pulled from the pool.

