![](/img/trans.png)
[英]Transactions in Spring Integration's DefaultMessageListenerContainer and JPA
[英]Spring Integration and JPA transactions
我正在使用Spring集成來接收XML正式XMPP消息:
<int-xmpp:inbound-channel-adapter id="xmppInboundAdapter" channel="xmppInbound" xmpp-connection="xmppConnection" auto-startup="true" />
<int:channel id="xmppInbound" />
<int:service-activator ref="messageRouterService" input-channel="xmppInbound" />
我已經有了一些持久性功能,並且可以正常工作(將消息存儲在數據庫中,將消息與數據庫中的消息進行比較,...)。
現在我有一個需要事務處理的方法,因為我需要初始化與1個實體相關的2個集合,因此第二個集合是通過對其上調用方法來進行延遲初始化的。
這在JUnit中可以正常工作,但是我需要做些什么才能使其在主代碼中起作用? 我可以將@Transactional放在任何級別嗎,還是需要將它放在整個過程的根本,甚至需要在入站適配器中以某種方式聲明?
如果您真正的JPA和數據庫工作只是在提到的messageRouterService
,那么使用@Transactional
批注標記該服務方法並正確引用JpaTransactionManager
確實足夠了。
如果考慮使整個流程具有事務性,那么就不可能從<int-xmpp:inbound-channel-adapter>
因為沒有鈎子可以插入TransactionInterceptor
。 但是,您可以使用TransactionInterceptorBuilder(true)
生成TransactionHandleMessageAdvice
,它將通過其<request-handler-advice-chain>
從建議的端點開始包裝整個子流: https : //docs.spring.io/spring-集成/文檔/ 5.0.6.RELEASE /參考/ HTML /消息端點-chapter.html#消息處理,建議鏈
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.