簡體   English   中英

Grails,GPars和數據持久性

[英]Grails, GPars and data persistence

有些東西沒有被沖洗。 發生了什么的簡化示例:

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是一個多線程工具,在您的域類中注入的hibernate會話不是線程安全的。

嘗試使用這些方法或直接調用SessionFactory:

  • withNewSession
  • withNewTransaction

請注意,為每個線程打開會話可能會非常昂貴,並且可能會使用新連接充斥您的數據庫。

我最近遇到了類似的問題。 據我所知,似乎線程無法綁定hibernate會話,我也無法讓它工作。 如果你真的不需要它,請嘗試編寫處理GPars持久性的代碼。 這就是我開始工作的方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM