Reproduce com.mysql.jdbc.exceptions.jdbc4.CommunicationsException with a setup of Spring, hibernate and C3P0

若如初见. 提交于 2019-11-30 08:35:31
David Rabinowitz

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

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:

  1. 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.)

  2. 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.

  3. 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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!