Spring学习之Spring的事务管理

        我们已经学习了如何使用Spring进行数据库开发,但是在实际开发中,操作数据库时都会涉及到事务管理问题,为此Spring提供了专门用于事务处理的API。Spring的事务管理简化了传统的事务管理流程,并且在一定程度上减少了开发者的工作量。
        1. 事务管理的核心接口
        在SpringJAR包中有一个名为spring-tx-4.3.6.RELEASE的JAR包,该包就是Spring提供的用于事务管理的依赖包。在该JAR包的org.springframework.transaction包中,有3个核心接口:PlatformTransactionManager、TransactionDefinition和TransactionStatus,
        1.1 Platform TransactionManager
        PlatformTransactionManager接口是Spring提供的平台事务管理器,主要用于管理事务。该接口中提供了三个事务操作的方法,具体如下:

方法说明
TransactionStatus getTransaction(TransactionDefinition definition)用于获取事务状态信息。
void commit(TransactionStatus status)用于提交事务。
void rollback(TransactionStatus status)用于回滚事务。

        PlatformTransactionManager接口只是代表事务管理的接口,并不知道底层是如何管理事务的,具体如何管理事务则由它的实现类来完成。该接口常见的几个实现类如下:(当底层采用不同的持久层技术时,系统只需使用不同的PlatformTransactionManager实现类即可)

常用接口实现类说明
org.springframework.jdbc.datasource.DataSourceTransactionManager 用于配置JDBC数据源的事务管理器。
org.springframework.orm.hibernate4.HibernateTransactionManager 用于配置Hibernate的事务管理器 。
org.springframework.transaction.jta.JtaTransactionManager 用于配置全局事务管理器 。

        1.2 TransactionDefinition
        TransactionDefinition接口是事务定义(描述)的对象,该对象中定义了事务规则,并提供了获取事务相关信息的方法,具体如下:

方法说明
String getName( )获取事务对象名称。
int getIsolationLevel( )获取事务的隔离级别。
int getPropagationBehavior( )获取事务的传播行为。
int getTimeout( )获取事务的超时时间。
boolean isReadOnly( )获取事务是否只读。

        上述方法中,事务的传播行为是指在同一个方法中,不同操作前后所使用的事务。传播行为有很多种,具体如下表所示:

属性名称说明
PROPAGATION_REQUIREDrequired支持当前事务。如果A方法已经在事务中,B将直接使用。如果没有将创建新事务。
PROPAGATION_SUPPORTSsupports支持当前事务。如果A方法已经在事务中,B将直接使用。如果没有将以非事务状态执行。
PROPAGATION_MANDATORYmandatory支持当前事务。如果A方法没有事务,将抛异常。
PROPAGATION_REQUIRES_NEWrequires_new将创建新的事务,如果A方法已经在事务中,将A事务挂起。
PROPAGATION_NOT_SUPPORTEDnot_supported不支持当前事务,总是以非事务状态执行。如果A方法已经在事务中,将挂起。
PROPAGATION_NEVERnever不支持当前事务,如果A方法,在事务中,将抛异常。
PROPAGATION_NESTEDnested嵌套事务,底层将使用Savepoint形成嵌套事务 。

        在事务管理过程中,传播行为可以控制是否需要创建事务以及如何创建事务,通常情况下,数据的查询不会影响原数据的改变,所以不需要进行事务管理,而对于数据的插入、更新和删除操作,必须进行事务管理。如果没有指定事务的传播行为,Spring默认传播行为是REQUIRED。
        1.3 TransactionStatus
        TransactionStatus接口是事务的状态,它描述了某一时间点上事务的状态信息。该接口中包含6个方法,具体如下:

方法说明
void flush()刷新事务。
boolean hasSavepoint()获取是否存在保存点。
boolean isCompleted()获取事务是否完成。
boolean isNewTransaction() 获取是否为新事务。
boolean isRollbackOnly()获取事务是否回滚。
void setRollbackOnly()设置事务回滚。

        1.4 事务管理的方式
        编程式事务管理:通过编写代码实现的事务管理,包括定义事务的开始、正常执行后的事务提交和异常时的事务回滚。
        声明式事务管理:通过AOP技术实现的事务管理,主要思想是将事务作为一个“切面”代码单独编写,然后通过AOP技术将事务管理的“切面”植入到业务目标类中。
        声明式事务管理最大的优点在于开发者无需通过编程的方式来管理事务,在配置文件中进行相关的事务规则声明,就可以将事务应用到业务逻辑中。提高了开发效率,实际开发中,通常都推荐使用声明式事务管理。

        2. 声明式事务管理
        Spring的声明式事务管理可以通过两种方式来实现,一种是基于XML的方式,另一种是基于Annotation的方式。
        2.1 基于XML方式的声明式事务

        基于XML方式的声明式事务是在配置文件中通过<tx:advice>元素配置事务规则来实现的。当配置了事务的增强处理后,就可以通过编写的AOP配置,让Spring自动对目标生成代理。<tx:advice>元素及其子元素如下图所示:
