SoFunction
Updated on 2025-03-02

SpringMvc multi-transaction commit and rollback issues

springmvc multi-transaction commit and rollback

# mysql
=
=jdbc:mysql://192.168.:3306/kintech_bo
=root
=root
 
=3
=3
=200
=20
=60
 
=1
=1
=200
=20
=60
 
# sqlserver
= 
= jdbc:sqlserver://192.168.:1433;DatabaseName=logisoft;useSSL=false
= sa
= sasa
 
 
#hibernate config
=
hibernate.show_sql=false
hibernate.format_sql=true
#hibernate. =update
hibernate.=none
.use_second_level_cache=false
.use_query_cache=false
.provider_class=
.factory_class=

Configuration

Key point: <!-- Configuring ChainedTransactionManager -->

Starting from Spring Framework version 5.3, Spring provides a new transaction manager implementation class CompositeTransactionManager

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;beans xmlns="/schema/beans"
       xmlns:xsi="http:///2001/XMLSchema-instance"
       xmlns:context="/schema/context"
       xmlns:mvc="/schema/mvc"
       xmlns:aop="/schema/aop"
       xmlns:tx="/schema/tx"
       xmlns:p="/schema/p" xmlns:jpa="/schema/data/jpa"
       xsi:schemaLocation="/schema/beans
                           /schema/beans/spring-beans-4.
                           /schema/context
                           /schema/context/spring-context-4.
                           /schema/mvc
                           /schema/mvc/spring-mvc-4.
                           /schema/aop
						   /schema/aop/spring-aop-4.
						   /schema/tx
                           /schema/tx/spring-tx-4. /schema/data/jpa /schema/data/jpa/"
       default-lazy-init="true"&gt;
    &lt;!--mysql Configure data source--&gt;
    &lt;bean  class=".v2." destroy-method="close"&gt;
        &lt;property name="driverClass" value="${}"/&gt;  &lt;!--Database connection driver--&gt;
        &lt;property name="jdbcUrl" value="${}"/&gt;     &lt;!--Database address--&gt;
        &lt;property name="user" value="${}"/&gt;   &lt;!--username--&gt;
        &lt;property name="password" value="${}"/&gt;   &lt;!--password--&gt;
        &lt;property name="maxPoolSize" value="${}"/&gt;      &lt;!--Maximum number of connections--&gt;
        &lt;property name="minPoolSize" value="${}"/&gt;       &lt;!--Minimum number of connections--&gt;
        &lt;property name="initialPoolSize" value="${}"/&gt;      &lt;!--Initialize the database connection in the connection pool--&gt;
        &lt;property name="maxIdleTime" value="${}"/&gt;  &lt;!--Maximum free time--&gt;
        &lt;property name="idleConnectionTestPeriod" value="${}"/&gt;  &lt;!--Check the number of spare connections--&gt;
    &lt;/bean&gt;
 
    &lt;!--  hibernate  --&gt;
    &lt;!--Configurationsessionfactory--&gt;
    &lt;bean  class="."&gt;
        &lt;property name="dataSource" ref="dataSource"/&gt;
        &lt;property name="packagesToScan" value=""/&gt;
        &lt;property name="hibernateProperties"&gt;
            &lt;props&gt;
                &lt;prop key="hibernate."&gt;${hibernate.}&lt;/prop&gt; &lt;!--hibernateAutomatically generate database tables based on entities--&gt;
                &lt;prop key=""&gt;${}&lt;/prop&gt;   &lt;!--Specify database dialect--&gt;
                &lt;prop key="hibernate.show_sql"&gt;${hibernate.show_sql}&lt;/prop&gt;     &lt;!--Display the executed database operation statement on the console--&gt;
                &lt;prop key="hibernate.format_sql"&gt;${hibernate.format_sql}&lt;/prop&gt;     &lt;!--Display the executed data cry operation statement on the console(Format)--&gt;
                &lt;prop key=".use_second_level_cache"&gt;${.use_second_level_cache}&lt;/prop&gt;
                &lt;prop key=".use_query_cache"&gt;${.use_query_cache}&lt;/prop&gt;  &lt;!-- Query cache --&gt;
                &lt;prop key=".provider_class"&gt;${.provider_class}&lt;/prop&gt;
                &lt;prop key=".factory_class"&gt;${.factory_class}&lt;/prop&gt;
                &lt;prop key="hibernate.allow_update_outside_transaction"&gt;true&lt;/prop&gt;
            &lt;/props&gt;
        &lt;/property&gt;
    &lt;/bean&gt;
    
    &lt;!-- JPA实体管理器factory --&gt;
    &lt;bean  name="jpaEntityManagerFactory"
          class=""&gt;
        &lt;property name="dataSource" ref="dataSource"/&gt;
        &lt;property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/&gt;
        &lt;!-- Add custom package path --&gt;
        &lt;property name="packagesToScan" value=".**"&gt;
        &lt;/property&gt;
 
        &lt;property name="jpaProperties"&gt;
            &lt;props&gt;
                &lt;prop key="hibernate.current_session_context_class"&gt;thread&lt;/prop&gt;
                &lt;prop key="hibernate."&gt;none&lt;/prop&gt;&lt;!-- validate/update/create --&gt;
                &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
                &lt;prop key="hibernate.format_sql"&gt;true&lt;/prop&gt;
                &lt;!-- Naming rules for building tables --&gt;
                &lt;prop key=".naming_strategy"&gt;&lt;/prop&gt;
                &lt;!-- Active release mode --&gt;
                &lt;prop key=".release_mode"&gt;after_statement&lt;/prop&gt;
            &lt;/props&gt;
        &lt;/property&gt;
    &lt;/bean&gt;
 
    &lt;!-- set upJPAImplement the specific properties of the manufacturer --&gt;
    &lt;bean 
          class=""&gt;
        &lt;property name="databasePlatform" value="${}"/&gt;
    &lt;/bean&gt;
    
    &lt;!-- JPA sqlserver --&gt;
    &lt;bean  class=".v2." destroy-method="close"&gt;
        &lt;property name="driverClass" value="${}"/&gt;  &lt;!--Database connection driver--&gt;
        &lt;property name="jdbcUrl" value="${}"/&gt;     &lt;!--Database address--&gt;
        &lt;property name="user" value="${}"/&gt;   &lt;!--username--&gt;
        &lt;property name="password" value="${}"/&gt;   &lt;!--password--&gt;
        &lt;property name="maxPoolSize" value="${}"/&gt;      &lt;!--Maximum number of connections--&gt;
        &lt;property name="minPoolSize" value="${}"/&gt;       &lt;!--Minimum number of connections--&gt;
        &lt;property name="initialPoolSize" value="${}"/&gt;      &lt;!--Initialize the database connection in the connection pool--&gt;
        &lt;property name="maxIdleTime" value="${}"/&gt;  &lt;!--Maximum free time--&gt;
        &lt;property name="idleConnectionTestPeriod" value="${}"/&gt;  &lt;!--Check the number of spare connections--&gt;
    &lt;/bean&gt;
 
    &lt;!-- Integrationsqlserverjpa --&gt;
    &lt;bean  name="sqlserverEntityManagerFactory"
          class=""&gt;
        &lt;property name="dataSource" ref="sqlserverDataSource"&gt;&lt;/property&gt;
        &lt;property name="packagesToScan" value=".**"&gt;
        &lt;/property&gt;
        &lt;property name="persistenceUnitName" value="sqlserverdb"&gt;&lt;/property&gt;
        &lt;property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter2"/&gt;
        &lt;property name="jpaProperties"&gt;
            &lt;props&gt;
                &lt;!--set up外连接抓取树的最大深度 --&gt;
                &lt;prop key="hibernate.max_fetch_depth"&gt;3&lt;/prop&gt;
                &lt;prop key=".fetch_size"&gt;18&lt;/prop&gt;
                &lt;prop key=".batch_size"&gt;10&lt;/prop&gt;
                &lt;!-- Whether to displaySQL --&gt;
                &lt;prop key="hibernate.show_sql"&gt;false&lt;/prop&gt;
                &lt;!-- showSQL是否Format化 --&gt;
                &lt;prop key="hibernate.format_sql"&gt;false&lt;/prop&gt;
                &lt;!-- Turn off Level 2 cache --&gt;
                &lt;prop key=".provider_class"&gt;&lt;/prop&gt;
                &lt;!-- Turn off Entity Field Mapping Verification --&gt;
                &lt;prop key=""&gt;none&lt;/prop&gt;
                &lt;!-- Active release mode --&gt;
                &lt;prop key=".release_mode"&gt;after_statement&lt;/prop&gt;
            &lt;/props&gt;
        &lt;/property&gt;
    &lt;/bean&gt;
    &lt;!-- set upJPAImplement the specific properties of the manufacturer --&gt;
    &lt;bean 
          class=""&gt;
        &lt;property name="databasePlatform" value=""/&gt;
    &lt;/bean&gt;
 
    &lt;!-- ConfigurationChainedTransactionManager --&gt;
    &lt;bean  class=""&gt;
        &lt;constructor-arg&gt;
            &lt;list&gt;
                &lt;bean class=""&gt;
                    &lt;property name="entityManagerFactory" ref="entityManagerFactory"/&gt;
                &lt;/bean&gt;
                &lt;bean class=""&gt;
                    &lt;property name="entityManagerFactory" ref="sqlserverEntityManagerFactory"/&gt;
                &lt;/bean&gt;
            &lt;/list&gt;
        &lt;/constructor-arg&gt;
    &lt;/bean&gt;
    &lt;!--  mysql jpa   --&gt;
    &lt;jpa:repositories base-package=".*"  transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"/&gt;
    &lt;!--  sqlserver jpa   --&gt;
    &lt;jpa:repositories base-package=""  transaction-manager-ref="transactionManager" entity-manager-factory-ref="sqlserverEntityManagerFactory"/&gt;
    &lt;tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/&gt;
    &lt;!-- JPA sqlserver --&gt;
 
&lt;/beans&gt;

3. Add @Transactional

Remember to add @Transactional to all related methods

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.