Grails异步执行多个本机SQL查询

时间:2015-04-21 10:44:42

标签: grails asynchronous gorm

我有一个执行多个本机sql语句的服务。是否可以异步运行这些sql语句?我的查询看起来像这样多个查询:

def sessionFactory
sessionFactory = ctx.sessionFactory  // this only necessary if your are working with the Grails console/shell
def session = sessionFactory.currentSession 

def query = session.createSQLQuery("select f.* from fee f where f.id = :filter)) order by f.name");

1 个答案:

答案 0 :(得分:0)

您可以使用新的Grails Promises API

import static grails.async.Promises.*

List queriesToExecute = []

someList.each {
    //create task to execute query
    def t = task {
        //create and execute query here
      }

   queriesToExecute << t
}

def result = waitAll(queriesToExecute) //if you want to wait till queries are executed
//or if you dont want to wait, use this - onComplete(queriesToExecute) { List results -> }​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​