eerriicc
eerriicc

Reputation: 1240

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?

Upvotes: 2

Views: 5008

Answers (1)

Korgen
Korgen

Reputation: 5409

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

Upvotes: 4

Related Questions