views:

6404

answers:

1

We have a java server connecting to a MySQL 5 database usingHibernate as our persistence layer which is using c3p0 for DB connection pooling.

I've tried following the c3p0 and hibernate documentation:

We're getting an error on our production servers stating that:

... Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.Connection was implicitly closed due to underlying exception/error:

BEGIN NESTED EXCEPTION

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException

MESSAGE: The last packet successfully received from the server was45000 seconds ago.The last packet sent successfully to the server was 45000 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.

STACKTRACE:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was45000 seconds ago.The last packet sent successfully to the server was 45000 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.

We have our c3p0 connection pool properties setup as follows:

hibernate.c3p0.max_size=10
hibernate.c3p0.min_size=1
hibernate.c3p0.timeout=5000
hibernate.c3p0.idle_test_period=300
hibernate.c3p0.max_statements=100
hibernate.c3p0.acquire_increment=2

The default MySQL wait_timetout is set to 28800 seconds (8 hours), the reported error is saying that it's been over 45000 seconds (about 12.5 hours). Although the c3p0 configuration states that it will "timeout" idle connections that haven't been used after 5000 seconds and it will check every 300 seconds, thus an idle connection should never live longer than 5299 seconds right?

I've tested locally by setting my developer MySQL (my.ini on windows, my.cnf on Unix) wait_timeout=60 and lowering the c3p0 idle timeout values below 60 seconds, and it will properly timeout idle connections and create new ones. I also check to ensure that we're not leaking DB connections and holding onto a connection, and it doesn't appear we are.

Here's the c3p0.properties file I'm using to test in my developer environment to ensure c3p0 is properly handling connections.

*hibernate.properties (testing with MySQL wait_timeout=60)*

hibernate.c3p0.max_size=10
hibernate.c3p0.min_size=1
hibernate.c3p0.timeout=20
hibernate.c3p0.max_statements=100
hibernate.c3p0.idle_test_period=5
hibernate.c3p0.acquire_increment=2

c3p0.properties

com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL=ALL
com.mchange.v2.log.MLog=com.mchange.v2.log.FallbackMLog
c3p0.debugUnreturnedConnectionStackTraces=true
c3p0.unreturnedConnectionTimeout=10
+1  A: 

Make sure that c3p0 really is starting by examine the log. I, for some reason, had two versions of hibernate (hibernate-core3.3.1.jar and hibernate-3.2.6GA.jar) on my classpath. I also used hibernate annotatations version 3.4.0GA which is not compatible with 3.2.x. (dont know if that had something to do with the original problem). After removal of one of the hibernate jar's (cant remember which i deleted, probably hibernate-3.2.6GA.jar) c3p0 finally started and i got rid of the annoying com.mysql.jdbc.exceptions.jdbc4.CommunicationsException that happend efter 8h inactivity.

Schildmeijer
We are using Hibernate 3.2.6 Hibernate, with 3.4.0GA Hibernate annotations, and we don't have any noticeable exceptions (do the issues only come up when dealing with c3p0 after 8 hours or would we notice otherwise).We are seeing C3P0 being configured and used in our logs (once I turned logging on with c3p0.properties) e.g."[INFO] Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource ... idleConnectionTestPeriod -> 20, , maxIdleTime -> 60... "
Dougnukem
try upgrade hibernate to 3.3.X, the above combination is not supported by hibernate. (or downgrade hibernate annotation)
Schildmeijer
tried anything? any luck yet?
Schildmeijer
It appears to be working now I didn't have to upgrade to 3.3.X, we were able to stay at 3.2.6 with annotations 3.4.0, unless Maven is resolving the dependencies in a weird manner. I did change my hibernate.properties to :hibernate.connection.provider_class=org.hibernate.connection.C3P0ConnectionProviderhibernate.c3p0.max_size=100hibernate.c3p0.min_size=10hibernate.c3p0.timeout=5000hibernate.c3p0.max_statements=100hibernate.c3p0.idle_test_period=300hibernate.c3p0.acquire_increment=2
Dougnukem
ok, interesting. as far as i can see the only thing you added was hibernate.connection.provider_class=org.hibernate.connection.C3P0ConnectionProvider ?
Schildmeijer
I know I'm lat on this question but this could be useful to people looking for answers : it seems that this provider_class parameter was added in 3.2 and is now required, but not very much documented. See http://www.agileapproach.com/blog-entry/hibernate-c3p0-connection-pool-config-changes
Pierre Henry