Spring JTA Transaction manager question
Asked Answered
D

1

5

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

Dyaus answered 19/4, 2011 at 21:26 Comment(0)
S
17

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).

Smuggle answered 19/4, 2011 at 21:28 Comment(2)
Thanks skaffman. This is working fine. By the way I didn't understand <tx:jta-transaction manager> configuration. How does spring will find transaction provided by server?Dyaus
@user509755: By looking for it at a known list of possible locations.Smuggle

© 2022 - 2024 — McMap. All rights reserved.