gpt4 book ai didi

java - HibernateTransactionManager @Transactional(propagation=REQUIRES_NEW) 无法打开 2 个 session

转载 作者:行者123 更新时间:2023-12-01 20:02:16 29 4
gpt4 key购买 nike

有一个批处理作业如下所示:

@Transactional
public void myBatchJob() {
// retrieves thousands of entries and locks them
// to prevent other jobs from touthing this dataset
entries = getEntriesToProcessWithLock();
additional = doPrepWork(); // interacts with DB
processor = applicationContext.getBean(getClass());
while (!entries.isEmpty()) {
result = doActualProcessing(entries, additional); // takes as many entries as it needs; removes them from collection afterwards
resultDao.save(result);
}
}

但是,如果 entries 集合足够大,我偶尔会收到以下错误。

ORA-01000: maximum open cursors exceeded

我决定责怪 doActualProcessing()save() 方法,因为它们最终可能会在一个事务中创建数百个 blob。

明显的出路似乎是将处理分成多个事务:一个用于获取和锁定条目,多个其他事务用于处理和持久化。像这样:

@Transactional
public void myBatchJob() {
// retrieves thousands of entries and locks them
// to prevent other jobs from touthing this dataset
entries = getEntriesToProcessWithLock();
additional = doPrepWork(); // interacts with DB
processor = applicationContext.getBean(getClass());
while (!entries.isEmpty()) {
processor.doProcess(entries, additional);
}
}

@Transactional(propagation=REQUIRES_NEW)
public void doProcess(entries, additional) {
result = doActualProcessing(entries, additional); // takes as many entries as it needs; removes them from collection afterwards
resultDao.save(result);
}

现在每当调用 doProcess 时我都会得到:

Caused by: org.hibernate.HibernateException: illegally attempted to associate a proxy with two open Sessions

如何让 HibernateTransactionManager 执行 REQUIRES_NEW javadoc 建议的操作:暂停当前事务并启动新事务?

最佳答案

在我看来,问题在于您已经检索了顶部事务中的实体,并且当它们仍然与该事务相关联时,您尝试将它们(代理)传递给将在单独的事务中处理的方法。

我认为你可以尝试两种选择:

1) 在调用之前分离实体 processor.doProcess(entries, extra);:

session.evict(entity); // loop through the list and do this

然后在内部事务中尝试合并:

session.merge(entity);

2) 第二个选项是检索 id,而不是 getEntriesToProcessWithLock 中的实体。然后您将传递不会导致代理问题的普通原始字段。然后,您将检索内部事务内的正确实体。

关于java - HibernateTransactionManager @Transactional(propagation=REQUIRES_NEW) 无法打开 2 个 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46363095/

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