springmvc+Hibernate+JPA(混合事務(wù))解讀
springmvc+Hibernate+JPA(混合事務(wù))
最近發(fā)覺 spring-data-jpa 比較好用。
我在springcloud的項目中使用后,也嘗試在springmvc中增加 jpa。
但是老項目用的是hibernate,在使用添加jpa后,事務(wù)出現(xiàn)了一些問題。
解決方案
配置文件
1 配置Hibernate事務(wù)(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事務(wù)(transactionManager_jpa)
<!-- Jpa 事務(wù)配置 --> <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定義事務(wù) --> <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"> <!--配置數(shù)據(jù)源--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driver}" /> <!--數(shù)據(jù)庫連接驅(qū)動--> <property name="jdbcUrl" value="${jdbc.jdbcUrl}" /> <!--數(shù)據(jù)庫地址--> <property name="user" value="${jdbc.user}" /> <!--用戶名--> <property name="password" value="${jdbc.password}" /> <!--密碼--> <property name="maxPoolSize" value="${jdbc.maxPoolSize}" /> <!--最大連接數(shù)--> <property name="minPoolSize" value="${jdbc.miniPoolSize}" /> <!--最小連接數(shù)--> <property name="initialPoolSize" value="${jdbc.initialPoolSize}" /> <!--初始化連接池內(nèi)的數(shù)據(jù)庫連接--> <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根據(jù)實體自動生成數(shù)據(jù)庫表--> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <!--指定數(shù)據(jù)庫方言--> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <!--在控制臺顯示執(zhí)行的數(shù)據(jù)庫操作語句--> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <!--在控制臺顯示執(zhí)行的數(shù)據(jù)哭操作語句(格式)--> <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> <!-- 建表的命名規(guī)則 --> <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop> </props> </property> </bean> <!-- 設(shè)置JPA實現(xiàn)廠商的特定屬性 --> <bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="databasePlatform" value="${hibernate.dialect}"/> </bean> <!-- Jpa 事務(wù)配置 --> <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定義事務(wù) --> <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不用改變(因為)
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring三級緩存思想解決循環(huán)依賴總結(jié)分析
這篇文章主要為大家介紹了Spring三級緩存思想解決循環(huán)依賴總結(jié)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08hadoop運行java程序(jar包)并運行時動態(tài)指定參數(shù)
這篇文章主要介紹了hadoop如何運行java程序(jar包)并運行時動態(tài)指定參數(shù),使用hadoop 運行 java jar包,Main函數(shù)一定要加上全限定類名,需要的朋友可以參考下2021-06-06Mybatis中如何設(shè)置sqlSession自動提交
在MyBatis中,默認(rèn)情況下,獲取的SqlSession對象不會自動提交事務(wù),這意味著在進行更新、刪除或插入等操作后,需要顯式調(diào)用commit方法來提交事務(wù),但是,可以在獲取SqlSession時通過將openSession方法的參數(shù)設(shè)置為true2024-09-09Java設(shè)計模式之策略模式的使用(Strategy?Pattern)
策略模式是一種行為型設(shè)計模式,用于定義一系列算法并將每個算法封裝起來,使它們可以互相替換,從而實現(xiàn)代碼的可維護性和靈活性,策略模式包含策略接口、具體策略類和上下文類,并通過將算法的選擇與使用分離,使得算法可以獨立變化2025-03-03Java數(shù)據(jù)結(jié)構(gòu)與算法之二分查找詳解
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)與算法之二分查找詳解,二分查找算法是一種在有序數(shù)組中查找某一特定元素的搜索算法,其思想就是不斷地將有序查找表“一分為二”,逐漸縮小搜索區(qū)域,進而找到目標(biāo)元素,需要的朋友可以參考下2023-12-12