【问题标题】:Spring JTA transaction with JPA and jndi datasource for Websphere用于 Websphere 的带有 JPA 和 jndi 数据源的 Spring JTA 事务
【发布时间】:2012-09-25 07:22:35
【问题描述】:

我有多个数据源和一个配置了 JPA 的数据库。我正在使用 websphere 7。我希望将所有这些数据源配置为全局事务。我正在使用以下弹簧配置,但交易未按预期的全球交易工作。如果一个数据库出现故障,则另一个数据库将被提交,这不是单个全局事务。你能帮我解决我做错的地方吗,

我有 2 个数据源,其中一个使用 id="us_icfs_datasource" 配置如下,另一个使用 JPA

<jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence/persistenceUnit"/> 
    <bean id="pabpp" class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" />

<!-- Needed for @Transactional annotation -->
    <tx:annotation-driven/>

<jee:jndi-lookup id="US_ICFS_DATASORCE" 
        jndi-name="jdbc/financing_tools_docgen_txtmgr"
        cache="true"
        resource-ref="true"
        proxy-interface="javax.sql.DataSource" />

我还在 web.xml 中添加了以下代码

 <persistence-unit-ref>
    <persistence-unit-ref-name>persistence/persistenceUnit</persistence-unit-ref-name>
    <persistence-unit-name>persistenceUnit</persistence-unit-name>
  </persistence-unit-ref> 

  <persistence-context-ref>
    <persistence-context-ref-name>persistence/persistenceUnit</persistence-context-ref-name>
    <persistence-unit-name>persistenceUnit</persistence-unit-name>
  </persistence-context-ref>

下面是我使用事务的代码

> @Transactional    public TemplateMapping addTemplateMapping(User user,
> TemplateMapping templateMapping)          throws
> TemplateMappingServiceException {         .... }

【问题讨论】:

    标签: spring jta


    【解决方案1】:

    在 Websphere 上,您应该使用此 bean 连接到 Websphere 事务管理器:

    <bean id="transactionManager"
         class="org.springframework.transaction.jta.WebSphereUowTransactionManager"/>
    

    另见article

    编辑:

    为了使用两阶段提交(即确保跨多个资源的一致性),您需要使用 XA 数据源。详情请见this article

    【讨论】:

    • 我们也可以有两个不同的事务管理器,一个名为 transactionManager,另一个名为 wsTransactionManager,这样我的 bean 中只有一个使用 wsTransactionManager,rest 自动使用 transactionManager
    • 为什么要使用两个不同的 txn 管理器 bean?最后,无论您使用JTATransactionManager 还是WebSphereUowTransactionManager,都将使用Websphere txn 管理器,UOW bean 只是让您可以更好地使用引擎盖下的websphere 功能...
    • 嘿,非常感谢更改为 WebshphereUowTransactionManager 的解决方案,但如果我更改为 websphereuowtransactionmanger,我仍然没有按预期得到回滚。尽管我的异常是从第二个数据库返回的,但一个数据库正在提交。下面是我的代码
    • 为了使用两阶段提交,您需要确保两个数据源都设置为 websphere 中的 XA 数据源(另请参阅我在答案中添加的链接) .如果你想添加代码,我会编辑上面的问题。
    • 是的。如果您提供一些片段会很好。因为如果我使用 XADatasource 那么下面的代码不起作用...class JFFS00AStoredProcedure extends StoredProcedure { public JFFS00AStoredProcedure(DataSource dataSource, String spName) { super(dataSource, spName);.....} 这里 Spring StoredProcedure 类只能使用 Datasource不是 XADataSource
    【解决方案2】:

    首先你参与全局事务的数据源必须是 javax.sql.XADataSource 类型。

    您还必须将持久性单元中的事务类型设置为 JTA(而不是 RESOURCE_LOCAL)。

    并且您需要通知您的 JPA 实现您想要进行全局事务。

    【讨论】:

    • 我该怎么做,并且您需要通知您的 JPA 实现您想要进行全局事务。?如果我在下面更改为 XA 数据源,那么我基于 spring 的存储过程不工作
    • 你使用什么 JPA 实现?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多