为什么在使用JtaTransactionManager时,JPA使用未在Spring服务中提交的EntityManager进行更改? [英] Why are JPA changes using an EntityManager not committed in a Spring Service when JtaTransactionManager is used?
问题描述
我需要在Spring <$ c中使用 org.springframework.transaction.jta.JtaTransactionManager
作为我的 TransactionManager
$ C>服务。但是,它不会在JPA实体中进行任何更改。我知道如果我使用 JpaTransactionManager
它可以工作。但是,我需要 JtaTransactionManager
。所以,请不要使用 JpaTransactionManager
。
My Spring Service
class是:
package testspring。视图;
导入javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
导入testspring.model.Regions;
@Service
公共类HelloBS {
@PersistenceContext
私有EntityManager entityManager;
public HelloBS(){
super();
@Transactional()
public void doSomething(){
Regions region = new Regions();
region.setRegionName(Antarctica);
entityManager.persist(region);
我的Spring xml配置是:
<?xml version =1.0encoding =UTF-8standalone =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/上下文
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 =prefixvalue =/ WEB-INF / jsp //>
< property name =suffixvalue =。jsp/>
< / bean>
< mvc:default-servlet-handler />
< bean id =dataSourcename =dataSource
class =org.springframework.jndi.JndiObjectFactoryBean>
< property name =jndiNamevalue =jdbc / hrDS/>
< property name =resourceRefvalue =true/>
< / bean>
< bean class =org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor/>
< bean id =entityManagerFactory
class =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =packagesToScanvalue =testspring.model/>
< property name =jpaVendorAdapter>
< bean class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter/>
< / property>
< property name =jpaProperties>
<道具>
< 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>
< /道具>
< / property>
< / bean>
< bean id =transactionManager
class =org.springframework.transaction.jta.JtaTransactionManager>< / bean>
< tx:注解驱动的事务管理器=transactionManager/>
< / beans>
如何在使用 JtaTransactionManager $ c提交的JPA实体中进行更改
$ div class =h2_lin>解决方案
感谢 @Bond - Java Bond 解决方案是:步骤:
$ b
- 设置
jtaDataSource
而不是dataSource code>
- 使用
< tx:jta-transaction-manager />
来代替< tx:annotation-driven transaction-manager =transactionManager/>
。 - 添加
< prop key = hibernate.transaction.jta.platform> org.hibernate.service.jta.platform.internal.WeblogicJtaPlatform< /丙>
。org.hibernate.service.jta.platform.internal
包中有不同的类,可用于不同的应用程序服务器。
所以最终的Spring xml配置是:
<?xml version = 1.0encoding =UTF-8standalone =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/s chema / 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 =prefixvalue =/ WEB-INF / jsp //>
< property name =suffixvalue =。jsp/>
< / bean>
< mvc:default-servlet-handler />
< bean id =dataSourcename =dataSource
class =org.springframework.jndi.JndiObjectFactoryBean>
< property name =jndiNamevalue =jdbc / hrDS/>
< property name =resourceRefvalue =true/>
< / bean>
< bean class =org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor/>
< bean id =entityManagerFactory
class =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =jtaDataSourceref =dataSource/>
< property name =packagesToScanvalue =testspring.model/>
< property name =jpaVendorAdapter>
< bean class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter/>
< / property>
< property name =jpaProperties>
<道具>
< 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>
< prop key =hibernate.transaction.jta.platform> org.hibernate.service.jta.platform.internal.WebternalJtaPlatform< / prop>
< /道具>
< / property>
< / bean>
< tx:jta-transaction-manager />
< / beans>
I need to use org.springframework.transaction.jta.JtaTransactionManager
as my TransactionManager
in Spring Service
. However, it does not commit any change in JPA entities. I know that if I use JpaTransactionManager
it works. But, I need JtaTransactionManager
. So, please don't recommend to use JpaTransactionManager
.
My Spring Service
class is:
package testspring.view;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import testspring.model.Regions;
@Service
public class HelloBS {
@PersistenceContext
private EntityManager entityManager;
public HelloBS() {
super();
}
@Transactional()
public void doSomething() {
Regions region = new Regions();
region.setRegionName("Antarctica");
entityManager.persist(region);
}
}
And my Spring xml config is:
<?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>
How can I have changes in JPA entities committed using JtaTransactionManager
?
Thanks to @Bond - Java Bond the solution is to do the following steps:
- Set
jtaDataSource
instead ofdataSource
. - Use
<tx:jta-transaction-manager/>
instead of<tx:annotation-driven transaction-manager="transactionManager"/>
. - Add
<prop key="hibernate.transaction.jta.platform">org.hibernate.service.jta.platform.internal.WeblogicJtaPlatform</prop>
. There are different classes inorg.hibernate.service.jta.platform.internal
package that can be used for different Application Servers.
So the final Spring xml config is:
<?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="jtaDataSource" 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>
<prop key="hibernate.transaction.jta.platform">org.hibernate.service.jta.platform.internal.WeblogicJtaPlatform</prop>
</props>
</property>
</bean>
<tx:jta-transaction-manager/>
</beans>
这篇关于为什么在使用JtaTransactionManager时,JPA使用未在Spring服务中提交的EntityManager进行更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!