【发布时间】:2023-04-09 22:00:01
【问题描述】:
我需要更新数千个实体,并且无法将逻辑放入 SQL 语句中。使用非托管 JPA 时,我使用的模式是:
long commitThreshold = 100; // or other appropriate value
try {
em.beginTransction().begin();
for(list of entities to be modified) {
// retrieve current Entity
// modify current Entity
if((++modifiedEntityCount % commitThreshold) == 0) {
em.getTransaction().commit();
em.getTransaction().begin();
}
}
if(em.getTransaction().isActive()) {
em.getTransaction().commit();
}
catch () {
}
finally {
// cleanup
}
在托管环境中,这会导致
java.lang.IllegalStateException: Transaction management is not available for container managed EntityManagers.
在使用容器管理事务和蓝图时,这种类型的用例有什么好的模式?我的具体环境是 karaf 3.0.5 和 openJPA 2.3.x 如果重要的话。
【问题讨论】: