0

我有一个 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
}
4

3 回答 3

1

Check out autoReconnect parameter:

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

Another properties, which may be helpful:

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
   }
}
于 2016-01-23T03:30:56.543 回答
1

The problem was easy to fix after hours of searching and missing the key information.

In my initial config in my question there was no properties {..} group around datasource settings. The genius Grails config management didn't warn me about that it is needed. Adding it, everything works fine and GORM can recover from lost connections.

于 2016-01-26T10:29:02.960 回答
0

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

The fact that restart does not resolve this issue, could be driver specific issue. It seems that the SQLState is not being reset (may still contain value '40', or whatever the vendor specific value could be).

Perhaps the main focus should be to pin point the cause of DB crash, on application level. Restarting DB server while application is running is bad practice any way.

于 2016-01-22T17:01:24.113 回答