文章目录
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>