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

后端 未结 3 950
独厮守ぢ
独厮守ぢ 2020-12-31 07:31

I got this error from the production code:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server

相关标签:
3条回答
  • 2020-12-31 07:49

    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.

    0 讨论(0)
  • 2020-12-31 07:55

    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?

    0 讨论(0)
  • 2020-12-31 08:04

    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
    
    0 讨论(0)
提交回复
热议问题