OpenJPA合并/持久化非常慢 [英] OpenJPA merging/persisting is very slow
问题描述
我在带有MySQL 5.0 DB的WebSphere Application Server 8上使用OpenJPA 2.2.0.
I use OpenJPA 2.2.0 on WebSphere Application Server 8 with a MySQL 5.0 DB.
我有一个要合并到数据库中的对象列表.
I have a list of objects which I want to merge into the DB.
就像:
for (Object ob : list) {
Long start = Calendar.getInstance().getTimeInMillis();
em = factory.createEntityManager();
em.getTransaction().begin();
em.merge(ob);
em.getTransaction().commit();
em.close();
Long end = Calendar.getInstance().getTimeInMillis();
Long diff = end - start;
LOGGER.info("Time: " + diff);
}
运行此循环时,我需要大约300-600毫秒来合并一个对象.当我删除"em.merge(ob);"行时那么我需要"0"毫秒来遍历1个列表对象.
When I run this loop I need about 300-600 Milliseconds to merge one object. When I delete the line "em.merge(ob);" then I need "0" Milliseconds to iterate over 1 List Object.
所以我的问题是:我该怎么做才能缩短合并一个对象的时间?
So my question is: What can I do to improve the time to merge one object?
谢谢!
推荐答案
您可以在迭代&之前尝试启动事务.然后在一次交易中将其提交.因此,基本上,您正在创建一个将在提交时合并/持久化的批处理.
You can try starting the transaction before iteration & then commiting it afterwards within a single transaction. So, basically you are creating a batch which would be merged/persisted on commit.
此外,您可以一次限制要处理的批次中的对象数量,可以将更改显式刷新到数据库中.
Also, you can limit the number of objects in a batch to be processed at a time & can explicitly flush the changes into database.
在这里,您正在发起交易&在每次迭代中提交它,以及每次创建/关闭实体管理器都会影响大量数据的性能.
Here, you are initiating a transaction & commiting it in each iteration and also creating/closing entity manager each time, will affect performance for numerous data.
这将类似于下面的代码.
It will be something like below code.
em = factory.createEntityManager();
em.getTransaction().begin();
int i = 0;
for (Object ob : list) {
Long start = Calendar.getInstance().getTimeInMillis();
em.merge(ob);
Long end = Calendar.getInstance().getTimeInMillis();
Long diff = end - start;
LOGGER.info("Time: " + diff);
/*BATCH_SIZE is the number of entities
that will be persisted/merged at once */
if(i%BATCH_SIZE == 0){
em.flush();
em.clear();
}
i++;
}
em.getTransaction().commit();
em.close();
在这里,如果任何对象未能持久/合并,您还可以回滚整个事务.
Here, you can also rollback the whole transaction if any of the object fails to persist/merge.
这篇关于OpenJPA合并/持久化非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!