在这里插入图片描述
        配置<tx:advice>元素的重点是配置<tx:method>子元素,上图中使用红色标注的几个属性是<tx:method>元素中的常用属性。其属性描述具体如下:

方法说明
name该属性为必选属性,它指定了与事务属性相关的方法名。其属性值支持使用通配符,如' * '、' get* '. ' handle* '、 ' *Order '等。
propagation用于指定事务的传播行为,其属性值就是上面传播行为中的值,它的默认值为REQURED。
isolation该属性用于指定事务的隔离级别,其属性值可以为DEFAULT、READ_UNCOMMITTED、READ_COMMITTED、REPEATABLE_READ和SERIALIZABLE,其默认值为DEFAULT。
read-only 该属性用于指定事务是否只读,其默认值为false。
timeout该属性用于指定事务超时的时间,其默认值为-1, 即永不超时。
rollback-for该属性用于指定触发事务回滚的异常类,在指定多个异常类时,异常类之间以英文逗号分隔。
no-rollback-for属性用于指定不触发事务回滚的异常类,在指定多个异常类时,异常类之间以英文逗号分隔。

        让我们通过一个模拟银行转账的案例来演示基于XML方式的声明式事务管理的使用。

           (1).我们在Spring的数据库开发中的代码的基础上进行实现,在com.example.jdbc包中,在接口AccountDao原有基础上,添加transfer()方法,并在接口的实现类AccountDaoImpl中,实现transfer()方法。
Spring学习之Spring的数据库开发链接:https://blog.csdn.net/weixin_43468667/article/details/88877369

package com.example.jdbc;

import java.util.List;

public interface AccountDao {
 public int addAccount(Account account);
 public int updateAccount(Account account);
 public int deleteAccount(int id);
 public Account findAccountById(int id);
 public List<Account> findAllAccount();
 public void transfer(String outUser,String inUser,Double money);
}
package com.example.jdbc;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

public class AccountDaoImpl implements AccountDao{
 //注入JdbcTemplate,提供set方法,得到JdbcTemplate对象
 private JdbcTemplate jdbcTemplate;
 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
  this.jdbcTemplate = jdbcTemplate;
 }
 //添加账户
 @Override
 public int addAccount(Account account) {
  // TODO Auto-generated method stub
  String sql = "insert into account(username,balance) value(?,?)";
  Object[] obj = new Object[]{
    account.getUsername(),
    account.getBalance()
  };
  int num = this.jdbcTemplate.update(sql, obj);
  return num;
 }
 //更新账户
 @Override
 public int updateAccount(Account account) {
  // TODO Auto-generated method stub
  String sql = "update account set username=?,balance=? where id=?";
  //String sql = "update account set username=?,balance=? where id = ?";
  Object[] params = new Object[]{
    account.getUsername(),
    account.getBalance(),
    account.getId()
  };
  int num = this.jdbcTemplate.update(sql, params);
  return num;
 }
 //删除账户
 @Override
 public int deleteAccount(int id) {
  // TODO Auto-generated method stub
  String sql = "delete from account where id=?";
  int num = this.jdbcTemplate.update(sql, id);
  return num;
 }
 @Override
 public Account findAccountById(int id) {
  // TODO Auto-generated method stub
  //定义SQL语句
  String sql = "select * from account where id = ?";
  //创建新的BeanPropertyRowMapper对象
  RowMapper<Account> rowMapper =
    new BeanPropertyRowMapper<Account>(Account.class);
  return this.jdbcTemplate.queryForObject(sql, rowMapper,id);
 }
 @Override
 public List<Account> findAllAccount() {
  // TODO Auto-generated method stub
  //定义SQL语句
  String sql = "select * from account";
  //创建新的BeanPropertyRowMapper对象
  RowMapper<Account> rowMapper =
    new BeanPropertyRowMapper<Account>(Account.class);
  return this.jdbcTemplate.query(sql, rowMapper);
 }
 @Override
 /*
  * 转账
  * inUser:收款人
  * outUser:汇款人
  * money:收款金额
  */
 public void transfer(String outUser, String inUser, Double money) {
  // TODO Auto-generated method stub
  //收款时,收款用户的余额=现有余额+所汇金额
  this.jdbcTemplate.update("update account set balance = balance+?"
    +"where username = ?",money,inUser);
  
  //汇款时,汇款用户的余额=现有余额-所汇金额
    this.jdbcTemplate.update("update account set balance = balance-?"
      +"where username = ?",money,outUser);
 }
 
}

           (2).在com.example.jdbc包中,在配置文件beans.xml编写代码。

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
 <!-- 配置数据源 -->
 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <!-- 数据库驱动 -->
  <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  <!-- 连接数据库的url -->
  <property name="url" value="jdbc:mysql://localhost/account_Info"></property>
  <!-- 连接数据库的用户名 -->
  <property name="username" value="root"></property>
  <!-- 连接数据库的密码 -->
  <property name="password" value="root"></property>
 </bean>
 <!-- 配置JDBC模板 -->
 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  <!-- 默认必须使用数据源 -->
  <property name="dataSource" ref="dataSource"></property>
 </bean>
 <bean id="accountDao" class="com.example.jdbc.AccountDaoImpl">
  <!-- 将jdbcTemplate注入到accountDao实例中 -->
  <property name="jdbcTemplate" ref="jdbcTemplate"></property>
 </bean>
 <!-- 事务管理器,依赖于数据源 -->
 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource"></property>
 </bean>
 <!-- 编写通知:对事务进行增强,需要编写对切入点和具体执行事务细节 -->
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
   <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
  </tx:attributes>
 </tx:advice>
 <!-- 编写AOP,让spring自动对目标生成代理,使用AspectJ的表达式 -->
 <aop:config>
  <!-- 切入点 -->
  <aop:pointcut expression="execution(* com.example.jdbc.*.*(..))" id="txPointCut"/>
  <!-- 切面 -->
  <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
 </aop:config>
