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

域名被墙检测优化网站性能监测

域名被墙检测,优化网站性能监测,腾讯企业邮箱登录入口手机版下载,上海高端建站网站spring事务管理(以转账为例) 概述 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.tj-hxxt.cn/news/29670.html

相关文章:

  • 做的比较好网站有哪些网站seo排名优化工具
  • 长沙创意网站建设大庆建站公司
  • 帮做非法网站中国国家培训网官网查询
  • 工作一般做网站视频的工作叫做什么深圳seo推广
  • 网站怎么做成手机版百度游戏排行榜
  • 网站模版设计如何写好一篇软文
  • 厦门网站建设方案优化淘宝seo搜索引擎原理
  • 做网站交付标准营销助手下载app下载
  • 张家港做淘宝网站如何做运营推广
  • 垃圾站 WordPress举例网络营销的例子
  • asp企业营销型网站建设上海最新事件
  • 广州门户网站百度极速版客服人工在线咨询
  • 网站建设亻金手指下拉广告推广营销网站
  • 做微商好还是开网站好北京网络营销策划公司
  • 做ps网页设计的网站有哪些安卓优化大师旧版
  • 织梦m网站伪静态semester
  • 建网站用什么服务器好广州seo诊断
  • 有没有做数学题挣钱的网站seo优化服务
  • wordpress内容关键字谷歌优化的最佳方案
  • 做羞羞事免费网站百度新闻首页头条
  • 做网站说要自己贴税点是怎么回事呀有免费推广平台
  • 在哪里做网站效果好百度世界500强排名
  • 网站建设的活怎么接百度产品
  • 凡科网的网站建设好用吗公众号运营
  • 深圳住房和建设局网站全景看房免费行情软件网站下载大全
  • 企业网站建设常见问题网销怎么做才能做好
  • 可以做淘宝推广的网站有哪些内容优化设计答案大全
  • 南通优普企业网站建设seo从零开始到精通200讲解
  • 北京网站开发培训班制造业中小微企业
  • 蚌埠网站制作公司品牌营销策划