电子商务成功的网站,个人开店的电商平台,重庆建设工程人力资源官网,优秀室内设计案例目录
一、事务概述
二、引入事务场景
三、Spring对事务的支持
Spring实现事务的两种方式
声明式事务之注解实现方式 1.在Spring配置文件中配置事务管理器 2. 在Spring配置文件引入tx命名空间 3. 在Spring配置文件中配置“事务注解驱动器”#xff0c;通过注解的方式控…目录
一、事务概述
二、引入事务场景
三、Spring对事务的支持
Spring实现事务的两种方式
声明式事务之注解实现方式 1.在Spring配置文件中配置事务管理器 2. 在Spring配置文件引入tx命名空间 3. 在Spring配置文件中配置“事务注解驱动器”通过注解的方式控制事务 4. Service类或方法上添加Transaction注解 5.执行转账操作
声明式事务之全注解开发 1.配置类替代配置文件 2.测试程序 3.测试结果
声明式事务之XML实现方式 1.Spring配置文件 2.编写测试程序 3.测试结果 一、事务概述 在一个业务流程中通常需要多条DML语言共同联合完成这多条DML语句必须同时成功或失败这样才能保证数据的安全。事务Transaction一般是指要做的或所做的事情。在计算机术语中是指访问并可能更新数据库中各种数据项的一个程序执行单元(unit)。事务通常由高级数据库操纵语言或编程语言如SQLC或Java书写的用户程序的执行所引起并用形如begin transaction和end transaction语句或函数调用来界定。事务由事务开始(begin transaction)和事务结束(end transaction)之间执行的全体操作组成。例如在关系数据库中一个事务可以是一条SQL语句一组SQL语句或整个程序。事务的四个处理流程 开启事务(begin transaction)执行核心业务代码提交事务业务执行过程没有出现异常 (commit transaction)回滚事务业务执行过程中出现异常(rollback transaction) 事务的ACID特性 原子性Atomicity事务小的工作单元不可划分。一致性Consistency事务要么同时成功要么同时失败。隔离性Isolation隔离性实际上解决的问题是多个事务作用于同一个或者同一批数据时的并发问题防止由于交叉执行而导致数据的不一致。一个完美的事务隔离在每个事务看来整个系统只有自己在工作对于整个系统而言这些并发的事务一个接一个的执行也仿佛只有一个事务这样的隔离成为“可序列化Serializability”。当然这样的隔离级别会带来巨大的开销因此出现了各种各样的隔离级别进而满足不同场景的需要。持久性Durability只要事务完成不管发生任何问题都不应该发生数据丢失。即事务处理结束后对数据的修改就是永久的即便系统故障也不会丢失。 二、引入事务场景 以银行转账为例 有两个账户act1 和 act2act1向act2转账10000对于这个流程包括两个操作act1减10000act2加10000这两步必须同时成功或者同时失败。 转账成功act 减10000 act2 加 10000 转账失败act减10000后因系统故障act2没有加10000 数据库为了避免这种特殊情况的发生提出了事务概念。例如通过jdbc连接数据库后可以手动开启事务提交事务回滚事务。我们可以借助aop技术实现事务管理更幸运的是Spring提供了事务支持。 三、Spring对事务的支持 Spring实现事务的两种方式
编程式事务 通过编写代码的方式实现事务的管理声明式事务 基于注解方式、XML配置方式 声明式事务之注解实现方式 1.在Spring配置文件中配置事务管理器
!--配置事务管理器--bean idtxManager classorg.springframework.jdbc.datasource.DataSourceTransactionManagerproperty namedataSource refdateSource//bean 2. 在Spring配置文件引入tx命名空间
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:txhttp://www.springframework.org/schema/txxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 3. 在Spring配置文件中配置“事务注解驱动器”通过注解的方式控制事务
!--开启事务注解驱动器开始事务注解。--tx:annotation-driven transaction-managertxManager/ 4. Service类或方法上添加Transaction注解
package com.xxx.bank.service.impl;import com.xxx.bank.dao.AccountDao;
import com.xxx.bank.pojo.Account;
import com.xxx.bank.service.AccountService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;Service(accountService)
Transactional
public class AccountServiceImpl implements AccountService {Resource(name accountDao)private AccountDao accountDao;Overridepublic void transfer(String fromActno, String toActno, double money) {Account fromAct accountDao.selectByActno(fromActno);if (fromAct.getBalance() money) {throw new RuntimeException(余额不足无法转账);}Account toAct accountDao.selectByActno(toActno);fromAct.setBalance(fromAct.getBalance() - money);toAct.setBalance(toAct.getBalance() money);int count accountDao.update(fromAct);count accountDao.update(toAct);/*模拟异常String s null;s.toString();*/if (count ! 2) {throw new RuntimeException(转账失败请联系银行...);}}
} 5.执行转账操作 当前账户情况 执行一次转账 模拟异常情况检查是否转账成功 声明式事务之全注解开发 1.配置类替代配置文件
package com.zhj.bank;import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;import javax.sql.DataSource;/*** author zhj1259615665qq.com* create 2023-10-04 {16:45}*/
Configuration
// 开启组件扫描
ComponentScan(com.zhj.bank)
// 开启事务管理
EnableTransactionManagement
public class Spring6Config {Bean(name dataSource)public DruidDataSource getDataSource() {DruidDataSource dataSource new DruidDataSource();dataSource.setDriverClassName(com.mysql.jdbc.Driver);dataSource.setUrl(jdbc:mysql://localhost:3307/spring);dataSource.setUsername(root);dataSource.setPassword(123456);return dataSource;}Bean(name jdbcTemplate)public JdbcTemplate getJdbcTemplate(DataSource dataSource) {JdbcTemplate jdbcTemplate new JdbcTemplate();jdbcTemplate.setDataSource(dataSource);return jdbcTemplate;}Bean(name txManager)public DataSourceTransactionManager getDataSourceTransactionManager(DataSource dataSource) {DataSourceTransactionManager txManager new DataSourceTransactionManager();txManager.setDataSource(dataSource);return txManager;}
}2.测试程序
Testpublic void testNoXml() {ApplicationContext applicationContext new AnnotationConfigApplicationContext(Spring6Config.class);AccountService accountService applicationContext.getBean(accountService, AccountService.class);try {accountService.transfer(act-001, act-002, 10000);System.out.println(转账成功);} catch (Exception e) {e.printStackTrace();}}Testpublic void test() {ApplicationContext applicationContext new ClassPathXmlApplicationContext(spring.xml);AccountService accountService applicationContext.getBean(accountService, AccountService.class);try {accountService.transfer(act-001, act-002, 10000);System.out.println(转账成功);} catch (Exception e) {e.printStackTrace();}} 3.测试结果 声明式事务之XML实现方式 1.Spring配置文件
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:txhttp://www.springframework.org/schema/txxmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd!--开启组件扫描--context:component-scan base-packagecom.zhj.bank/!--配置数据源--bean iddateSource classcom.alibaba.druid.pool.DruidDataSourceproperty namedriverClassName valuecom.mysql.jdbc.Driver/property nameurl valuejdbc:mysql://localhost:3307/spring/property nameusername valueroot/property namepassword value123456//beanbean idjdbcTemplate classorg.springframework.jdbc.core.JdbcTemplateproperty namedataSource refdateSource//bean!--配置事务管理器--bean idtxManager classorg.springframework.jdbc.datasource.DataSourceTransactionManagerproperty namedataSource refdateSource//bean!--配置通知具体的增强代码--tx:advice idtxAdvice transaction-managertxManagertx:attributestx:method nametransfer propagationREQUIRED//tx:attributes/tx:advice!--配置切面--aop:configaop:pointcut idtxPointcut expressionexecution(* com.zhj.bank.service..*(..))/!--切面 切点 通知--aop:advisor advice-reftxAdvice pointcut-reftxPointcut//aop:config
/beans 2.编写测试程序
public class BankTxTest {Testpublic void test() {ApplicationContext applicationContext new ClassPathXmlApplicationContext(spring.xml);AccountService accountService applicationContext.getBean(accountService, AccountService.class);try {accountService.transfer(act-001, act-002, 10000);System.out.println(转账成功...);} catch (Exception e) {e.printStackTrace();}}
} 3.测试结果 转账一次 模拟异常情况 事务在数据库中是十分重要的概念Spring提供了两种事务的支持方式这篇博客记录了一点点的知识点通过银行转账案例希望可以帮助你更好地理解事务。上述内容如果有错误的地方希望大佬们可以指正。我一直在学习的路上您的帮助使我收获更大觉得对您有帮助的话还请点赞支持我也会不断更新文章