Spring JTA事务管理器问题
我们正在使用 jboss 管理的 EntityMangerFactory 使用以下 spring bean
<jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence-units/myPU"/>
现在在我们的 spring bean 中我们使用 @PersistenceContext 来获取实体管理器并且它工作正常。我想要的是,我如何告诉spring获取jbos jta服务提供的事务管理器并在我的dao中使用它?
如果我像下面一样定义 txmanager 那么 spring 可以使用 @Transaction 注释来控制管理事务吗?
<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManagerName" value="java:/TransactionManager"/>
<property name="userTransactionName" value="UserTransaction"/>
</bean>
如果是这样那么spring什么时候会提交事务并回滚它?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
几乎 - 你应该将其称为
transactionManager
而不是txManager
。您可以覆盖它查找的名称,但遵守约定更容易。另外,
JtaTransactionManager
通常会自动检测各种 JNDI 名称,您不需要手动指定它们。更好的是,根本不声明
JtaTransactionManager
,只需使用
和 Spring 应该做正确的事情 。因此,您需要做的就是:
一旦到位,任何用@Transactional注释的bean都将由Spring管理其事务边界,例如,当注释的方法退出时提交或回滚事务(参见文档)。
Almost - you should call it
transactionManager
rather thantxManager
. 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:
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).