</beans>

           (3).在com.example.jdbc包中,创建测试类TransactionXmlTest。

package com.example.jdbc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TransactionXmlTest {
 public static void main(String[] args) {
  // 加载配置文件
  ApplicationContext ac = 
     new ClassPathXmlApplicationContext("com/example/jdbc/beans.xml");
  //获取AccountDao实例
  AccountDao accountDao = (AccountDao) ac.getBean("accountDao");
  accountDao.transfer("zhangsan", "lisi", 200.0);
  System.out.println("转账成功!");
 }
 
}

           运行结果:
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
        2.2 基于Annotation方式的声明式事务
        Spring的声明式事务管理还可以通过Annotation的方式来实现。只需要以下两步:
        1. 在Spring容器中注册事务注解驱动:
<tx:annotation-driven transaction-manager=“transactionManager”/>

        2. 在需要事务管理的类或方法上使用@Transactional注解:如果将注解添加在Bean类上,则表示事务的设置对整个Bean类的所有方法都起作用;如果将注解添加在Bean类中的某个方法上,则表示事务的设置只对该方法有效。

        使用@Transactional注解时,可以通过参数配置事务详情:

参数名称说明
value用于指定需要使用的事务管理器,默认为“",其别名为transactionManager。
transactionManager指定事务的限定符值,可用于确定目标事务管理器,匹配特定的限定值(或者Bean的name值),默认为"",其别名为value。
isolation用于指定事务的隔离级别,默认为Isolation.DEFAULT (即底层事务的隔离级别)。
noRollbackFor 用于指定遇到特定异常时强制不回滚事务。
noRollbackForClassName用于指定遇到特定的多个异常时强制不回滚事务。其属性值可以指定多个异常类名。
propagation用于指定事务的传播行为,默认为Propagation.REQUIRED 。
read-only用于指定事务是否只读,默认为false。
rollbackFor用于指定遇到特定异常时强制回滚事务。
rollbackForClassName用于指定遇到特定的多个异常时强制回滚事务。其属性值可以指定多个异常类名。
timeout用于指定事务的超时时长,默认为TransactionDefinition.TIMEOUT_ DEFAULT (即底层事务系统的默认时间)。

        使用@Transactional注解与<tx:method>元素中的事务属性基本是对应的,且含义也基本相似。
        我们就对前面模拟银行转账的案例进行改进,来演示基于Annotation方式的声明式事务管理的使用。

           (2).在com.example.jdbc包中,在AccountDaoImpl类中的transfer()方法上添加事务注解。

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
 <!-- 配置数据源 -->
 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <!-- 数据库驱动 -->
  <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  <!-- 连接数据库的url -->
  <property name="url" value="jdbc:mysql://localhost/account_Info"></property>
  <!-- 连接数据库的用户名 -->
  <property name="username" value="root"></property>
  <!-- 连接数据库的密码 -->
  <property name="password" value="root"></property>
 </bean>
 <!-- 配置JDBC模板 -->
 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  <!-- 默认必须使用数据源 -->
  <property name="dataSource" ref="dataSource"></property>
 </bean>
 <bean id="accountDao" class="com.example.jdbc.AccountDaoImpl">
  <!-- 将jdbcTemplate注入到accountDao实例中 -->
  <property name="jdbcTemplate" ref="jdbcTemplate"></property>
 </bean>
 <!-- 事务管理器,依赖于数据源 -->
 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource"></property>
 </bean>
 <!-- 注册事务管理器驱动 -->
 <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

           (1).在com.example.jdbc包中,新建配置文件beans-annotation.xml编写代码。\

