使用 Quartz 调度程序时在 Grails 中关闭连接

Closed connection in Grails when using Quartz scheduler

我有一个 Grails 应用程序,其中每 10 秒有两个 Quartz 作业 运行。它在一段时间内工作正常,但一段时间后我不断收到以下错误。

[quartzScheduler_Worker-10] ERROR listeners.ExceptionPrinterJobListener  - Exception occurred in job: Grails Job
Message: org.hibernate.exception.JDBCConnectionException: could not execute query

我正在尝试了解导致问题的原因。

这是其中一名工人,

package noalert
import noalert.NoAlertActivity
import noalert.NoAlertSchedule
import java.text.SimpleDateFormat


class ArchiveJob {
    static triggers = {
      simple repeatInterval: 10000l // execute job once in 10 seconds
    }

    def execute() {
        // execute job
        Map<Object,Object> params = new HashMap<Object,Object>();
        def currentTime = new Date()
            def noAlertActivities = NoAlertActivity.createCriteria().list(){
            and{
                lt("stopTime",currentTime)
                eq('state',1)
            }

        }


        NoAlertActivity currentRecord
        for (int i =0; i<noAlertActivities.size();i++){
            currentRecord = noAlertActivities.get(i)
            params.lastActive = currentTime
            currentRecord.delete(currentRecord,params)
        }


        }
    }

您能告诉我这个错误的可能原因吗?

上次我的 Quartz 作业出现这种错误是因为我的 JDBC 连接关闭了(在生产中)。这是通过更好的生产数据源配置解决的。我使用了 Grails documentation 中建议的那个。它将正确管理连接池并在需要时创建连接。请注意,这个是针对 MySQL 但它可能适用于其他数据库。

dataSource {
    pooled = true
    dbCreate = "update"
    url = "jdbc:mysql://localhost:3306/my_database"
    driverClassName = "com.mysql.jdbc.Driver"
    dialect = org.hibernate.dialect.MySQL5InnoDBDialect
    username = "username"
    password = "password"
    properties {
       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 = false
       jdbcInterceptors = "ConnectionState;StatementCache(max=200)"
       defaultTransactionIsolation = java.sql.Connection.TRANSACTION_READ_COMMITTED
    }
}