广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Spring使用注解方式处理事务
  • 884
分享到

Spring使用注解方式处理事务

Spring注解事务Spring注解处理事务Spring处理事务 2022-11-13 15:11:42 884人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

spring有专门的类来处理事务,在这之前我们先要理解Spring处理事务中的几个概念: 1.接口: 事务管理器是PlatfORMTransactionManager接口,在接口中定

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文件:


@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);
}

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

--结束END--

本文标题: Spring使用注解方式处理事务

本文链接: https://www.lsjlt.com/news/166112.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作