springmvc+Hibernate+JPA(混合事务)解读
作者:正怒月神
在Spring项目中,Spring Data JPA作为一种持久层框架,因其简化数据库操作而受到青睐,但在将其引入使用Hibernate的旧项目时,可能会遇到事务处理问题,解决方案包括配置两种事务管理器:Hibernate事务管理器和JPA事务管理器
springmvc+Hibernate+JPA(混合事务)
最近发觉 spring-data-jpa 比较好用。
我在springcloud的项目中使用后,也尝试在springmvc中增加 jpa。
但是老项目用的是hibernate,在使用添加jpa后,事务出现了一些问题。
解决方案
配置文件
1 配置Hibernate事务(transactionManager)
<!-- 事物管理器配置 --> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> <!-- hibernate -->
2 配置Jpa事务(transactionManager_jpa)
<!-- Jpa 事务配置 --> <bean id="transactionManager_jpa" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <!-- Spring Data Jpa配置 --> <jpa:repositories base-package="com.kintech.dao.*" transaction-manager-ref="transactionManager_jpa" entity-manager-factory-ref="entityManagerFactory"/> <!-- 使用annotation定义事务 --> <tx:annotation-driven transaction-manager="transactionManager_jpa" proxy-target-class="true" />
配置全本:
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd" default-lazy-init="true"> <!--配置数据源--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driver}" /> <!--数据库连接驱动--> <property name="jdbcUrl" value="${jdbc.jdbcUrl}" /> <!--数据库地址--> <property name="user" value="${jdbc.user}" /> <!--用户名--> <property name="password" value="${jdbc.password}" /> <!--密码--> <property name="maxPoolSize" value="${jdbc.maxPoolSize}" /> <!--最大连接数--> <property name="minPoolSize" value="${jdbc.miniPoolSize}" /> <!--最小连接数--> <property name="initialPoolSize" value="${jdbc.initialPoolSize}" /> <!--初始化连接池内的数据库连接--> <property name="maxIdleTime" value="${jdbc.maxIdleTime}" /> <!--最大空闲时间--> </bean> <!-- hibernate --> <!--配置session工厂--> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan" value="com.kintech.model" /> <property name="hibernateProperties"> <props> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <!--hibernate根据实体自动生成数据库表--> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <!--指定数据库方言--> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <!--在控制台显示执行的数据库操作语句--> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <!--在控制台显示执行的数据哭操作语句(格式)--> <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop> <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop> <!-- 查询缓存 --> <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop> <prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop> </props> </property> </bean> <!-- 事物管理器配置 --> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> <!-- hibernate --> <!-- JPA --> <!-- JPA实体管理器工厂 --> <bean id="entityManagerFactory" name="jpaEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" /> <!-- 加入定制化包路径 --> <property name="packagesToScan" value="com.kintech.model.domain.*" /> <property name="jpaProperties"> <props> <prop key="hibernate.current_session_context_class">thread</prop> <prop key="hibernate.hbm2ddl.auto">none</prop><!-- validate/update/create --> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <!-- 建表的命名规则 --> <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop> </props> </property> </bean> <!-- 设置JPA实现厂商的特定属性 --> <bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="databasePlatform" value="${hibernate.dialect}"/> </bean> <!-- Jpa 事务配置 --> <bean id="transactionManager_jpa" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <!-- Spring Data Jpa配置 --> <jpa:repositories base-package="com.kintech.dao.*" transaction-manager-ref="transactionManager_jpa" entity-manager-factory-ref="entityManagerFactory"/> <!-- 使用annotation定义事务 --> <tx:annotation-driven transaction-manager="transactionManager_jpa" proxy-target-class="true" /> <!-- JPA --> </beans>
修改@Transactional
在使用JPA时,注明
@Transactional("transactionManager_jpa")
@Transactional("transactionManager_jpa") @Override public Model_Res add(Model_Req req) { //jpa dao Model entity = dao.save(reqData); return res; }
原来的Hibernate不用改变(因为)
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。