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

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

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(&quot;tenant&quot;)
@Transactional
public List&lt;TestDAO&gt; testCreate(List&lt;TestDAO&gt; daos) {
  List&lt;Test&gt; 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&lt;TestDAO&gt; daos=  testDAOServive.getItemByProcessID(id);				
   ExecutorService executorService = Executors.newFixedThreadPool(20);
   daos.forEach(testDao -&gt; {
	 Runnable task = () -&gt; {
       	testDao.setStatus(&quot;IN_PROGRESS&quot;);
	    // 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&lt;TestDAO&gt; daos=  testDAOServive.getItemByProcessID(id);              
   ExecutorService executorService = Executors.newFixedThreadPool(20);
   daos.forEach(testDao -&gt; {
     Runnable task = () -&gt; {
        testDao.setStatus(&quot;IN_PROGRESS&quot;);
        // Getting exception here.
        testDAOServive.updateItem(testDao);             
     }
   }
}

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:

确定