英文:
Entities are not fetching in Transaction event listener spring boot
问题
我有一个带有`@Transactional`注释的服务调用,它使用Spring Data JPA保存一些实体。
```java
@Override
@Async("tenant")
@Transactional
public List<TestDAO> testCreate(List<TestDAO> daos) {
List<Test> converted = convert(daos);
repository.save(converted);
}
保存后,使用applicationEventPublisher.publishEvent(object);
发布事件。
有一个事务监听器,我曾经在其中使用服务调用来调用和更新上面保存的实体。获取调用正常运行,但更新失败,说找不到实体。
@TransactionalEventListener(value = Event.class, phase = TransactionPhase.AFTER_COMMIT)
public void onApplicationEvent(Event event) {
Long id = event.getProcessId();
// 调用以前保存的数据。正常运行。
List<TestDAO> daos= testDAOServive.getItemByProcessID(id);
ExecutorService executorService = Executors.newFixedThreadPool(20);
daos.forEach(testDao -> {
Runnable task = () -> {
testDao.setStatus("IN_PROGRESS");
// 在这里出现异常。
testDAOServive.updateItem(testDao);
}
}
}
我尝试将所有方法都设置为事务性的,尝试在完成后进行事务处理。我已经删除了Executor服务,然后它就正常工作了。似乎相同的事务不会在线程中继续传递。
<details>
<summary>英文:</summary>
I have a service call which is annotated with `@Transactional` which save some entities using spring data JPA.
```java
@Override
@Async("tenant")
@Transactional
public List<TestDAO> testCreate(List<TestDAO> daos) {
List<Test> converted = convert(daos);
repository.save(converted);
}
After save pushing an event using applicationEventPublisher.publishEvent(object);
.
There is a transaction listener where I used to call and update above saved entities using service calls. Get call works perfectly, but update fails saying entity not found.
@TransactionalEventListener(value = Event.class, phase = TransactionPhase.AFTER_COMMIT)
public void onApplicationEvent(Event event) {
Long id = event.getProcessId();
//calling previously saved data. Works perfectly.
List<TestDAO> daos= testDAOServive.getItemByProcessID(id);
ExecutorService executorService = Executors.newFixedThreadPool(20);
daos.forEach(testDao -> {
Runnable task = () -> {
testDao.setStatus("IN_PROGRESS");
// Getting exception here.
testDAOServive.updateItem(testDao);
}
}
}
I tried making all the method as transactional. tried transaction after completion . I have removed Executor service then its working. Seems same transaction not getting carryforward in threads.
答案1
得分: 0
已添加 @Transactional 注释到事件监听方法,并且现在它正常工作。
@Transactional
public void onApplicationEvent(Event event) {
Long id = event.getProcessId();
// 调用先前保存的数据。完美运行。
List<TestDAO> daos = testDAOServive.getItemByProcessID(id);
ExecutorService executorService = Executors.newFixedThreadPool(20);
daos.forEach(testDao -> {
Runnable task = () -> {
testDao.setStatus("IN_PROGRESS");
// 在这里发生异常。
testDAOServive.updateItem(testDao);
};
});
}
英文:
Added @Transactional on event listening method and its working now.
**@Transactional**
public void onApplicationEvent(Event event) {
Long id = event.getProcessId();
//calling previously saved data. Works perfectly.
List<TestDAO> daos= testDAOServive.getItemByProcessID(id);
ExecutorService executorService = Executors.newFixedThreadPool(20);
daos.forEach(testDao -> {
Runnable task = () -> {
testDao.setStatus("IN_PROGRESS");
// Getting exception here.
testDAOServive.updateItem(testDao);
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论