2015-03-02 65 views
0

我有一個Grails應用程序,其中有兩個Quartz作業每10秒運行一次。它可以在一段時間內正常工作,但一段時間後我仍然收到以下錯誤。使用Quartz調度程序在Grails中關閉連接

[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) 
     } 


     } 
    } 

可否請你讓我知道了這個錯誤的可能原因是什麼?

回答

1

上次我的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 
    } 
}