gpt4 book ai didi

multithreading - GPar,GORM,批处理上传

转载 作者:行者123 更新时间:2023-12-02 14:41:28 24 4
gpt4 key购买 nike

我有一个流程,需要构建一个PDF文档,并将其与100多个正在创建的其他文档结合在一起。

我想为此多线程。我将向其中传递需要生成PDF并保存到DB的PDF事件的列表。该文档也保存到目标位置,并且路径保存到DB。

所以我正在使用10个线程的GPar。我遇到一个问题,当我从事件中延迟获取时,会收到一条错误消息

failed to lazily initialize a collection of role: com.srm.billing.DepositNotice.depositNoticeRevisions, no session or session was closed; nested exception is org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.srm.billing.DepositNotice.depositNoticeRevisions, no session or session was closed



我注意到几个站点都在说Hibernate不是线程安全的,这是导致问题的原因。
GParsPool.withPool( poolSize ) { forkJoinPool ->
depositNotices = pEvents.collectParallel{ event ->
Event.withNewSession{ session ->
return buildDepositNotice( event )
session.flush()
}
}
}

byte[] findlDocument = null
finalDocument = pdfUtilService.concatPDFs( depositNotices )

我看到的结果是,当发生此错误时,先前处理的事件将提交给数据库,这是一个问题。

我的最终目标是将传递到方法中的事件列表作为一个批处理完成,如果1失败,则不会将任何记录提交给DB。

最佳答案

从技术上讲,collectParallel()旨在与无副作用的代码一起使用。但是您仍然可以谨慎使用它。

假设buildDepositNotice(Event)返回一个已保存的域实例,则在Event.withNewSession(Closure)闭包之外使用该实例时,它将被分离。分离实例后,您可以访问它的属性等,但是要保留它,您必须先将其重新连接到当前 session 。

要收集存款通知,请确保实例是withNewSession()闭包中最后计算的表达式。

Event.withNewSession{ session ->
def depositNotice = buildDepositNotice(event)

session.flush()
depositNotice
}

而且,如果 pdfUtilService.concatPDFs()需要保留存款通知书,那么请确保它首先在存款通知书上调用 attach()

关于multithreading - GPar,GORM,批处理上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33289710/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com