威海 网站建设,wordpress景点展示插件,上海专业优化排名工具,wordpress自助友链文章目录前言一、事务的特性#xff08;ACID#xff09;二、事务的隔离级别三、spring中的事务平台事务管理器.事务定义ISOLation_XXX#xff1a;**事务隔离级别.**PROPAGATION_XXX#xff1a;**事务的传播行为**.事务状态关系#xff1a;四、使用XML文件配置事务1、 搭建…
文章目录前言一、事务的特性ACID二、事务的隔离级别三、spring中的事务平台事务管理器.事务定义ISOLation_XXX**事务隔离级别.**PROPAGATION_XXX**事务的传播行为**.事务状态关系四、使用XML文件配置事务1、 搭建环境2、 配置及业务编写3、 测试五、使用注解形式进行事务开发总结前言
事务是逻辑上的一组操作的集合要么全部成功要么全部失败。在实际的业务场景中事务的应用场景范围很广泛例如转账订单支付等 一、事务的特性ACID 事务有四大特性 原子性:事务不可分割一致性:事务执行的前后数据完整性保持一致.隔离性:一个事务执行的时候不应该受到其他事务的打扰持久性:一旦结束数据就永久的保存到数据库. 二、事务的隔离级别
在实际的开发中可能会出现以下几种由数据操作引起的异常情况 如果不考虑隔离性: 脏读:一个事务读到另一个事务未提交数据不可重复读:一个事务读到另一个事务已经提交数据update导致一个事务多次查询结果不一致虚读:一个事务读到另一个事务已经提交数据insert导致一个事务多次查询结果不一致 事务存在几种隔离级别 未提交读:以上情况都有可能发生。已提交读:避免脏读但不可重复读虚读是有可能发生。可重复读:避免脏读不可重复读但是虚读有可能发生。串行化:避免以上所有情况. 三、spring中的事务
后端分层开发中事务应用于Service层中。spring提供了事务管理API
平台事务管理器.
PlatformTransactionManager
getTransaction(TransactionDefinition definition) 开启事务rollback(TransactionStatus status) 回滚事务commit(TransactionStatus status) 提交事务
Spring为不同的持久化框架提供了不同PlatformTransactionManager接口实现 使用Spring JDBC或iBatis 进行持久化数据时使用重点 org.springframework.jdbc.datasource.DataSourceTransactionManager 使用Hibernate进行持久化数据时使用 org.springframework.orm.hibernate.HibernateTransactionManager 使用JPA进行持久化时使用 org.springframework.orm.jpa.JpaTransactionManager 当持久化机制是Jdo时使用 org.springframework.jdo.JdoTransactionManager 使用一个JTA实现来管理事务在一个事务跨越多个资源时必须使用 org.springframework.transaction.jta.JtaTransactionManager 事务定义
TransactionDefinition
ISOLation_XXX事务隔离级别. ISOLATION_DEFAULT:默认级别. Mysql -- repeatable_read | Oracle -- read_commited 级别如下 ISOLATION_READ_UNCOMMITTED 读未提交ISOLATION_READ_COMMITTED 读已提交ISOLATION_REPEATABLE_READ 可重复读ISOLATION_SERIALIZABLE 串行化 PROPAGATION_XXX事务的传播行为. 事务的传播行为是用来解决实际开发中的问题 传播行为是指解决业务层调用事务的关系。 PROPAGATION_REQUIRED: 支持当前事务如果不存在 就新建一个 A,B 如果A有事务B使用A的事务如果A没有事务B就开启一个新的事务.(A,B是在一个事务中。) PROPAGATION_SUPPORTS: 支持当前事务如果不存在就不使用事务 A,B 如果A有事务B使用A的事务如果A没有事务B就不使用事务. PROPAGATION_MANDATORY: 支持当前事务如果不存在抛出异常 A,B 如果A有事务B使用A的事务如果A没有事务抛出异常. PROPAGATION_REQUIRES_NEW: 如果有事务存在挂起当前事务创建一个新的事务 A,B 如果A有事务B将A的事务挂起重新创建一个新的事务.(A,B不在一个事务中.事务互不影响.) PROPAGATION_NOT_SUPPORTED: 以非事务方式运行如果有事务存在挂起当前事务 A,B 非事务的方式运行A有事务就会挂起当前的事务. PROPAGATION_NEVER: 以非事务方式运行如果有事务存在抛出异常 PROPAGATION_NESTED: 如果当前事务存在则嵌套事务执行 基于SavePoint技术. A,B A有事务A执行之后将A事务执行之后的内容保存到SavePoint.B事务有异常的话用户需要自己设置事务提交还是回滚. 常用:(重点) PROPAGATION_REQUIRED PROPAGATION_REQUIRES_NEW PROPAGATION_NESTED
事务状态
TransactionStatus 是否有保存点 是否是一个新的事务 事务是否已经提交
关系 关系:PlatformTransactionManager通过TransactionDefinition设置事务相关信息管理事务管理事务过程中产生一些事务状态状态由TransactionStatus记录。 四、使用XML文件配置事务
spring支持声明式事务管理只需要配置文件不需要额外的编写代码 演示如下
1、 搭建环境 创建account表并插入两条数据 CREATE TABLE account (id int(11) NOT NULL AUTO_INCREMENT,money double DEFAULT NULL,PRIMARY KEY (id)
) ENGINEInnoDB AUTO_INCREMENT3 DEFAULT CHARSETutf8;INSERT INTO account VALUES (1, 1000);
INSERT INTO account VALUES (2, 1000);新建项目略 导入pom.xml依赖 ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.wmj/groupIdartifactIdSpringTX/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/propertiesdependencies!-- spring相关依赖版本必须保持一致 --dependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.3.24/version/dependency!--spring数据连接支持 --dependencygroupIdorg.springframework/groupIdartifactIdspring-jdbc/artifactIdversion5.3.24/version/dependency!--aop支持 --dependencygroupIdorg.aspectj/groupIdartifactIdaspectjweaver/artifactIdversion1.9.9.1/version/dependency!-- 事务 --dependencygroupIdorg.springframework/groupIdartifactIdspring-tx/artifactIdversion5.3.24/version/dependency!-- mybatis相关依赖 --dependencygroupIdorg.mybatis/groupIdartifactIdmybatis/artifactIdversion3.5.11/version/dependency!-- myabtis整合spring依赖必须导入 --dependencygroupIdorg.mybatis/groupIdartifactIdmybatis-spring/artifactIdversion2.0.7/version/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.31/version/dependency!--德鲁伊数据连接池支持 --dependencygroupIdcom.alibaba/groupIdartifactIddruid/artifactIdversion1.2.9/version/dependency!--lombok注解支持 --dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.24/version/dependency!--log4j日志 --dependencygroupIdlog4j/groupIdartifactIdlog4j/artifactIdversion1.2.17/version/dependency!--单元测试 --dependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.13.2/versionscopetest/scope/dependency/dependencies
/project2、 配置及业务编写 配置mybatis-config.xml ?xml version1.0 encodingUTF-8 ?
!DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttps://mybatis.org/dtd/mybatis-3-config.dtd
configuration!-- 配置日志 --settingssetting namelogImpl valueLOG4J//settings/configuration配置日志 log4j.rootLoggerDEBUG, stdout
log4j.appender.stdoutorg.apache.log4j.ConsoleAppender
log4j.appender.stdout.layoutorg.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern%5p [%t] - %m%n配置数据源 db.username 帐号
db.password 密码
db.url jdbc:mysql:///库名?serverTimezoneAsia/ShanghaicharacterEncodingUTF8
db.driverClassName com.mysql.cj.jdbc.Driver搭建后端业务代码 控制层
package com.wmj.controller;import com.wmj.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;/*** --效率是成功的核心关键--** Author lzl* Date 2023/2/13 16:04*/
Controller
public class TestController {Autowiredprivate TestService service;public void testTx(Integer id1,Integer id2,Double money){service.transferMoney(id1,id2,money);}
}
业务层接口及其实现类
package com.wmj.service;/*** --效率是成功的核心关键--** Author lzl* Date 2023/2/13 16:10*/public interface TestService {void transferMoney(Integer id1, Integer id2, Double money);
}
package com.wmj.service.impl;import com.wmj.mapper.TestMapper;
import com.wmj.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** --效率是成功的核心关键--** Author lzl* Date 2023/2/13 16:11*/
Service
public class TestServiceImpl implements TestService {Autowiredprivate TestMapper mapper;Overridepublic void transferMoney(Integer id1, Integer id2, Double money) {mapper.dmoney(id1,money);int a 1/0;mapper.amoney(id2,money);}
}
持久层接口及其xml数据访问
package com.wmj.mapper;import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;/*** --效率是成功的核心关键--** Author lzl* Date 2023/2/13 16:12*/
Repository
public interface TestMapper {Integer dmoney(Param(id1) Integer id1, Param(money) Double money);Integer amoney(Param(id2) Integer id2, Param(money) Double money);
}
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtdmapper namespacecom.wmj.mapper.TestMapperupdate iddmoneyupdate account set money money - #{money} where id #{id1}/updateupdate idamoneyupdate account set money money #{money} where id #{id2}/update
/mapper配置spring核心配置文件application.xml ?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:aophttp://www.springframework.org/schema/aopxmlns:txhttp://www.springframework.org/schema/txxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!-- bean definitions here --!-- 扫描包下的注解 --context:component-scan base-packagecom.wmj/context:component-scan!-- 引入db.properties配置文件 --context:property-placeholder locationclasspath:db.properties/context:property-placeholder!-- 配置数据库链接 --bean iddataSource classcom.alibaba.druid.pool.DruidDataSourceproperty nameusername value${db.username}/propertyproperty namepassword value${db.password}/propertyproperty nameurl value${db.url}/propertyproperty namedriverClassName value${db.driverClassName}/property/bean!-- 配置SqlSession --bean idsqlSessionFactory classorg.mybatis.spring.SqlSessionFactoryBean!-- dataSource属性必选属性 --property namedataSource refdataSource/property!-- configLocation属性配置mybatis-config.xml非必选属性--property nameconfigLocation valueclasspath:mybatis-config.xml/property!-- mapperLocations属性用于配置Mapper.xml文件所在位置 非必选属性--property namemapperLocations valueclasspath:mapper/*.xml/property/bean!-- 扫描Mapper --bean idmapperScannerConfigurer classorg.mybatis.spring.mapper.MapperScannerConfigurerproperty namebasePackage valuecom.wmj.mapper/property/bean!-- 事务核心管理器,封装了所有事务操作. 依赖于连接池 --bean nametransactionManagerclassorg.springframework.jdbc.datasource.DataSourceTransactionManagerproperty namedataSource refdataSource/property/bean!-- 配置事务通知 --tx:advice idtxAdvice transaction-managertransactionManagertx:attributes!-- 企业中配置CRUD方法一般使用方法名通配符*的形式配置通知此时类中的方法名要和配置的方法名一致 --!-- 以方法为单位,指定方法应用什么事务属性isolation用于指定事务的隔离级别。默认值是DEFAULT表示使用数据库的默认隔离级别。propagation用于指定事务的传播行为。默认值是REQUIRED表示一定会有事务增删改的选择。查询方法可以选择SUPPORTS。read-only用于指定事务是否只读。只有查询方法才能设置为true。默认值是false表示读写。timeout用于指定事务的超时时间默认值是-1表示永不超时。如果指定了数值以秒为单位。rollback-for用于指定一个异常当产生该异常时事务回滚产生其他异常时事务不回滚。没有默认值。表示任何异常都回滚。no-rollback-for用于指定一个异常当产生该异常时事务不回滚产生其他异常时事务回滚。没有默认值。表示任何异常都回滚。--tx:method nametransferMoney isolationREPEATABLE_READpropagationREQUIRED read-onlyfalse //tx:attributes/tx:advice!-- 配置织入 --aop:config!-- 配置切点表达式 --aop:pointcut expressionexecution(* com.wmj.service.impl.*ServiceImpl.*(..))idtxPc /!-- 配置切面 : 通知切点 advice-ref:通知的名称 pointcut-ref:切点的名称 --aop:advisor advice-reftxAdvice pointcut-reftxPc //aop:config/beans
3、 测试
package com.test;import com.wmj.controller.TestController;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** --效率是成功的核心关键--** Author lzl* Date 2023/2/13 16:02*/public class Demo1 {Testpublic void testTX(){ClassPathXmlApplicationContext applicationContext new ClassPathXmlApplicationContext(application.xml);TestController controller (TestController)applicationContext.getBean(testController);controller.testTx(1,2,200.00);}
}
执行结果 当没有遇到异常时转账能正常执行不在演示此处演示当遇到异常时的事务效果 代码执行前数据库 当遇到异常代码终止执行此时数据库 转账并没有进行提交此时说明我们的事务配置成功
五、使用注解形式进行事务开发
spring给我们提供了事务注解开发的方式大大减少了代码编写十分方便 创建项目配置基本与上方相同我们将application.xml内容进行修改如下 ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:aophttp://www.springframework.org/schema/aopxmlns:txhttp://www.springframework.org/schema/txxmlns:contexthttp://www.springframework.org/schema/context xsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd !-- bean definitions here --!-- 导入外部配置文件 db.properties --context:property-placeholder locationclasspath:db.properties/context:property-placeholder!-- 配置数据源对象 --bean iddatasource classcom.alibaba.druid.pool.DruidDataSource!-- 导入 db.properties 中的值--property nameusername value${db.username}/propertyproperty namepassword value${db.password}/propertyproperty nameurl value${db.url}/propertyproperty namedriverClassName value${db.driver}/property/bean!-- 扫描对应包下的注解 --context:component-scan base-packagecom.wmj/context:component-scan!-- 配置sqlSessionFactory --bean idsqlSessionFactoryBean classorg.mybatis.spring.SqlSessionFactoryBean!-- 必选配置 --property namedataSource refdatasource/property!-- 非必选属性根据自己需求去配置 --!-- 导入 mybatis-config.xml --property nameconfigLocation valueclasspath:mybatis-config.xml/property!-- 导入 Mapper.xml 文件classpath后面不能有空格 --property namemapperLocations valueclasspath:mapper/*.xml/property/bean!-- 扫描 Mapper 接口生成代理对象 --bean idmapperScannerConfigurer classorg.mybatis.spring.mapper.MapperScannerConfigurer!-- 指定扫描的具体位置 --property namebasePackage valuecom.wmj.mapper/property/bean!-- 配置事务 --!-- 事务平台管理器封装了所有的事务操作依赖数据源 --bean idtransactionManager classorg.springframework.jdbc.datasource.DataSourceTransactionManagerproperty namedataSource refdatasource/property/bean!-- 开启注解驱动事务支持 --tx:annotation-driven/tx:annotation-driven/beans将业务实现类改造如下 package com.wmj.service.impl;import com.wmj.mapper.AccountMapper;
import com.wmj.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;/*** Transactional 配置事务* 可以写在类上也可以写在方法上*/
//Transactional(isolation Isolation.REPEATABLE_READ,propagation Propagation.REQUIRED,readOnly false)
Service
package com.wmj.service.impl;import com.wmj.mapper.TestMapper;
import com.wmj.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** --效率是成功的核心关键--** Author lzl* Date 2023/2/13 16:11*/
/*** Transactional 配置事务* 可以写在类上也可以写在方法上*/
//Transactional(isolation Isolation.REPEATABLE_READ,propagation Propagation.REQUIRED,readOnly false)
Service
public class TestServiceImpl implements TestService {Autowiredprivate TestMapper mapper;Transactional(isolation Isolation.REPEATABLE_READ,propagation Propagation.REQUIRED,readOnly false)Overridepublic void transferMoney(Integer id1, Integer id2, Double money) {mapper.dmoney(id1,money);int a 1/0;mapper.amoney(id2,money);}
}
测试略 总结
本次主要记录了spring的事务支持及其两种开发方式。再会