Spring JTA Transaction manager question

user509755 picture user509755 · Apr 19, 2011 · Viewed 11.1k times · Source

We are using jboss managed EntityMangerFactory using following spring bean

<jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence-units/myPU"/>

Now in our spring bean we use @PersistenceContext to get the entitymanager and it works fine. What I want is that how can i tell spring to grab the transaction manager provided by jbos jta service and use it in my dao?

If I define the txmanager like below then can spring will take controll of managing the transction with @Transaction annotation?

<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
      <property name="transactionManagerName" value="java:/TransactionManager"/>
    <property name="userTransactionName" value="UserTransaction"/>
</bean> 

If so then when spring will commit the transaction and roll back it?

Thanks

Answer

skaffman picture skaffman · Apr 19, 2011

Almost - you should call it transactionManager rather than txManager. You can override the name that it looks for, but it's easier to stick to the convention.

Also, JtaTransactionManager will generally auto-detect the various JNDI names, you shouldn't need to specify them manually.

Better yet, don't declare JtaTransactionManager at all, just use <tx:jta-transaction-manager/>, and Spring should do the right thing.

So, all you should need is:

<context:annotation-driven/>
<tx:jta-transaction-manager/> 

Once that's in place, any beans annotated with @Transactional will have their transaction boundaries managed by Spring, e.g. have transactions committed or rolled back when the annotated method exits (see docs).