MySQL Connection Timeout Issue - Grails Application on Tomcat using Hibernate and ORM

后端 未结 7 2089
青春惊慌失措
青春惊慌失措 2020-12-08 07:42

I have a small grails application running on Tomcat in Ubuntu on a VPS. I use MySql as my datastore and everything works fine unless I leave the application for more than ha

相关标签:
7条回答
  • 2020-12-08 08:16

    Starting from grails 2.3.6 default configuration already has options for preventing closing connection by timeout

    These are the new defaults.

        properties {
           // See http://grails.org/doc/latest/guide/conf.html#dataSource for documentation
           ....
           minIdle = 5
           maxIdle = 25
           maxWait = 10000
           maxAge = 10 * 60000
           timeBetweenEvictionRunsMillis = 5000
           minEvictableIdleTimeMillis = 60000
           validationQuery = "SELECT 1"
           validationQueryTimeout = 3
           validationInterval = 15000
           testOnBorrow = true
           testWhileIdle = true
           testOnReturn = false
           jdbcInterceptors = "ConnectionState;StatementCache(max=200)"
           defaultTransactionIsolation = java.sql.Connection.TRANSACTION_READ_COMMITTED
        }
    
    0 讨论(0)
  • 2020-12-08 08:21

    Add these parameters to dataSource

            testOnBorrow = true
            testWhileIdle = true
            testOnReturn = true
    

    See this article for more information http://sacharya.com/grails-dbcp-stale-connections/

    0 讨论(0)
  • 2020-12-08 08:22

    What does your JDBC connection string look like? You can set an autoReconneect param in your data source config, e.g.

    jdbc:mysql://hostname/mydb?autoReconnect=true
    
    0 讨论(0)
  • 2020-12-08 08:31

    In grails 1.3.X, you can modify the evictor values in the DataSource.groovy file to make sure pooled connections are used during idle. This will make sure the mysql server will not time out the connection.

    production {
      dataSource {
        pooled = true
        // Other database parameters..
        properties {
           maxActive = 50
           maxIdle = 25
           minIdle = 5
           initialSize = 5
           minEvictableIdleTimeMillis = 1800000
           timeBetweenEvictionRunsMillis = 1800000
           maxWait = 10000
        }
    }
    

    A quick way to verify this works is to modify the MySQL my.cnf configuration file [mysql] element and add wait_time parameter with a low value.

    0 讨论(0)
  • 2020-12-08 08:36

    Try increasing the number of open MySQL connections by putting the following in your DataSources.groovy:

    dataSource {
         driverClassName = "com.mysql.jdbc.Driver"
         pooled=true
         maxActive=10
         initialSize=5
         // Remaining connection params
    }
    

    If you want to go the whole hog, try implementing a connection pool; here is a useful link on this.

    0 讨论(0)
  • 2020-12-08 08:39

    Referring to this article, you have stale connections in your DBCP connections pool that are silently dropped by OS or firewall.

    The solution is to define a validation query and do a sanity check of the connection before you actually use it in your application. In grails this is actually done by modifying the grails-app/conf/spring/Resource.groovy file and add the following:

    beans = {
      dataSource(BasicDataSource) {
        //run the evictor every 30 minutes and evict any connections older than 30 minutes.
        minEvictableIdleTimeMillis=1800000
        timeBetweenEvictionRunsMillis=1800000
        numTestsPerEvictionRun=3
        //test the connection while its idle, before borrow and return it
        testOnBorrow=true
        testWhileIdle=true
        testOnReturn=true
        validationQuery="SELECT 1"
      }
    } 
    
    0 讨论(0)
提交回复
热议问题