网站开发完没人运营,sns社交网站 有哪些,暴雪将至,工会网站升级改造建设方案spring事务管理#xff08;以转账为例#xff09;
概述
Spring事务管理提供了一种在应用程序中管理事务的机制#xff0c;它抽象了底层的事务管理细节#xff0c;使得开发者可以更加专注于业务逻辑的实现#xff0c;而不必过多关心事务的处理。以下是Spring事务管理的一…spring事务管理以转账为例
概述
Spring事务管理提供了一种在应用程序中管理事务的机制它抽象了底层的事务管理细节使得开发者可以更加专注于业务逻辑的实现而不必过多关心事务的处理。以下是Spring事务管理的一般概述 事务的概念 事务是一组操作它们被看作一个不可分割的工作单元要么全部执行成功要么全部不执行。在数据库中事务通常用于确保数据库的一致性和完整性。 Spring事务抽象 Spring提供了一个强大的事务管理抽象它包括两个核心概念事务管理器TransactionManager 和 事务定义TransactionDefinition。 事务管理器TransactionManager 是一个接口它负责事务的启动、提交和回滚。Spring有多个实现类可以适用于不同的事务管理场景。 事务定义TransactionDefinition 定义了事务的属性如隔离级别、传播行为、超时等。Spring允许通过XML或注解的方式来配置事务的属性。 声明式事务管理 Spring支持声明式事务管理这意味着你可以通过配置文件或注解的方式来声明事务而不必在代码中显式编写事务管理的逻辑。 XML配置方式 通过XML配置文件中的 和 元素来声明事务。 注解配置方式 使用 Transactional 注解来标注需要事务管理的方法然后通过配置启用注解驱动事务管理。 编程式事务管理 除了声明式事务管理外Spring还支持编程式事务管理这意味着你可以在代码中显式控制事务的开始、提交和回滚。 使用 TransactionTemplate 类 Spring提供了 TransactionTemplate 类它封装了事务的基本操作你可以在代码中使用它来编程式地管理事务。 事务传播行为 在Spring中事务的传播行为Transaction Propagation Behavior定义了在一个事务方法被另一个事务方法调用时应该如何处理事务。Spring定义了七种事务传播行为 PROPAGATION_REQUIRED默认 如果当前存在事务则加入该事务如果不存在事务则创建一个新的事务。这是最常见的传播行为。 PROPAGATION_SUPPORTS 如果当前存在事务则加入该事务如果不存在事务则以非事务的方式执行。 PROPAGATION_MANDATORY 如果当前存在事务则加入该事务如果不存在事务则抛出异常。要求外部调用方必须在事务中调用。 PROPAGATION_REQUIRES_NEW 无论当前是否存在事务都创建一个新的事务。如果有事务存在将它挂起。 PROPAGATION_NOT_SUPPORTED 以非事务的方式执行操作如果当前存在事务则将其挂起。 PROPAGATION_NEVER 以非事务方式执行操作如果当前存在事务则抛出异常。 PROPAGATION_NESTED 如果当前存在事务则创建一个嵌套事务并且它是当前事务的一个保存点savepoint。如果不存在事务则行为类似于PROPAGATION_REQUIRED。 事务隔离级别 事务隔离级别定义了在多个事务同时执行时一个事务对数据的修改对其他事务的可见性程度。Spring定义了五个事务隔离级别每个级别都有不同的特性和影响 READ_UNCOMMITTED读取未提交 特性允许一个事务读取另一个事务未提交的数据。 可能问题脏读、不可重复读、幻读。 READ_COMMITTED读取已提交 特性一个事务只能读取已经提交的另一个事务的数据。 可能问题不可重复读、幻读。 REPEATABLE_READ可重复读 特性确保事务可以多次从相同的数据集读取相同的数据即使其他事务正在修改这些数据。 可能问题幻读。 SERIALIZABLE序列化 特性事务是串行执行的所有的事务都按顺序依次执行不允许并发执行。 可能问题性能较低但避免了脏读、不可重复读、幻读。 DEFAULT 使用数据库默认的隔离级别通常是数据库的默认配置。 事务回滚规则 Spring允许你配置哪些异常触发事务回滚哪些异常不触发事务回滚。 分布式事务 Spring提供对分布式事务的支持通过 JtaTransactionManager 实现了对JTA事务的集成。
PlatformTransactionManager概述
Spring的事务管理模块定义了一个平台事务管理器PlatformTransactionManager接口该接口有多种实现以适应不同的事务管理机制。主要的实现类包括 DataSourceTransactionManager 用于基于JDBC的事务管理适用于关系型数据库如MySQL、PostgreSQL等。 HibernateTransactionManager 用于基于Hibernate的事务管理适用于使用Hibernate框架的应用。 JtaTransactionManager 用于基于Java EE的JTAJava Transaction API事务管理适用于复杂的分布式事务场景。 JpaTransactionManager 用于基于JPAJava Persistence API的事务管理适用于使用JPA的应用。 WebLogicJtaTransactionManager 专门用于WebLogic服务器上的JTA事务管理。
实体类domain层
package domain;
public class account {private String name;private int gold;
public account(String name, int gold) {this.name name;this.gold gold;}
public account() {}
public String getName() {return name;}
public void setName(String name) {this.name name;}
public int getGold() {return gold;}
public void setGold(int gold) {this.gold gold;}
Overridepublic String toString() {return account{ name name \ , gold gold };}
}
Dao层
接口
package dao;
public interface accountDao {public void out(String man,int money);转出public void in(String man,int money);转入
}
实现类
package dao.Impl;
import dao.accountDao;
import org.springframework.jdbc.core.JdbcTemplate;
public class accountImpl implements accountDao {JdbcTemplate jdbcTemplate ;
• public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
• this.jdbcTemplate jdbcTemplate;
• }
• Override
• public void out(String man, int money) {
• jdbcTemplate.update(update account set goldgold-? where name?,money,man);
• }
• Override
• public void in(String man,int money) {
• jdbcTemplate.update(update account set goldgold? where name?,money,man);
• }
} service层
接口
package dao;
public interface accountDao {public void out(String man,int money);public void in(String man,int money);
}
实现类
package service.Impl;
import dao.accountDao;
import service.accountService;
public class accountServiceImpl implements accountService {
accountDao accountdao;
public void setAccountdao(accountDao accountdao) {this.accountdao accountdao;}
Overridepublic void transfer(String outman, String inman, int money) {accountdao.out(outman,money);int i1/0;accountdao.in(inman,money);}
}
ApplicationContext.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:txhttp://www.springframework.org/schema/txxmlns:aophttp://www.springframework.org/schema/aopxsi: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/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdcontext:property-placeholder locationclasspath:mysql.properties/context:property-placeholder配置数据源bean iddatasource classcom.mchange.v2.c3p0.ComboPooledDataSourceproperty namedriverClass value${jdbc.Driver}/propertyproperty namejdbcUrl value${jdbc.url}/propertyproperty nameuser value${jdbc.user}/propertyproperty namepassword value${jdbc.password}/property/bean配置jdbc模板bean idjdbcTemplate classorg.springframework.jdbc.core.JdbcTemplateproperty namedataSource refdatasource/property/beanbean idaccount classdao.Impl.accountImplproperty namejdbcTemplate refjdbcTemplate/property/beanbean idaccountService classservice.Impl.accountServiceImplproperty nameaccountdao refaccount/property/bean
!-- 配置事务管理平台--
这里是基于springjdbc模板实现的所以用的是DataSourcebean idtransactionclassorg.springframework.jdbc.datasource.DataSourceTransactionManagerproperty namedataSource refdatasource/property/bean
!-- 通知--tx:advice idmyAdvice transaction-managertransactiontx:attributes
!-- 哪些方法被增强--tx:method nametransferisolationREAD_COMMITTEDpropagationREQUIRED read-onlyfalse 因为转账肯定需要修改 所以不是只读//tx:attributes/tx:advice
!-- 配置事务的aop织入--aop:configaop:advisor advice-refmyAdvicepointcutexecution(* service.Impl.accountServiceImpl.*(..))//aop:config
/beans
注解式声明式事务管理
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:txhttp://www.springframework.org/schema/txxmlns:aophttp://www.springframework.org/schema/aopxsi: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/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdcontext:property-placeholder locationclasspath:mysql.properties/context:property-placeholder配置数据源bean iddatasource classcom.mchange.v2.c3p0.ComboPooledDataSourceproperty namedriverClass value${jdbc.Driver}/propertyproperty namejdbcUrl value${jdbc.url}/propertyproperty nameuser value${jdbc.user}/propertyproperty namepassword value${jdbc.password}/property/bean配置jdbc模板bean idjdbcTemplate classorg.springframework.jdbc.core.JdbcTemplateproperty namedataSource refdatasource/property/beanbean idaccount classdao.Impl.accountImplproperty namejdbcTemplate refjdbcTemplate/property/beanbean idaccountService classservice.Impl.accountServiceImplproperty nameaccountdao refaccount/property/bean
!-- 配置事务管理平台--
这里是基于springjdbc模板实现的所以用的是DataSourcebean idtransactionclassorg.springframework.jdbc.datasource.DataSourceTransactionManagerproperty namedataSource refdatasource/property/bean新加tx:annotation-driven transaction-managertransaction/tx:annotation-drivencontext:component-scan base-packageorg.dao.Impl /context:component-scan base-packageorg.service.Impl /组件扫描aop:aspectj-autoproxy/aop:aspectj-autoproxy 启用 AspectJ 自动代理context:annotation-config/context:annotation-config启用注解驱动的配置包括支持注解驱动的 AOP。在这个上下文中主要是为了支持 Transactional 注解它用于启用声明式事务管理。在这个配置中它会启用对带有 Repository, Service, Controller 和其他注解的类的处理以及其他一些注解相关的功能。
Transactional() 等价于
!-- 通知-- tx:advice idmyAdvice transaction-managertransaction tx:attributes !-- 哪些方法被增强-- tx:method nametransfer isolationREAD_COMMITTED propagationREQUIRED read-onlyfalse 因为转账肯定需要修改 所以不是只读 / /tx:attributes /tx:advice !-- 配置事务的aop织入-- aop:config aop:advisor advice-refmyAdvice pointcutexecution(* service.Impl.accountServiceImpl.*(..))/ /aop:config /beans
总结
转账这个例子 就是主动设计的错误是脏读 如果转账时出现网络 错误 一方转出了钱 一方没有收到钱此时是不合理的所以需要事务管理