java - Why are JPA changes using an EntityManager not committed in a Spring Service when JtaTransactionManager is used? -
i need use org.springframework.transaction.jta.jtatransactionmanager
transactionmanager
in spring service
. however, not commit change in jpa entities. know if use jpatransactionmanager
works. but, need jtatransactionmanager
. so, please don't recommend use jpatransactionmanager
. 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 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 have changes in jpa entities committed using jtatransactionmanager
?
thanks @bond - java bond solution 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 different classes inorg.hibernate.service.jta.platform.internal
package can used different application servers.
so 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>
Comments
Post a Comment