简体   繁体   English

Spring Integration和JPA事务

[英]Spring Integration and JPA transactions

I'm using Spring integration to receive XML formal XMPP messages : 我正在使用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" />

I have already some persistence features and it works (storing messages in database, analysing message compared to what's in database, ...). 我已经有了一些持久性功能,并且可以正常工作(将消息存储在数据库中,将消息与数据库中的消息进行比较,...)。

Now I have a method which need to be transactional because I need to initialize 2 collections related to 1 entity, so the second collection is lazy initialized by calling a method on it. 现在我有一个需要事务处理的方法,因为我需要初始化与1个实体相关的2个集合,因此第二个集合是通过对其上调用方法来进行延迟初始化的。

This works fine in JUnit, but what do I need to do to make it work in the main code ? 这在JUnit中可以正常工作,但是我需要做些什么才能使其在主代码中起作用? Can I just put @Transactional at any level, or does it need to be at the root of the whole process or even declared in some way in the inbound adapter ? 我可以将@Transactional放在任何级别吗,还是需要将它放在整个过程的根本,甚至需要在入站适配器中以某种方式声明?

If your hard work with JPA and database is really just in the mentioned messageRouterService , then it is indeed should be enough to mark that service method with the @Transactional annotation and refer properly to your JpaTransactionManager . 如果您真正的JPA和数据库工作只是在提到的messageRouterService ,那么使用@Transactional批注标记该服务方法并正确引用JpaTransactionManager确实足够了。

If think to make the whole flow as transactional, then it isn't possible starting with the <int-xmpp:inbound-channel-adapter> because there is no hooks to inject TransactionInterceptor . 如果考虑使整个流程具有事务性,那么就不可能从<int-xmpp:inbound-channel-adapter>因为没有钩子可以插入TransactionInterceptor However you can use a TransactionInterceptorBuilder(true) to produce a TransactionHandleMessageAdvice which will wraps the whole sub-flow starting with the adviced endpoint via its <request-handler-advice-chain> : https://docs.spring.io/spring-integration/docs/5.0.6.RELEASE/reference/html/messaging-endpoints-chapter.html#message-handler-advice-chain 但是,您可以使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM