【问题标题】:Grails automatically reopen connection on failGrails 在失败时自动重新打开连接
【发布时间】:2016-01-22 15:00:28
【问题描述】:

我有一个 Grails 应用程序通过域类与 ingres 数据库进行通信。当数据库崩溃或我在应用程序运行时重新启动它时,我得到一个异常:

| Error Caused by: java.sql.SQLTransactionRollbackException: Connection failed.

当我访问数据库时,这个异常每次都会永远出现,尽管数据库从重启/崩溃中再次恢复。

如何强制 Grails / Hibernate 重新创建连接或将其设置为自动重新创建。

这是我的配置:

dataSource {
    dbCreate = 'validate'
    url = "jdbc:ingres://xxx.xxx.xxx.xxx:II7/test"
    driverClassName = "com.ingres.jdbc.IngresDriver"
    username = "ingres"
    password = "ingres"
    jmxEnabled = true
    initialSize = 5
    maxActive = 50
    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 = true
    jdbcInterceptors = "ConnectionState"
    defaultTransactionIsolation = java.sql.Connection.TRANSACTION_READ_COMMITTED
}

【问题讨论】:

    标签: hibernate grails jdbc ingres


    【解决方案1】:

    查看 autoReconnect 参数:

    dataSource {
        url = "jdbc:mysql://localhost/databaseName?useUnicode=yes&characterEncoding=UTF-8&autoReconnect=true"
            }
    

    另一个可能有用的属性:

    dataSource {
        pooled = true
        driverClassName = "com.mysql.jdbc.Driver"
        username = "secret"
        password = "santa"
    
       properties {
          maxActive = 50
          maxIdle = 25
          minIdle = 1
          initialSize = 1
    
          numTestsPerEvictionRun = 3
          maxWait = 10000
    
          testOnBorrow = true
          testWhileIdle = true
          testOnReturn = true
    
          validationQuery = "select now()"
    
          minEvictableIdleTimeMillis = 1000 * 60 * 5
          timeBetweenEvictionRunsMillis = 1000 * 60 * 5
       }
    }
    

    【讨论】:

    • 我没有在我的配置中使用properties {} 部分(见问题)。有必要吗?
    • validationQuery = "select now()": now() 在入口处不可用。请改用select 1
    • autoReconnect URL 参数不适用于 Ingres JDBC 驱动程序。它适用于 mySQL。
    【解决方案2】:

    经过数小时的搜索和丢失关键信息后,问题很容易解决。

    在我的问题的初始配置中,数据源设置周围没有 properties {..} 组。天才的 Grails 配置管理并没有警告我它是必需的。添加它,一切正常,GORM 可以从丢失的连接中恢复。

    【讨论】:

      【解决方案3】:

      java.sql.SQLTransactionRollbackException 表示由于死锁或其他事务序列化失败,可能导致 DB 崩溃的最后一条语句被 DB 自动回滚。

      重启并不能解决这个问题,这可能是驱动程序特定的问题。 SQLState 似乎没有被重置(可能仍包含值“40”,或者任何供应商特定的值)。

      也许主要关注点应该是在应用程序级别查明数据库崩溃的原因。在应用程序运行时重新启动数据库服务器无论如何都是不好的做法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-13
        • 1970-01-01
        • 2020-04-26
        • 2016-05-06
        • 2020-12-10
        • 2018-08-08
        • 2015-11-13
        • 1970-01-01
        相关资源
        最近更新 更多