事务的初步了解

本文介绍了事务的基本概念,包括事务的四大特性——原子性、一致性、隔离性和持久性,并详细阐述了在Spring中如何配置事务管理器以及如何通过注解开启事务管理。通过实例展示了在Spring配置文件中配置DataSourceTransactionManager以及使用@Transaction注解的步骤。
摘要由CSDN通过智能技术生成


1、事务概念

(1)什么是事务

①事务是数据库操作的最基本单元,逻辑上一组操作要么都成功,如果有一个失败,那么都失败

②典型场景:银行转账

(2)事务的四个特性(ACID)

  • 原子性:要成功都成功,一个失败都失败
  • 一致性:操作前后总量不变
  • 隔离性:互不干扰
  • 持久性:提交之后表种数据发生变化

2、事务的配置

Ⅰ、在Spring中配置文件中配置事务管理器

<!--        创建事务管理器-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--                注入数据源-->
                <property name="dataSource" ref="dataSource"></property>
        </bean>

Ⅱ、在Spring中配置文件,开启事务的注解

(1)在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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

(2)开启事务的注解

<!--        开启事务注解-->
        <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值