I got this error from the production code:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server
To reproduce your error, set your connection timeout in your MySQL properties to a very low value, ie 2 ms, and run a query known to have a long processing time. You can set the timeout property either in the MySQL connection string or via a property if you're using properties files to setup your JDBC connection. You can look up the Javadocs on your specific jaxax.sql.DataSource connection and the MySQL docs for the specifics on how to do this.
Fei - could be one of several things, can't really say based on the info posted so far.
Suggest you add MySQL/Spring/Hibernate/C3PO/JDBC version numbers to your question, in case there is a known issue out there.
The production error message is a common one, with many possible root causes. Some leads for you:
The production error may indicate that your application is not releasing a connection back to the pool when done with it, preventing c3p0 from checking it. (The c3p0 idle checks can only be applied to unchecked-out connections.)
Check that c3p0 is really working (you may be using 'vanilla' connections if not). In your test, if you set (e.g.) MySql wait_timeout=10, application thread sleep=35, and idleConnectionTestPeriod=30, if pooling is working, the exception should go away.
On the expense of the idle checks: consider not using the default getTables() - maybe set preferredTestQuery to something cheap(-er) 'SELECT 1' maybe for MySQL?
I had similar problems with MySQL and a connection pool. The problem is you tell the connection pool that an idle timeout is 30 minutes, but the database cuts the connection after 10 seconds. Since your idle connection check period is 120 sec, it leaves a little under 110 secs for the pool to use a broken connection!
I'd use the following settings for production:
MySQL:
wait_timeout=75
C3P0:
maxIdleTime=60
idleConnectionTestPeriod=55