• 编程式事务之基于XML的声明式事务控制


    目录

    编程式事务控制相关对象

    PlatformTransactionManager平台事务管理

    TransactionDefinition事务定义

    事务的传播行为

    TransactionStatus事务状态

     基于XML的声明式事务控制

    切点方法的事务参数的配置


    编程式事务控制相关对象

    编程式:即使用java的api书写代码

    声明式:使用配置去配置

    PlatformTransactionManager平台事务管理

    PlatformTransactionManager接口时spring的事务管理器,它里面提供来我们常用的操作事务的方法

     PlatformTransactionManager是接口类型,不同的Dao层技术则有不同的实现类,例如:Dao层技术是jdbc或mybatis时:orqspringframeworkidbcdatasourceDataSourceTransactionManager
    Dao层技术是hibernate时:orq.springframework.orm.hibernate5.HibernateTransactionManager

    TransactionDefinition事务定义

    TransactionDefinition是事务的定义信息对象,里面有如下方法:

     设置隔离级别,可以解决事务并发产生的问题,如

    1. ISOLATION_DEFAULT//默认的
    2. ISOLATION_READ_UNCOMMITTED//读未提交,哪种都不能解决
    3. ISOLATION_READ_COMMITTED//读已提交,解决脏读
    4. ISOLATION_REPEATABLE READ//可重复读,解不可重复读
    5. ISOLATION_SERIALIZABLE//串行化,解决所有,性能最低

    事务的传播行为

    1. REQUIRED:如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。一般的选择(默认值)
    2. SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行(没有事务)
    3. MANDATORY:使用当前的事务,如果当前没有事务,就抛出异常
    4. RFOUFRS NEW:新增事务,如果当前在事务中,把当前事务挂起
    5. NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起
    6. NEVER:以非事务方式运行,如果当前存在事务,抛出异常
    7. NESTED:如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行REOUIRED类似的操作
    8. 超时时间:默认值是-1.没有超时限制,如果有,以秒为单位进行设置
    9. 是否只读:建议查询时设置为只读,

    TransactionStatus事务状态

    TransactionStatus接口时提供事务具体运行状态(是被动产生的,不需要自己去设置),方法介绍如下

     基于XML的声明式事务控制

    spring的声明式事务就是指在配置文件中声明,用在spring配置文件中的声明式的处理事务来代替diam式的处理事务

      转账业务演示事务

     controller包下AccountController类

    1. package com.controller;
    2. import com.service.AccountService;
    3. import org.springframework.context.ApplicationContext;
    4. import org.springframework.context.support.ClassPathXmlApplicationContext;
    5. public class AccountController {
    6. public static void main(String[] args) {
    7. ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext1.xml");
    8. AccountService accountService = app.getBean(AccountService.class);
    9. accountService.transfer("KC","ZH",500);
    10. }
    11. }

    service包下AccountService接口

    1. package com.service;
    2. public interface AccountService {
    3. public void transfer(String outMan, String inMan, double money);
    4. }

    接口实现类

    1. package com.service.impl;
    2. import com.dao.AccountDao;
    3. import com.service.AccountService;
    4. public class AccountServiceImpl implements AccountService {
    5. private AccountDao accountDao;
    6. public void setAccountDao(AccountDao accountDao) {
    7. this.accountDao = accountDao;
    8. }
    9. public void transfer(String outMan, String inMan, double money) {
    10. accountDao.out(outMan,money);
    11. accountDao.in(inMan,money);
    12. }
    13. }

    pojo包下Account类

    1. package com.pojo;
    2. public class Account {
    3. private String name;
    4. private double money;
    5. public String getName() {
    6. return name;
    7. }
    8. public void setName(String name) {
    9. this.name = name;
    10. }
    11. public double getMoney() {
    12. return money;
    13. }
    14. public void setMoney(double money) {
    15. this.money = money;
    16. }
    17. }

    dao包下AccountDao

    1. package com.dao;
    2. public interface AccountDao {
    3. public void out(String outMan, double money);
    4. public void in(String inMan, double money);
    5. }

    实现类下

    1. package com.dao.impl;
    2. import com.dao.AccountDao;
    3. import org.springframework.jdbc.core.JdbcTemplate;
    4. public class AccountDaoImpl implements AccountDao {
    5. private JdbcTemplate jdbcTemplate;
    6. public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    7. this.jdbcTemplate = jdbcTemplate;
    8. }
    9. public void out(String outMan, double money) {
    10. jdbcTemplate.update("update account set money=money-? where name=?",money,outMan);
    11. }
    12. public void in(String inMan, double money) {
    13. jdbcTemplate.update("update account set money=money+? where name=?",money,inMan);
    14. }
    15. }

    配置文件applicationCntext1.xml下

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:aop="http://www.springframework.org/schema/aop"
    4. xmlns:tx="http://www.springframework.org/schema/tx"
    5. xsi:schemaLocation="
    6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    9. ">
    10. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    11. <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    12. <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
    13. <property name="user" value="root"/>
    14. <property name="password" value="123456"/>
    15. bean>
    16. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    17. <property name="dataSource" ref="dataSource"/>
    18. bean>
    19. <bean id="accountDao" class="com.dao.impl.AccountDaoImpl">
    20. <property name="jdbcTemplate" ref="jdbcTemplate"/>
    21. bean>
    22. <bean id="accountService" class="com.service.impl.AccountServiceImpl">
    23. <property name="accountDao" ref="accountDao"/>
    24. bean>
    25. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    26. <property name="dataSource" ref="dataSource"/>
    27. bean>
    28. <tx:advice id="txAdvice" transaction-manager="transactionManager">
    29. <tx:attributes>
    30. <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
    31. <tx:method name="save" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
    32. <tx:method name="findAll" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
    33. <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
    34. <tx:method name="*"/>
    35. tx:attributes>
    36. tx:advice>
    37. <aop:config>
    38. <aop:pointcut id="txPointcut" expression="execution(* com.service.impl.*.*(..))"/>
    39. <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    40. aop:config>
    41. beans>

    pom.xml下

    1. <dependencies>
    2. <dependency>
    3. <groupId>junitgroupId>
    4. <artifactId>junitartifactId>
    5. <version>4.12version>
    6. dependency>
    7. <dependency>
    8. <groupId>org.springframeworkgroupId>
    9. <artifactId>spring-testartifactId>
    10. <version>5.0.5.RELEASEversion>
    11. dependency>
    12. <dependency>
    13. <groupId>org.aspectjgroupId>
    14. <artifactId>aspectjweaverartifactId>
    15. <version>1.8.4version>
    16. dependency>
    17. <dependency>
    18. <groupId>org.springframeworkgroupId>
    19. <artifactId>spring-contextartifactId>
    20. <version>5.0.5.RELEASEversion>
    21. <scope>compilescope>
    22. dependency>
    23. <dependency>
    24. <groupId>org.springframeworkgroupId>
    25. <artifactId>spring-jdbcartifactId>
    26. <version>5.0.5.RELEASEversion>
    27. dependency>
    28. <dependency>
    29. <groupId>org.springframeworkgroupId>
    30. <artifactId>spring-testartifactId>
    31. <version>5.0.5.RELEASEversion>
    32. dependency>
    33. <dependency>
    34. <groupId>org.springframeworkgroupId>
    35. <artifactId>spring-txartifactId>
    36. <version>5.0.5.RELEASEversion>
    37. dependency>
    38. <dependency>
    39. <groupId>c3p0groupId>
    40. <artifactId>c3p0artifactId>
    41. <version>0.9.1.1version>
    42. dependency>
    43. <dependency>
    44. <groupId>mysqlgroupId>
    45. <artifactId>mysql-connector-javaartifactId>
    46. <version>5.1.32version>
    47. dependency>
    48. dependencies>

    数据库中

     运行结果

     数据库中

     当发生错误时,数据库中的值都不变这就控制住了事务

    切点方法的事务参数的配置

    1. <tx:advice id="txAdvice" transaction-manager="transactionManager">
    2. <tx:attributes>
    3. <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED">
    4. tx:attributes>
    5. tx:advice>

    其中代表切点方法的事务参数的配置。例如:

    • name:切点方法名称
    • isolation:事务的隔离级别
    • propogation:事务的传播行为
    • timeout:超时时间
    • read-only:是否只读

    声明式事务控制的配置要点

    • 平台事务管理器配置
    • 事通知的配置
    • 事务aop织入的配置
  • 相关阅读:
    二、PostgreSQL初始化配置&启动
    (九)笔记.net学习之委托和事件、多播委托、观察者模式
    Centos 8 stream x64设置交换空间
    Cell:代谢组学肠道微生物群介导生酮饮食的抗癫痫作用
    Java程序员必备的工具和框架
    操作指南|JumpServer与Keycloak集成对接
    包含BN层的神经网络的学习率优化
    练气第六天
    指针权限,new与delete,类与对象,函数模板,类模板的用法
    民族民俗景区3d智慧旅游系统提升游客旅游体验和质量
  • 原文地址:https://blog.csdn.net/weixin_60719453/article/details/126329717