开发者

Spring JTA Transaction manager question

开发者 https://www.devze.com 2023-02-26 21:06 出处:网络
We are using jboss managed EntityMangerFactory using following spring bean <jee:jndi-lookup id=\"entityManagerFactory\" jndi-name=\"persistence-units/myPU\"/>

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"/>
</be开发者_运维知识库an> 

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

Thanks


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

0

精彩评论

暂无评论...
验证码 换一张
取 消