Grails 异步执行多个原生 sql 查询

标签 grails asynchronous grails-orm

我有一个执行多个 native 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");

最佳答案

您可以使用新的 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 -> }​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

关于Grails 异步执行多个原生 sql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29769953/

相关文章:

javascript - 带有闪烁器的Grails Flash消息

rest - spring-security-rest插件注销失败

grails - Grails渲染不会渲染 View

c# - WCF 调用一个函数而不等待它完成它的工作?

multithreading - sync::mpsc 意外关闭 channel

Grails GORM 组合还是hasOne?

grails - 如何使用Grails GORM进行多个字段匹配的左外部联接?

javascript - 如何添加新脚本并等待它在 Javascript 中执行

grails - 如何在 grails 中编写 hasMany 属性的约束

Grails/GORM : The meaning of belongsTo in 1:N relationships