springmvc多事務(wù)提交和回滾問題
更新時間:2024年09月27日 10:10:28 作者:正怒月神
本文介紹了Spring MVC中如何配置和使用多事務(wù)管理及回滾,重點內(nèi)容包括配置jdbc.properties文件,使用Spring Framework 5.3版本新增的CompositeTransactionManager,以及如何在相關(guān)方法上添加@Transactional注解
springmvc多事務(wù)提交和回滾
1.jdbc.properties
# mysql jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql://192.168.1.xxx:3306/kintech_bo jdbc.user=root jdbc.password=root jdbc.initialPoolSize=3 jdbc.miniPoolSize=3 jdbc.maxPoolSize=200 jdbc.maxIdleTime=20 jdbc.idleConnectionTestPeriod=60 jdbc.sqlserver.initialPoolSize=1 jdbc.sqlserver.miniPoolSize=1 jdbc.sqlserver.maxPoolSize=200 jdbc.sqlserver.maxIdleTime=20 jdbc.sqlserver.idleConnectionTestPeriod=60 # sqlserver sqlserver.gica.driver= com.microsoft.sqlserver.jdbc.SQLServerDriver sqlserver.gica.jdbcUrl= jdbc:sqlserver://192.168.1.xxx:1433;DatabaseName=logisoft;useSSL=false sqlserver.gica.user= sa sqlserver.gica.password= sasa #hibernate config hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.show_sql=false hibernate.format_sql=true #hibernate.hbm2ddl.auto =update hibernate.hbm2ddl.auto=none hibernate.cache.use_second_level_cache=false hibernate.cache.use_query_cache=false hibernate.cache.provider_class=net.sf.ehcache.hibernate.EhCacheProvider hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
2.spring配置
重點:<!-- 配置ChainedTransactionManager -->
從Spring Framework 5.3版本開始,Spring提供了一個新的事務(wù)管理器實現(xiàn)類CompositeTransactionManager
<?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">
<!--mysql 配置數(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}"/> <!--最大空閑時間-->
<property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}"/> <!--檢查空余連接數(shù)-->
</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>
<prop key="hibernate.allow_update_outside_transaction">true</prop>
</props>
</property>
</bean>
<!-- 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>
<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>
<!-- 積極釋放模式 -->
<prop key="hibernate.connection.release_mode">after_statement</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 sqlserver -->
<bean id="sqlserverDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${sqlserver.gica.driver}"/> <!--數(shù)據(jù)庫連接驅(qū)動-->
<property name="jdbcUrl" value="${sqlserver.gica.jdbcUrl}"/> <!--數(shù)據(jù)庫地址-->
<property name="user" value="${sqlserver.gica.user}"/> <!--用戶名-->
<property name="password" value="${sqlserver.gica.password}"/> <!--密碼-->
<property name="maxPoolSize" value="${jdbc.sqlserver.maxPoolSize}"/> <!--最大連接數(shù)-->
<property name="minPoolSize" value="${jdbc.sqlserver.miniPoolSize}"/> <!--最小連接數(shù)-->
<property name="initialPoolSize" value="${jdbc.sqlserver.initialPoolSize}"/> <!--初始化連接池內(nèi)的數(shù)據(jù)庫連接-->
<property name="maxIdleTime" value="${jdbc.sqlserver.maxIdleTime}"/> <!--最大空閑時間-->
<property name="idleConnectionTestPeriod" value="${jdbc.sqlserver.idleConnectionTestPeriod}"/> <!--檢查空余連接數(shù)-->
</bean>
<!-- 整合sqlserverjpa -->
<bean id="sqlserverEntityManagerFactory" name="sqlserverEntityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="sqlserverDataSource"></property>
<property name="packagesToScan" value="com.kintech.model.gica.**">
</property>
<property name="persistenceUnitName" value="sqlserverdb"></property>
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter2"/>
<property name="jpaProperties">
<props>
<!--設(shè)置外連接抓取樹的最大深度 -->
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">18</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<!-- 是否顯示SQL -->
<prop key="hibernate.show_sql">false</prop>
<!-- 顯示SQL是否格式化 -->
<prop key="hibernate.format_sql">false</prop>
<!-- 關(guān)閉二級緩存 -->
<prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
<!-- 關(guān)閉實體字段映射校驗 -->
<prop key="javax.persistence.validation.mode">none</prop>
<!-- 積極釋放模式 -->
<prop key="hibernate.connection.release_mode">after_statement</prop>
</props>
</property>
</bean>
<!-- 設(shè)置JPA實現(xiàn)廠商的特定屬性 -->
<bean id="hibernateJpaVendorAdapter2"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.SQLServerDialect"/>
</bean>
<!-- 配置ChainedTransactionManager -->
<bean id="transactionManager" class="org.springframework.data.transaction.ChainedTransactionManager">
<constructor-arg>
<list>
<bean class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="sqlserverEntityManagerFactory"/>
</bean>
</list>
</constructor-arg>
</bean>
<!-- mysql jpa -->
<jpa:repositories base-package="com.kintech.dao.*" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"/>
<!-- sqlserver jpa -->
<jpa:repositories base-package="com.kintech.dao.gica" transaction-manager-ref="transactionManager" entity-manager-factory-ref="sqlserverEntityManagerFactory"/>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<!-- JPA sqlserver -->
</beans>3.添加@Transactional
相關(guān)方法都記得加上@Transactional
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring 定時任務(wù)@Scheduled 注解中的 Cron 表達式詳解
Cron 表達式是一種用于定義定時任務(wù)觸發(fā)時間的字符串表示形式,它由七個字段組成,分別表示秒、分鐘、小時、日期、月份、星期和年份,這篇文章主要介紹了Spring 定時任務(wù)@Scheduled 注解中的 Cron 表達式,需要的朋友可以參考下2023-07-07
SpringBoot嵌入式Servlet容器與定制化組件超詳細講解
這篇文章主要介紹了SpringBoot嵌入式Servlet容器與定制化組件的使用介紹,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-10-10
通過prometheus監(jiān)控springboot程序運行狀態(tài)的操作流程
jmx_exporter用于從Java應(yīng)用程序中提取JMX指標(biāo),適用于SpringBoot應(yīng)用,通過下載jar包和配置文件,可以抓取JVM基礎(chǔ)指標(biāo),要獲取應(yīng)用級別指標(biāo),需要集成Prometheus客戶端庫并自定義指標(biāo),本文給大家介紹了如何通過prometheus監(jiān)控springboot程序運行狀態(tài)2025-02-02
springboot版本升級以及解決springsecurity漏洞的問題
這篇文章主要介紹了springboot版本升級以及解決springsecurity漏洞的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08

