当前位置: 首页 > news >正文

淄博网站建设排行榜注册城乡规划师报考条件

淄博网站建设排行榜,注册城乡规划师报考条件,江山做网站,阿里ecs wordpressspring事务管理(以转账为例) 概述 Spring事务管理提供了一种在应用程序中管理事务的机制,它抽象了底层的事务管理细节,使得开发者可以更加专注于业务逻辑的实现,而不必过多关心事务的处理。以下是Spring事务管理的一…

spring事务管理(以转账为例)

概述

Spring事务管理提供了一种在应用程序中管理事务的机制,它抽象了底层的事务管理细节,使得开发者可以更加专注于业务逻辑的实现,而不必过多关心事务的处理。以下是Spring事务管理的一般概述:

  1. 事务的概念: 事务是一组操作,它们被看作一个不可分割的工作单元,要么全部执行成功,要么全部不执行。在数据库中,事务通常用于确保数据库的一致性和完整性。

  2. Spring事务抽象: Spring提供了一个强大的事务管理抽象,它包括两个核心概念:事务管理器(TransactionManager)事务定义(TransactionDefinition)

    • 事务管理器(TransactionManager): 是一个接口,它负责事务的启动、提交和回滚。Spring有多个实现类,可以适用于不同的事务管理场景。

    • 事务定义(TransactionDefinition): 定义了事务的属性,如隔离级别、传播行为、超时等。Spring允许通过XML或注解的方式来配置事务的属性。

  3. 声明式事务管理: Spring支持声明式事务管理,这意味着你可以通过配置文件或注解的方式来声明事务,而不必在代码中显式编写事务管理的逻辑。

    • XML配置方式: 通过XML配置文件中的 元素来声明事务。

    • 注解配置方式: 使用 @Transactional 注解来标注需要事务管理的方法,然后通过配置启用注解驱动事务管理。

  4. 编程式事务管理: 除了声明式事务管理外,Spring还支持编程式事务管理,这意味着你可以在代码中显式控制事务的开始、提交和回滚。

    • 使用 TransactionTemplate 类: Spring提供了 TransactionTemplate 类,它封装了事务的基本操作,你可以在代码中使用它来编程式地管理事务。

  5. 事务传播行为: 在Spring中,事务的传播行为(Transaction Propagation Behavior)定义了在一个事务方法被另一个事务方法调用时,应该如何处理事务。Spring定义了七种事务传播行为:

    1. PROPAGATION_REQUIRED(默认):

      • 如果当前存在事务,则加入该事务;如果不存在事务,则创建一个新的事务。这是最常见的传播行为。

    2. PROPAGATION_SUPPORTS:

      • 如果当前存在事务,则加入该事务;如果不存在事务,则以非事务的方式执行。

    3. PROPAGATION_MANDATORY:

      • 如果当前存在事务,则加入该事务;如果不存在事务,则抛出异常。要求外部调用方必须在事务中调用。

    4. PROPAGATION_REQUIRES_NEW:

      • 无论当前是否存在事务,都创建一个新的事务。如果有事务存在,将它挂起。

    5. PROPAGATION_NOT_SUPPORTED:

      • 以非事务的方式执行操作,如果当前存在事务,则将其挂起。

    6. PROPAGATION_NEVER:

      • 以非事务方式执行操作,如果当前存在事务,则抛出异常。

    7. PROPAGATION_NESTED:

      • 如果当前存在事务,则创建一个嵌套事务,并且它是当前事务的一个保存点(savepoint)。如果不存在事务,则行为类似于PROPAGATION_REQUIRED

  6. 事务隔离级别: 事务隔离级别定义了在多个事务同时执行时,一个事务对数据的修改对其他事务的可见性程度。Spring定义了五个事务隔离级别,每个级别都有不同的特性和影响:

    1. READ_UNCOMMITTED(读取未提交):

      • 特性:允许一个事务读取另一个事务未提交的数据。

      • 可能问题:脏读、不可重复读、幻读。

    2. READ_COMMITTED(读取已提交):

      • 特性:一个事务只能读取已经提交的另一个事务的数据。

      • 可能问题:不可重复读、幻读。

    3. REPEATABLE_READ(可重复读):

      • 特性:确保事务可以多次从相同的数据集读取相同的数据,即使其他事务正在修改这些数据。

      • 可能问题:幻读。

    4. SERIALIZABLE(序列化):

      • 特性:事务是串行执行的,所有的事务都按顺序依次执行,不允许并发执行。

      • 可能问题:性能较低,但避免了脏读、不可重复读、幻读。

    5. DEFAULT:

      • 使用数据库默认的隔离级别,通常是数据库的默认配置。

  7. 事务回滚规则: Spring允许你配置哪些异常触发事务回滚,哪些异常不触发事务回滚。

  8. 分布式事务: Spring提供对分布式事务的支持,通过 JtaTransactionManager 实现了对JTA事务的集成。

PlatformTransactionManager概述

Spring的事务管理模块定义了一个平台事务管理器(PlatformTransactionManager)接口,该接口有多种实现,以适应不同的事务管理机制。主要的实现类包括:

  1. DataSourceTransactionManager: 用于基于JDBC的事务管理,适用于关系型数据库,如MySQL、PostgreSQL等。

  2. HibernateTransactionManager: 用于基于Hibernate的事务管理,适用于使用Hibernate框架的应用。

  3. JtaTransactionManager: 用于基于Java EE的JTA(Java Transaction API)事务管理,适用于复杂的分布式事务场景。

  4. JpaTransactionManager: 用于基于JPA(Java Persistence API)的事务管理,适用于使用JPA的应用。

  5. 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 gold=gold-? where name=?",money,man);
