programing

MySQL, Grails 2 앱을 사용하지 않는 동안 풀링된 연결을 지속적으로 유지하는 올바른 방법(또는 풀링된 연결의 시간을 초과하고 새 연결을 얻는 방법)

css3 2023. 11. 1. 22:30

MySQL, Grails 2 앱을 사용하지 않는 동안 풀링된 연결을 지속적으로 유지하는 올바른 방법(또는 풀링된 연결의 시간을 초과하고 새 연결을 얻는 방법)

저는 활동량이 많은 성배 앱을 가지고 있지만, 종종 몇 시간에서 밤새 지속될 수 있는 활동량이 없는 기간을 가지고 있습니다.아침에 처음 사용하는 사용자는 다음과 같은 유형의 예외를 받게 됩니다. 풀의 연결이 오래되고 MYSql 데이터베이스가 닫히기 때문이라고 생각합니다.

Connector/J 연결 속성 'autoReconnect=true'를 사용하는 것이 좋은 아이디어인지(그리고 클라이언트가 연결이 복원되더라도 예외가 발생하는지 여부), 유휴 연결을 주기적으로 제거하거나 새로 고침하는 다른 속성을 설정할지, 대여 테스트 등에 대해 구글에서 상반된 정보를 발견했습니다.성배는 밑에 DBCP를 사용합니다.저는 현재 아래와 같은 간단한 구성을 가지고 있으며, 오랜 비활성 기간 후에 풀에서 잡힌 연결이 유효하고 닫히지 않도록 가장 잘 보장하는 방법에 대한 답을 찾고 있습니다.

dataSource {
        pooled = true
        dbCreate = "update"
        url = "jdbc:mysql://my.ip.address:3306/databasename"
        driverClassName = "com.mysql.jdbc.Driver"
        dialect = org.hibernate.dialect.MySQL5InnoDBDialect
        username = "****"
        password = "****"
        properties {
          //what should I add here?
          }
    }

예외.

    2012-06-20 08:40:55,150 [http-bio-8443-exec-1] ERROR transaction.JDBCTransaction  - JDBC begin failed
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 64,129,968 milliseconds ago.  The last packet sent successfully to the server was 64,129,968 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.
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
    at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3851)
    ...... Lots more .......
Caused by: java.sql.SQLException: Already closed.
    at org.apache.commons.dbcp.PoolableConnection.close(PoolableConnection.java:114)

애플리케이션에 전달되기 전에 연결을 테스트하기 위해 실행할 쿼리를 지정하도록 연결 풀을 구성하는 것이 가장 쉬운 방법입니다.

validationQuery="select 1 as dbcp_connection_test"
testOnBorrow=true

이와 같은 "연결 유효성 검사" 쿼리는 다른 이벤트에서 실행될 수 있습니다.다음에 대한 기본값을 잘 모르겠습니다.

testOnReturn=true
testWhileIdle=true

풀에서 유휴 연결의 "연령"을 제한하는 구성 설정도 있는데, 이 설정은 유휴 연결이 서버 끝에서 닫히는 경우에 유용할 수 있습니다.

minEvictableIdleTimeMillis
timeBetweenEvictionRunsMillis

http://commons.apache.org/dbcp/configuration.html

데이터베이스 연결을 처리하는 가장 좋은 방법인지는 모르겠지만, 말씀하신 것과 같은 문제가 있었습니다.나는 많은 노력을 했고 결국 c3p0 연결 풀이 되었습니다.

c3p0을 사용하면 일정 시간 후에 데이터베이스 연결을 강제로 갱신할 수 있습니다.

배치.c3p0.jar당신의 안으로lib폴더에 구성을 추가합니다.conf/spring/resources.groovy.

나의resources.groovy다음과 같습니다.

import com.mchange.v2.c3p0.ComboPooledDataSource
import org.codehaus.groovy.grails.commons.ConfigurationHolder as CH

beans = {
    /**
    * c3P0 pooled data source that forces renewal of DB connections of certain age
    * to prevent stale/closed DB connections and evicts excess idle connections
    * Still using the JDBC configuration settings from DataSource.groovy
    * to have easy environment specific setup available
    */
    dataSource(ComboPooledDataSource) { bean ->
        bean.destroyMethod = 'close'
        //use grails' datasource configuration for connection user, password, driver and JDBC url
        user = CH.config.dataSource.username
        password = CH.config.dataSource.password
        driverClass = CH.config.dataSource.driverClassName
        jdbcUrl = CH.config.dataSource.url
        //force connections to renew after 4 hours
        maxConnectionAge = 4 * 60 * 60
        //get rid too many of idle connections after 30 minutes
        maxIdleTimeExcessConnections = 30 * 60
    }
 }  

언급URL : https://stackoverflow.com/questions/11125962/correct-way-to-keep-pooled-connections-alive-or-time-them-out-and-get-fresh-one