openJPA 批量更新模式 [英] openJPA batch update pattern

查看:31
本文介绍了openJPA 批量更新模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要更新数千个实体,无法将逻辑放入 SQL 语句中.使用非托管 JPA 时,我使用的模式是:

I need to update thousands of Entities and the logic can not be put into a SQL statement. When using non-managed JPA the pattern I use is:

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 
}

在托管环境中,这会导致

In a managed environment this results in a

java.lang.IllegalStateException: Transaction management is not available for container managed EntityManagers.

在使用容器管理的事务和蓝图时,这种类型的用例有什么好的模式?如果重要的话,我的特定环境是 karaf 3.0.5 和 openJPA 2.3.x.

What is a good pattern for this type of use case when using container managed transactions and blueprint? My specific environment is karaf 3.0.5 and openJPA 2.3.x if it matters.

推荐答案

更新你的 persistence.xml 配置以使用语句批处理

Update your persistence.xml configuration to use statement batching

OpenJPA 语句批处理

这篇关于openJPA 批量更新模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