【发布时间】:2013-12-11 10:54:30
【问题描述】:
我在使用 grails 处理数百万条数据库记录方面需要帮助。
我在数据库中有数百万条记录。我无法在单个查询中获取所有记录,因为它会抛出内存异常。所以我想批量获取记录。并处理每批的每条记录。 我想在 grails 中实现阻塞队列,我将在队列中添加所有子查询的结果,然后多个线程将消耗队列并进行自己的处理。
我怎样才能做到这一点?
【问题讨论】:
标签: mysql grails concurrency
我在使用 grails 处理数百万条数据库记录方面需要帮助。
我在数据库中有数百万条记录。我无法在单个查询中获取所有记录,因为它会抛出内存异常。所以我想批量获取记录。并处理每批的每条记录。 我想在 grails 中实现阻塞队列,我将在队列中添加所有子查询的结果,然后多个线程将消耗队列并进行自己的处理。
我怎样才能做到这一点?
【问题讨论】:
标签: mysql grails concurrency
这可能不是您想要的有效解决方案,但是,使用
bootstarp.groovy 文件我处理过类似的问题,例如从 excel 文件中读取并将其添加到数据库中,每条记录使用线程逐行获取。
import grails.transaction.Transactional;
BootStrap.groovy
@Transactional
def importformExcel(){
// add you code here for batching...and call it inside the thread
}
在bootstrap.init 部分添加以下代码以在启动时执行它。您可以添加一些逻辑以防止每次运行或使其一次执行并永久停止。
def init = { servletContext ->
sessionFactory.currentSession.flush()
try{
@Transactional
Thread stateLgaThread = new Thread({
//code ur thread logic here!
importformExcel()
//sessionFactory.currentSession.flush()
TimerThread.sleep(100) // even give more chance to other application/instruction
} as Runnable).start()
@Transactional
Thread regionThread = new Thread({
//code ur thread logic here!
importformExcel()
// sessionFactory.currentSession.flush()
TimerThread.sleep(100) // even give more chance to other application/instruction
} as Runnable).start()
}
catch(ex){
log.debug "Inside Thread Exception:{ex.message}"
}
}
【讨论】: