Spring使用注解方式处理事务

Spring有专门的类来处理事务,在这之前我们先要理解Spring处理事务中的几个概念:

1.接口:

事务管理器是PlatformTransactionManager接口,在接口中定义了事务的主要函数:commit(); 事务提交
rollback();事务回滚

2.事务管理器接口的实现类:

1)DataSourcTransactionManager:使用jdb或者mybatis访问数据库时使用的
<bean id=”myDataSource” class=“xx包.DataSourceTransactionManager”>
必须指定数据源
</bean>
2)HibernateTransactionManager:使用Hibernate框架时,使用的实现类
3)事务超时:TIMEOUT,秒为单位,默认是-1,使用数据库的默认超时时间
超时:指事务的最长执行时间,也就是一个函数最长的执行时间.当时间到了,函数没有
执行完毕,Spring会回滚该函数的执行(回滚事务)

3.事务的传播行为:事务在函数之间传递,函数怎么使用事务。通过传播行为指定函数怎么使用事务

有7个传播行为:
事务的传播行为常量都是以PROPAGATION_开头,形如:PROPAGATION_XXX
PROPAGATION_REQUIRED 指定的函数必须在事务内执行。若当前存在事务,就加入到当前事务中, 若当前没事务,就创建一个新事务。Spring默认的事务传播行为
PROPAGATION_REQUIES_NEW 总是新建一个新事务,若当前存在事务,就将当前事务挂起来,直 到新事务执行完毕
PROPAGATION_SUPPORTS 指定的函数支持当前事务,但若当前没事务,也可以使用非事务方式执 行
PROPAGATION_MANDATORY
PROPAGATION_NESTED
PROPAGATION_NEVER
PROPAGATION_NOT_SUPPORTED

我们了解了Spring处理事务的一些概念以及一些常用的类,那么现在在Spring中使用事务

项目目录:

要spring使用事务的注解就需要在application-config.xml(Spring核心配置文件)添加头部信息:

<?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: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-2.0.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

<!--声明事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="myDataSource"/>
</bean>
<!--声明事务的注解驱动
  transaction-manager:事务管理器对象的id
-->
<tx:annotation-driven transaction-manager="transactionManager"/>

BuyGoodsServiceImpl文件:

/**使用注解来设值事务的属性(传播行为,隔离等级,超时,当哪些异常发生的时候触发回滚事务)
 * 注意:该注解必须使用在公有函数上,而且抛出的异常必需继承RuntimeException
 * */
@Transactional(propagation = Propagation.REQUIRED,
isolation = Isolation.DEFAULT,timeout = 20,
rollbackFor = {NullPointerException.class,NotEnoughException.class})
public void buyGoods(int goodsId, int nums)
        throws NullPointerException, NotEnoughException{
    /**生成销售的订单
     * */
    Sale sale=new Sale();
    sale.setGid(goodsId);
    sale.setNum(nums);
    saleDao.insertSale(sale);

    /**修改库存
     * */
    Goods goods=goodsDao.selectGoodsById(goodsId);
    if(goods==null){
        throw new NullPointerException(goodsId+"没有该商品");
    }
    if(goods.getAmount()<nums){
        throw new NotEnoughException(goodsId+"库存不足");
    }

    /**操作库存
     * */
    goods.setAmount(nums);
    goodsDao.updateGoods(goods);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • spring 中事务注解@Transactional与trycatch的使用

    spring事务注解@Transactional与trycatch 在项目中 @service层中 我们会经常在做一些增删改操作的方法上看到 spring 的事务注解 @transaction 已知@transaction 是让spring 帮我们实现事务的控制. 但是在项目中会经常看到 有的方法中 会存在trycatch块包括的方法上注解着@transaction eg: @Override @Transactional public Json addOrder(TOrderAddReq tO

  • Spring 使用注解方式进行事务管理配置方式

    使用步骤: 步骤一.在spring配置文件中引入<tx:>命名空间 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocatio

  • Spring中的事务操作、注解及XML配置详解

    事务 事务全称叫数据库事务,是数据库并发控制时的基本单位,它是一个操作集合,这些操作要么不执行,要么都执行,不可分割.例如我们的转账这个业务,就需要进行数据库事务的处理. 转账中至少会涉及到两条 SQL 语句: update Acoount set balance = balance - money where id = 'A'; update Acoount set balance = balance + money where id = 'B' 上面这两条 SQL 就可以要看成是一个事务,必

  • Spring事务注解@Transactional失效的八种场景分析

    首先说一下最近自己遇到的一个坑: @Transactional service A(){ try{ insert(); serviceB.update(); }catch(){ throw new RunTimeException(); } } serviceB(){ @Transactional update(){ try{ mapperB.update(); }catch(){ throw new RunTimeException(); } } } mapperB (){ try{ //do

  • Spring实战之使用注解实现声明式事务操作示例

    本文实例讲述了Spring实战之使用注解实现声明式事务操作.分享给大家供大家参考,具体如下: 一 配置文件 <?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xm

  • spring boot基于注解的声明式事务配置详解

    事务配置 1.配置方式一 1)开启spring事务管理,在spring boot启动类添加注解@EnableTransactionManagement(proxyTargetClass = true):等同于xml配置方式的 <tx:annotation-driven />(注意:1项目中只需配置一次,2需要配置proxyTargetClass = true) 2)在项目中需要添加事务的类或方法上添加注解@Transactional(建议添加在方法上),一般使用默认属性即可,若要使用事务各属性

  • 使用SpringBoot注解方式处理事务回滚实现

    我们在SpringBoot和MyBatis整合的时候,需要在SpringBoot中通过注解方式配置事务回滚 1 Pojo类 package com.zxf.domain; import java.util.Date; public class User { private Integer id; private String name; private String pwd; private String head_img; private String phone; private Date

  • Spring @Transaction 注解执行事务的流程

    前言 相信小伙伴一定用过 @Transaction 注解,那 @Transaction 背后的秘密又知道多少呢? Spring 是如何开启事务的?又是如何进行提交事务和关闭事务的呢? 画图猜测 在开始 debug 阅读源码之前,小伙伴们应该已经知道 MySQL 是如何开启事务的. 因此可以得出猜测: 那下面跟着源码一起读一读,Spring 的 @Transaction 注解是如何执行事务逻辑的? Spring 事务执行流程 开启事务 这里使用的是 Spring Boot + MySQL + Dr

  • SpringBoot 注解事务声明式事务的方式

    springboot 对新人来说可能上手比springmvc要快,但是对于各位从springmvc转战到springboot的话,有些地方还需要适应下,尤其是xml配置.我个人是比较喜欢注解➕xml是因为看着方便,查找方便,清晰明了.但是xml完全可以使用注解代替,今天就扒一扒springboot中事务使用注解的玩法. springboot的事务也主要分为两大类,一是xml声明式事务,二是注解事务,注解事务也可以实现类似声明式事务的方法,关于注解声明式事务,目前网上搜索不到合适的资料,所以在这里

  • Spring声明式事务注解之@EnableTransactionManagement解析

    Spring声明式事务注解之@EnableTransactionManagement 1. 说明 @EnableTransactionManagement声明在主配置类上,表示开启声明式事务,其原理是通过@Import导入TransactionManagementConfigurationSelector组件,然后又通过TransactionManagementConfigurationSelector导入组件AutoProxyRegistrar和ProxyTransactionManageme

随机推荐