实体在事务事件监听器中未获取到的问题。

huangapple go评论92阅读模式
英文:

Entities are not fetching in Transaction event listener spring boot

问题

  1. 我有一个带有`@Transactional`注释的服务调用它使用Spring Data JPA保存一些实体
  2. ```java
  3. @Override
  4. @Async("tenant")
  5. @Transactional
  6. public List<TestDAO> testCreate(List<TestDAO> daos) {
  7. List<Test> converted = convert(daos);
  8. repository.save(converted);
  9. }

保存后,使用applicationEventPublisher.publishEvent(object);发布事件。

有一个事务监听器,我曾经在其中使用服务调用来调用和更新上面保存的实体。获取调用正常运行,但更新失败,说找不到实体。

  1. @TransactionalEventListener(value = Event.class, phase = TransactionPhase.AFTER_COMMIT)
  2. public void onApplicationEvent(Event event) {
  3. Long id = event.getProcessId();
  4. // 调用以前保存的数据。正常运行。
  5. List<TestDAO> daos= testDAOServive.getItemByProcessID(id);
  6. ExecutorService executorService = Executors.newFixedThreadPool(20);
  7. daos.forEach(testDao -> {
  8. Runnable task = () -> {
  9. testDao.setStatus("IN_PROGRESS");
  10. // 在这里出现异常。
  11. testDAOServive.updateItem(testDao);
  12. }
  13. }
  14. }

我尝试将所有方法都设置为事务性的,尝试在完成后进行事务处理。我已经删除了Executor服务,然后它就正常工作了。似乎相同的事务不会在线程中继续传递。

  1. <details>
  2. <summary>英文:</summary>
  3. I have a service call which is annotated with `@Transactional` which save some entities using spring data JPA.
  4. ```java
  5. @Override
  6. @Async(&quot;tenant&quot;)
  7. @Transactional
  8. public List&lt;TestDAO&gt; testCreate(List&lt;TestDAO&gt; daos) {
  9. List&lt;Test&gt; converted = convert(daos);
  10. repository.save(converted);
  11. }

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.

  1. @TransactionalEventListener(value = Event.class, phase = TransactionPhase.AFTER_COMMIT)
  2. public void onApplicationEvent(Event event) {
  3. Long id = event.getProcessId();
  4. //calling previously saved data. Works perfectly.
  5. List&lt;TestDAO&gt; daos= testDAOServive.getItemByProcessID(id);
  6. ExecutorService executorService = Executors.newFixedThreadPool(20);
  7. daos.forEach(testDao -&gt; {
  8. Runnable task = () -&gt; {
  9. testDao.setStatus(&quot;IN_PROGRESS&quot;);
  10. // Getting exception here.
  11. testDAOServive.updateItem(testDao);
  12. }
  13. }
  14. }

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 注释到事件监听方法,并且现在它正常工作。

  1. @Transactional
  2. public void onApplicationEvent(Event event) {
  3. Long id = event.getProcessId();
  4. // 调用先前保存的数据。完美运行。
  5. List<TestDAO> daos = testDAOServive.getItemByProcessID(id);
  6. ExecutorService executorService = Executors.newFixedThreadPool(20);
  7. daos.forEach(testDao -> {
  8. Runnable task = () -> {
  9. testDao.setStatus("IN_PROGRESS");
  10. // 在这里发生异常。
  11. testDAOServive.updateItem(testDao);
  12. };
  13. });
  14. }
英文:

Added @Transactional on event listening method and its working now.

  1. **@Transactional**
  2. public void onApplicationEvent(Event event) {
  3. Long id = event.getProcessId();
  4. //calling previously saved data. Works perfectly.
  5. List&lt;TestDAO&gt; daos= testDAOServive.getItemByProcessID(id);
  6. ExecutorService executorService = Executors.newFixedThreadPool(20);
  7. daos.forEach(testDao -&gt; {
  8. Runnable task = () -&gt; {
  9. testDao.setStatus(&quot;IN_PROGRESS&quot;);
  10. // Getting exception here.
  11. testDAOServive.updateItem(testDao);
  12. }
  13. }
  14. }

huangapple
  • 本文由 发表于 2023年6月19日 23:31:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76508100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定