package com.example.jdbc;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

public class AccountDaoImpl implements AccountDao{
 //注入JdbcTemplate,提供set方法,得到JdbcTemplate对象
 private JdbcTemplate jdbcTemplate;
 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
  this.jdbcTemplate = jdbcTemplate;
 }
 //添加账户
 @Override
 public int addAccount(Account account) {
  // TODO Auto-generated method stub
  String sql = "insert into account(username,balance) value(?,?)";
  Object[] obj = new Object[]{
    account.getUsername(),
    account.getBalance()
  };
  int num = this.jdbcTemplate.update(sql, obj);
  return num;
 }
 //更新账户
 @Override
 public int updateAccount(Account account) {
  // TODO Auto-generated method stub
  String sql = "update account set username=?,balance=? where id=?";
  //String sql = "update account set username=?,balance=? where id = ?";
  Object[] params = new Object[]{
    account.getUsername(),
    account.getBalance(),
    account.getId()
  };
  int num = this.jdbcTemplate.update(sql, params);
  return num;
 }
 //删除账户
 @Override
 public int deleteAccount(int id) {
  // TODO Auto-generated method stub
  String sql = "delete from account where id=?";
  int num = this.jdbcTemplate.update(sql, id);
  return num;
 }
 @Override
 public Account findAccountById(int id) {
  // TODO Auto-generated method stub
  //定义SQL语句
  String sql = "select * from account where id = ?";
  //创建新的BeanPropertyRowMapper对象
  RowMapper<Account> rowMapper =
    new BeanPropertyRowMapper<Account>(Account.class);
  return this.jdbcTemplate.queryForObject(sql, rowMapper,id);
 }
 @Override
 public List<Account> findAllAccount() {
  // TODO Auto-generated method stub
  //定义SQL语句
  String sql = "select * from account";
  //创建新的BeanPropertyRowMapper对象
  RowMapper<Account> rowMapper =
    new BeanPropertyRowMapper<Account>(Account.class);
  return this.jdbcTemplate.query(sql, rowMapper);
 }
 @Override
 /*
  * 转账
  * inUser:收款人
  * outUser:汇款人
  * money:收款金额
  */
 @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,readOnly = false)
 public void transfer(String outUser, String inUser, Double money) {
  // TODO Auto-generated method stub
  //收款时,收款用户的余额=现有余额+所汇金额
  this.jdbcTemplate.update("update account set balance = balance+?"
    +"where username = ?",money,inUser); 
    
  //汇款时,汇款用户的余额=现有余额-所汇金额
    this.jdbcTemplate.update("update account set balance = balance-?"
      +"where username = ?",money,outUser);
 }
 
}

           (3).在com.example.jdbc包中,创建测试类TransactionXmlTest。

package com.example.jdbc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TransactionAnnoationTest {
 public static void main(String[] args) {
  // 加载配置文件
  ApplicationContext ac = 
     new ClassPathXmlApplicationContext("com/example/jdbc/beans.xml");
  //获取AccountDao实例
  AccountDao accountDao = (AccountDao) ac.getBean("accountDao");
  accountDao.transfer("wangwu", "zhaoliu", 400.0);
  System.out.println("转账成功!");
 }
 
}

           运行结果:
在这里插入图片描述

在这里插入图片描述

这就是Spring的事务管理,若有错漏,欢迎指正,希望大家一起学习进步!!!!
如果转载以及CV操作,请务必注明出处,谢谢!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值