简体   繁体   English

Spring JTA事务管理器问题

[英]Spring JTA Transaction manager question

We are using jboss managed EntityMangerFactory using following spring bean 我们使用jboss管理的EntityMangerFactory使用以下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. 现在在我们的spring bean中,我们使用@PersistenceContext来获取实体管理器,它工作正常。 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? 我想要的是,我怎么能告诉spring抓住jbos jta服务提供的事务管理器并在我的dao中使用它?

If I define the txmanager like below then can spring will take controll of managing the transction with @Transaction annotation? 如果我像下面那样定义txmanager那么spring可以控制使用@Transaction注释来管理转换吗?

<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? 如果是这样,那么当spring将提交事务并回滚它时?

Thanks 谢谢

Almost - you should call it transactionManager rather than txManager . 差不多 - 你应该把它叫做transactionManager而不是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. 此外, JtaTransactionManager通常会自动检测各种JNDI名称,您不需要手动指定它们。

Better yet, don't declare JtaTransactionManager at all, just use <tx:jta-transaction-manager/> , and Spring should do the right thing . 更好的是,根本不要声明JtaTransactionManager ,只需使用<tx:jta-transaction-manager/>Spring应该做正确的事情

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, eg have transactions committed or rolled back when the annotated method exits ( see docs ). 一旦到位,任何使用@Transactional注释的bean都将具有由Spring管理的事务边界,例如,在注释方法退出时提交或回滚事务( 请参阅文档 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM