Friday, June 11, 2010

Hibernate JDBCConnectionException: could not execute query- Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:

"org.quartz.JobExecutionException: could not execute query; nested exception is org.hibernate.exception.JDBCConnectionException: could not execute query [See nested exception: org.springframework.dao.DataAccessResourceFailureException: could not execute query; nested exception is org.hibernate.exception.JDBCConnectionException: could not execute query]
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 13,768,115 milliseconds ago.  The last packet sent successfully to the server was 13,768,115 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."

This problem occurred in a Grails application having a cron job that requests some data from an external server, processes it and persists some results. However the data is very large and the app requires 2+ Hrs. to process all of it before persisting a small result. The jdbc connection remains idle during this time.
Setting 'autoReconnect=true' or max_idle_time is certainly not a reliable solution for this.

What worked for me:
http://sacharya.com/grails-dbcp-stale-connections/: This was my exact problem. It says "By default, DBCP holds the pooled connections open for infinite time. But a database connection is essentially a socket connection, and it doesn’t come for free. The host OS, database host, and firewall have to allocate a certain amount of memory and other resources for each socket connection. It makes sense to those devices not to hold onto idle connections for ever. So the idea is to make sure that you don’t have stale connections in your pool that would otherwise be silently dropped by OS or firewall."

Modified my Datasource.groovy for working out this solution.
Had to use the following syntax from: http://stackoverflow.com/questions/376544/grails-mysql-maxpoolsize
Modified my Datasource.groovy:

dataSource {
    pooled = true
    dbCreate = "update"
    url = "jdbc:mysql://localhost/yourDB"
    driverClassName = "com.mysql.jdbc.Driver"
    username = "yourUser"
    password = "yourPassword"
    properties {
        maxActive = 50
        maxIdle = 25
        minIdle = 5
        initialSize = 5
        minEvictableIdleTimeMillis = 60000
        timeBetweenEvictionRunsMillis = 60000
        maxWait = 10000     
    }   }


Other relevant links:

http://drglennn.blogspot.com/2009/05/javasqlsqlexception-communication-link.html
http://commons.apache.org/dbcp/configuration.html
http://www.grails.org/DataSources+New
http://www.grails.org/1.2+Release+Notes

2 comments:

  1. This design is incredible! You definitely know how to keep a reader
    amused. Between your wit and your videos, I was almost moved to
    start my own blog (well, almost...HaHa!) Wonderful job.

    I really enjoyed what you had to say, and more than that,
    how you presented it. Too cool!

    My page http://completegolfsets.Net

    ReplyDelete
  2. I comment whenever I appreciate a post on a site or if I have something to valuable to contribute to the discussion.
    It's a result of the sincerness displayed in the post I browsed. And on this post "Hibernate JDBCConnectionException: could not execute query- Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:". I was actually moved enough to drop a leave a responsea response :-P I do have a few questions for you if you tend not to mind. Could it be only me or do some of these remarks appear like they are coming from brain dead people? :-P And, if you are posting on other sites, I'd
    like to follow you. Could you list every one of
    your community sites like your linkedin profile, Facebook page
    or twitter feed?

    Feel free to surf to my webpage - micahmoney.com

    ReplyDelete