Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring JTA Transaction manager question

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

like image 351
user509755 Avatar asked Apr 19 '11 21:04

user509755


1 Answers

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

like image 75
skaffman Avatar answered Oct 04 '22 21:10

skaffman