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

x33g5p2x  于2022-08-17 转载在 其他  
字(8.3k)|赞(0)|评价(0)|浏览(837)

编程式事务控制相关对象

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. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
  10. ">
  11. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  12. <property name="driverClass" value="com.mysql.jdbc.Driver"/>
  13. <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
  14. <property name="user" value="root"/>
  15. <property name="password" value="123456"/>
  16. </bean>
  17. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  18. <property name="dataSource" ref="dataSource"/>
  19. </bean>
  20. <bean id="accountDao" class="com.dao.impl.AccountDaoImpl">
  21. <property name="jdbcTemplate" ref="jdbcTemplate"/>
  22. </bean>
  23. <!--目标对象 内部的方法就是切点-->
  24. <bean id="accountService" class="com.service.impl.AccountServiceImpl">
  25. <property name="accountDao" ref="accountDao"/>
  26. </bean>
  27. <!--配置平台事务管理器-->
  28. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  29. <property name="dataSource" ref="dataSource"/>
  30. </bean>
  31. <!--通知 事务的增强-->
  32. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  33. <!--设置事务的属性信息的-->
  34. <tx:attributes>
  35. <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
  36. <tx:method name="save" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
  37. <tx:method name="findAll" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
  38. <!-- update*表示只要update...都是这个,*表示通配符 -->
  39. <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
  40. <tx:method name="*"/>
  41. </tx:attributes>
  42. </tx:advice>
  43. <!--配置事务的aop织入-->
  44. <aop:config>
  45. <aop:pointcut id="txPointcut" expression="execution(* com.service.impl.*.*(..))"/>
  46. <!-- 事务专用advisor,控制事务-->
  47. <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
  48. </aop:config>
  49. </beans>

pom.xml下

  1. <dependencies>
  2. <dependency>
  3. <groupId>junit</groupId>
  4. <artifactId>junit</artifactId>
  5. <version>4.12</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework</groupId>
  9. <artifactId>spring-test</artifactId>
  10. <version>5.0.5.RELEASE</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.aspectj</groupId>
  14. <artifactId>aspectjweaver</artifactId>
  15. <version>1.8.4</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework</groupId>
  19. <artifactId>spring-context</artifactId>
  20. <version>5.0.5.RELEASE</version>
  21. <scope>compile</scope>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework</groupId>
  25. <artifactId>spring-jdbc</artifactId>
  26. <version>5.0.5.RELEASE</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework</groupId>
  30. <artifactId>spring-test</artifactId>
  31. <version>5.0.5.RELEASE</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework</groupId>
  35. <artifactId>spring-tx</artifactId>
  36. <version>5.0.5.RELEASE</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>c3p0</groupId>
  40. <artifactId>c3p0</artifactId>
  41. <version>0.9.1.1</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>mysql</groupId>
  45. <artifactId>mysql-connector-java</artifactId>
  46. <version>5.1.32</version>
  47. </dependency>
  48. </dependencies>

数据库中

运行结果

数据库中

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

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

  1. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  2. <!--设置事务的属性信息的-->
  3. <tx:attributes>
  4. <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED">
  5. </tx:attributes>
  6. </tx:advice>

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

  1. <tx:method name="save" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
  • name:切点方法名称
  • isolation:事务的隔离级别
  • propogation:事务的传播行为
  • timeout:超时时间
  • read-only:是否只读

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

  • 平台事务管理器配置
  • 事通知的配置
  • 事务aop织入的配置

相关文章