我想从Spring ApplicationModule@Transactional方法中调用Oracle ADF Service的方法,我也将其称为JPA EntityManager方法。这些调用需要在同一事务中提交。两者都使用相同的DataSource,而我的Application Server是Weblogic 10.3.5。我该如何实现?

当我使用JtaTransactionManager时,我的应用程序模块更改已提交,但JPA的更改未提交。当我使用JpaTransactionManager时,不会提交ApplicationModule更改。

这是我的Spring Config:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
       http://www.springframework.org/schema/jee
       http://www.springframework.org/schema/jee/spring-jee-3.2.xsd">
    <context:component-scan base-package="testspring.view"/>
    <context:annotation-config/>
    <mvc:annotation-driven/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
                  value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <mvc:default-servlet-handler/>
    <bean id="dataSource" name="dataSource"
          class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="jdbc/hrDS"/>
        <property name="resourceRef" value="true"/>
    </bean>
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
    <bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="testspring.model"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="javax.persistence.validation.mode">AUTO</prop>
                <prop key="hibernate.archive.autodetection">class</prop>
                <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
                <prop key="hibernate.connection.charSet">UTF-8</prop>
                <prop key="hibernate.connection.useUnicode">true</prop>
                <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.transaction.flush_before_completion">true</prop>
                <prop key="hibernate.transaction.auto_close_session">true</prop>
                <prop key="hibernate.connection.release_mode">auto</prop>
            </props>
        </property>
    </bean>
    <bean id="transactionManager"
          class="org.springframework.transaction.jta.JtaTransactionManager"></bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>


这是我的服务班级:

package testspring.view;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import oracle.jbo.client.Configuration;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import testspring.adfmodel.AppModuleImpl;

import testspring.model.Regions;


@Service
public class HelloBS {
    @PersistenceContext
    private EntityManager entityManager;

    public HelloBS() {
        super();
    }

    @Transactional()
    public void doSomething() {
        AppModuleImpl am = null;
        String amDef = "testspring.adfmodel.AppModule";
        String config = "AppModuleLocal";

        am = (AppModuleImpl)Configuration.createRootApplicationModule(amDef, config);
        am.insertRegions("ADF");

        Regions region = new Regions();
        region.setRegionName("JPA");
        entityManager.persist(region);
    }
}


我想补充的一点是,我使用EJB Beans尝试了相同的场景,并且效果很好。

最佳答案

您正在寻找的是在两个环境之间共享相同的java.sql.Connection。

我不认为您可以将连接传递到ADF BC,因为AM可以透明地管理其连接,但是您可以使用ADF BC创建的连接并将其传递给Spring。
但是我不确定Spring是否可以像这样工作-您必须向比我有更多Spring经验的人咨询。

当然,您可以做的是在同一事务中使用ADF BC和JDBC。

10-06 05:02
查看更多