如何在 Spring 服务的 @Transactional 方法中实现 Oracle ADF ApplicationModule 和 JPA 之间的共享事务?
how to have a Shared Transaction between Oracle ADF ApplicationModule and JPA in a @Transactional Method of a Spring Service?
我想从 Spring [=15= 的 @Transactional
方法调用 Oracle ADF ApplicationModule
的方法] 我也调用 JPA EntityManager
方法。这些调用需要在同一个事务中提交。两者使用相同的 DataSource
,我的应用程序服务器是 Weblogic 10.3.5。我怎样才能做到这一点?
当我使用 JtaTransactionManager
时,我的应用程序模块更改已提交,但 JPA
的更改是 而非 。当我使用 JpaTransactionManager
时,ApplicationModule
更改未提交。
这是我的 Spring 配置:
<?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>
这是我的服务class:
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。
您能否覆盖您的 JPA 部分的提交以首先提交 ADF 部分,并且仅当它有效时才提交 JPA 部分?
我认为您不能对两者使用一个事务管理器。唯一的例外可能是您将 AM 公开为 Web 服务并在外部管理对该 Web 服务的调用事务。
我简化了我的问题并删除了 ADF 部分并将其作为另一个问题发布在这里:
在得到 @Bond - Java Bond 的一些帮助和更多研究后,我找到了答案并将其发布在那个问题上。这个答案也适用于这个问题。
我想从 Spring [=15= 的 @Transactional
方法调用 Oracle ADF ApplicationModule
的方法] 我也调用 JPA EntityManager
方法。这些调用需要在同一个事务中提交。两者使用相同的 DataSource
,我的应用程序服务器是 Weblogic 10.3.5。我怎样才能做到这一点?
当我使用 JtaTransactionManager
时,我的应用程序模块更改已提交,但 JPA
的更改是 而非 。当我使用 JpaTransactionManager
时,ApplicationModule
更改未提交。
这是我的 Spring 配置:
<?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>
这是我的服务class:
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。
您能否覆盖您的 JPA 部分的提交以首先提交 ADF 部分,并且仅当它有效时才提交 JPA 部分?
我认为您不能对两者使用一个事务管理器。唯一的例外可能是您将 AM 公开为 Web 服务并在外部管理对该 Web 服务的调用事务。
我简化了我的问题并删除了 ADF 部分并将其作为另一个问题发布在这里:
在得到 @Bond - Java Bond 的一些帮助和更多研究后,我找到了答案并将其发布在那个问题上。这个答案也适用于这个问题。