gpt4 book ai didi

spring - 在@Transactional 中的事务期间提交

转载 作者:行者123 更新时间:2023-12-04 22:48:52 25 4
gpt4 key购买 nike

是否可以在标记为 Spring 的 的方法中执行提交? @事务 ?

@PersistenceContext
private EntityManager em;

@Transactional(propagation = Propagation.REQUIRED)
public void saveMembersWithMultipleCommits(List<Member> members)
throws HibernateException
{
Iterator<Member> it = members.iterator();
while (it.hasNext())
{
while (it.hasNext())
{
Member wsBean = it.next();
em.persist(wsBean); // overall commit will be made after method exit
log.info("Webservices record " + wsBean + " saved. " + i++);
}
}
}

我想在说每 500 个项目后提交给 DB。在上述背景下这可能吗?

最佳答案

不,您需要以编程方式使用,例如 TransactionTemplate API。阅读更多 here .

它看起来像

while (it.hasNext())
{
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
int counter = 0;
while (it.hasNext() && counter++ < 500) {
Member wsBean = it.next();
em.persist(wsBean);
log.info("Webservices record " + wsBean + " saved. " + i++);
}
}
);
}

关于spring - 在@Transactional 中的事务期间提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12897882/

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