有些东西没有被冲洗掉。正在发生的事情的简化示例:
def testDemo() {
def person = new Person(...)
person.save(flush: true)
println "Number of people after save: " + Person.all.size()
def dummyList = [1, 2, 3, 4, 5]
GParsPool.withPool { num ->
println "Number of people after withPool: " + Person.all.size()
dummyList.eachParallel {
println "Number of people after eachParallel " + Person.all.size()
Person.withTransaction {
...
这输出:
Number of people after save: 1
Number of people after withPool: 1
Number of people after eachParallel: 0
我不明白我是否必须对 Session 和 Transaction 做一些事情以使数据持久存在,或者这是否是 GPars 中的错误。底层 hibernate 级别发生了什么?
我希望最近创建的 Person 在并行闭包中可见。
最佳答案
Gpars 是一个多线程工具,注入(inject)到您的域类中的 hibernate session 不是线程安全的。
尝试使用这些方法或直接调用 SessionFactory:
请注意,为每个线程打开一个 session 可能会非常昂贵,并且可能会用新的连接淹没您的数据库。
关于hibernate - Grails、GPars 和数据持久性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14079194/