•    }
​
•    @Override
•    public void in(String man,int money) {
•        jdbcTemplate.update("update account set gold=gold+? 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 i=1/0;accountdao.in(inman,money);}
}

ApplicationContext.xml配置

<?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"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://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.xsd"><context:property-placeholder location="classpath:mysql.properties"></context:property-placeholder>配置数据源<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.Driver}"></property><property name="jdbcUrl" value="${jdbc.url}"></property><property name="user" value="${jdbc.user}"></property><property name="password" value="${jdbc.password}"></property></bean>配置jdbc模板<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="datasource"></property></bean><bean id="account" class="dao.Impl.accountImpl"><property name="jdbcTemplate" ref="jdbcTemplate"></property></bean><bean id="accountService" class="service.Impl.accountServiceImpl"><property name="accountdao" ref="account"></property></bean>
<!--    配置事务管理平台-->
这里是基于springjdbc模板实现的所以用的是DataSource<bean id="transaction"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="datasource"></property></bean>
<!--    通知--><tx:advice id="myAdvice" transaction-manager="transaction"><tx:attributes>
<!--            哪些方法被增强--><tx:method name="transfer"isolation="READ_COMMITTED"propagation="REQUIRED" read-only="false"   因为转账肯定需要修改 所以不是只读/></tx:attributes></tx:advice>
<!--    配置事务的aop织入--><aop:config><aop:advisor advice-ref="myAdvice"pointcut="execution(* service.Impl.accountServiceImpl.*(..))"/></aop:config>
</beans>

注解式声明式事务管理

xml配置

<?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"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://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.xsd"><context:property-placeholder location="classpath:mysql.properties"></context:property-placeholder>配置数据源<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.Driver}"></property><property name="jdbcUrl" value="${jdbc.url}"></property><property name="user" value="${jdbc.user}"></property><property name="password" value="${jdbc.password}"></property></bean>配置jdbc模板<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="datasource"></property></bean><bean id="account" class="dao.Impl.accountImpl"><property name="jdbcTemplate" ref="jdbcTemplate"></property></bean><bean id="accountService" class="service.Impl.accountServiceImpl"><property name="accountdao" ref="account"></property></bean>
<!--    配置事务管理平台-->
这里是基于springjdbc模板实现的所以用的是DataSource<bean id="transaction"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="datasource"></property></bean>新加<tx:annotation-driven transaction-manager="transaction"></tx:annotation-driven><context:component-scan base-package="org.dao.Impl" /><context:component-scan base-package="org.service.Impl" />组件扫描<aop:aspectj-autoproxy></aop:aspectj-autoproxy>    启用 AspectJ 自动代理<context:annotation-config></context:annotation-config>启用注解驱动的配置,包括支持注解驱动的 AOP。在这个上下文中,主要是为了支持 @Transactional 注解,它用于启用声明式事务管理。在这个配置中,它会启用对带有 @Repository, @Service, @Controller 和其他注解的类的处理,以及其他一些注解相关的功能。

@Transactional() 等价于

<!-- 通知--> <tx:advice id="myAdvice" transaction-manager="transaction"> tx:attributes <!-- 哪些方法被增强--> <tx:method name="transfer" isolation="READ_COMMITTED" propagation="REQUIRED" read-only="false" 因为转账肯定需要修改 所以不是只读 /> /tx:attributes /tx:advice <!-- 配置事务的aop织入--> aop:config <aop:advisor advice-ref="myAdvice" pointcut="execution(* service.Impl.accountServiceImpl.*(..))"/> /aop:config </beans>

总结

转账这个例子 就是主动设计的错误是脏读 如果转账时出现网络 错误 一方转出了钱 ,一方没有收到钱,此时是不合理的,所以需要事务管理

http://www.qdjiajiao.com/news/5690.html

相关文章:

  • 长沙网站优化排名推广优化营商环境心得体会
  • 券妈妈网站是如何做的seo排名优化教程
  • 南昌网站建设模板合作阿里巴巴国际贸易网站
  • 网站网页压缩黄石seo
  • 导航网站制作教程seo推广的公司
  • 网站建设贵阳公司营销策划方案案例
  • 国外哪些网站是python做的郑州seo排名工具
  • wordpress应用商店主题seo一般包括哪些内容
  • 橙子建站是监控手机云南网站推广公司
  • 南京网站建设 个人杭州疫情最新情况
  • 做财务还是网站运营sem是什么显微镜
  • 快速网站优化技巧网站建设是干嘛的
  • 招聘网站上怎么做推广百度网盘搜索引擎
  • 织梦门户网站源码下载google搜索引擎下载
  • 石排镇专业建站公司百度 营销怎么收费
  • 做投票的网站赚钱嘛北京网站推广公司
  • 做网站是什么鬼郑州seo推广外包
  • 四方坪网站建设网站关键词推广
  • 常州网站推广优化百度惠生活怎么优化排名
  • 从化区住房和建设局网站百度指数怎么算
  • Python用数据库做网站灰色关键词排名
  • WordPress禁用自适应专业seo站长工具
  • 搞钱的路子网站本地广告推广平台哪个好
  • 手机如何做api网站网络营销的实现方式包括
  • 网站流量报告2022最新新闻
  • 网站建设需要会什么软件有哪些内容今日要闻
  • 池州做网站上海百度推广公司排名
  • 深圳网站制作有名 乐云践新郑州网络营销公司排名
  • 工程认证网站的建设长沙网站制作费用
  • 沈阳做网站哪家公司好站长统计软件