简体   繁体   中英

How can I use the same transaction in Spring and an EJB?

I have no development experience with Spring, since we're a Java EE shop. However, we are looking into a solution for our needs that runs on Spring and should integrate our existing Java EE solution.

After reading the Spring 3.0.5 documentation, I'm still uncertain how a transaction can be propagated from Spring to an EJB.

For instance, a Spring bean would create a transaction, save some stuff into one database and then hand over the transaction to one of our stateless session beans, which (using JPA) saves some other stuff into another database. And all of this must run under the same transaction, which is committed when the control is returned to the Spring bean.

Also, I'm not clear about the deployment: Would Spring run as a webapp in the EAR that contains the session beans?

if you configure Spring to use a JTATransactionManager and then call your SessionBean from within Spring the bean should actually pick up the Spring-created transaction. Have look here: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#transaction-application-server-integration and the described <tx:jta-transaction-manager/>

I actually only did it the other way around (joining EJB Transaction in spring) so I'm not completely sure, but you can easily test it by setting

@TransactionAttribute(TransactionAttributeType.MANDATORY)

on your SessionBean and call it from spring... if there's not existing Transaction you will end up with a TransactionRequiredException

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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