如何解决jpaTemplate和jdbcTemplate 共享一个事务处理
发布网友
发布时间:2022-04-08 01:38
我来回答
共1个回答
热心网友
时间:2022-04-08 03:08
1.xml
xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
<!-- property name="proxyTargetClass" value="true"></property -->
</bean>
<bean id="railWeightEntityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="railWeightEntityManager" />
<property name="jpaProperties">
<props></props>
</property>
</bean>
<bean id="railWeightTransactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="railWeightEntityManagerFactory" />
</bean>
<bean id="railWeightTransactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="railWeightTransactionManager" />
<property name="transactionAttributeSource">
<bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource" />
</property>
</bean>
<bean id="railWeightTransactionAttributeSourceAdvisor"
class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
<property name="transactionInterceptor" ref="railWeightTransactionInterceptor" />
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="railWeightEntityManager" transaction-type="RESOURCE_LOCAL">
<!-- provider>org.hibernate.ejb.HibernatePersistence</provider-->
<!-- jta-data-source>java:comp/env/jdbc/opendb</jta-data-source-->
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect" />
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.connection.driver_class" value="com.ibm.db2.jcc.DB2Driver" />
<property name="hibernate.connection.url" value="jdbc:db2://10.1.1.21:50001/matDev" />
<property name="hibernate.connection.username" value="db2inst1" />
<property name="hibernate.connection.password" value="admin" />
</properties>
</persistence-unit>
</persistence>
要使用jdbctemplate 必须要依赖datasource 如何配置可以解决使得jpaTemplate和jdbcTemplate 共享一个事务处理