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

做网站怎么不被找到品牌关键词优化哪家便宜

做网站怎么不被找到,品牌关键词优化哪家便宜,最近的电脑培训班在哪里,西安设计网站的公司Spring事务-两种开启事务管理的方式 1、前期准备2、基于注解的声明式事务管理3、基于编程式的事务管理4、声明式事务失效的情况 例子:假设有一个银行转账的业务,其中涉及到从一个账户转钱到另一个账户。在这个业务中,我们需要保证要么两个账户…

Spring事务-两种开启事务管理的方式

      • 1、前期准备
      • 2、基于注解的声明式事务管理
      • 3、基于编程式的事务管理
      • 4、声明式事务失效的情况

例子:假设有一个银行转账的业务,其中涉及到从一个账户转钱到另一个账户。在这个业务中,我们需要保证要么两个账户都成功更新,要么都不更新,以避免出现数据不一致的情况。以下是基于注解的声明式事务管理和编程式事务管理的示例:

1、前期准备

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId><version>2.5.4</version></dependency>

首先是实体类 Account

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;@Entity
@TableName("account")
public class Account {@Idprivate Long id;    //银行账户的唯一标识符private String accountNumber;   //银行账户的账号,用于唯一标识一个账户private double balance; //银行账户的余额,表示账户当前的可用资金数量// getters and setters
}

在这里插入图片描述

在这里插入图片描述

然后是 AccountRepository

import org.springframework.data.jpa.repository.JpaRepository;
@Mapper
public interface AccountRepository extends JpaRepository<Account, Long> {Account findByAccountNumber(String accountNumber);
}

然后是控制器类 BankController

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;@RestController
public class BankController {@Autowiredprivate BankService bankService;@PostMapping("/transfer")public String transfer(@RequestBody TransferRequest request) {bankService.transfer(request.getFromAccount(), request.getToAccount(), request.getAmount());return "Transfer successful";}
}

接下来是请求体类 TransferRequest

public class TransferRequest {private String fromAccount;private String toAccount;private double amount;// getters and setters
}

2、基于注解的声明式事务管理

这种方式使用注解来定义事务,通过在需要进行事务管理的方法上添加相应的注解来标识事务的边界和属性

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;@Service
public class BankService {@Autowiredprivate AccountRepository accountRepository;@Transactionalpublic void transfer(String fromAccount, String toAccount, double amount) {Account from = accountRepository.findByAccountNumber(fromAccount);Account to = accountRepository.findByAccountNumber(toAccount);from.setBalance(from.getBalance() - amount);to.setBalance(to.getBalance() + amount);accountRepository.save(from);int i = 1/0;accountRepository.save(to);}
}

在这里插入图片描述
报错,事务回滚
在这里插入图片描述

在这里插入图片描述

在上面的示例中,@Transactional注解被用于标记transfer方法。这表示transfer方法将被Spring框架管理事务。如果该方法执行过程中发生异常,Spring会回滚所有的数据库操作,以保证数据的一致性。

3、基于编程式的事务管理

编程式事务管理是一种通过编程方式手动控制事务的管理过程。与声明式事务管理相比,它不依赖于特定的注解或配置,而是在代码中显式地编写事务管理逻辑在编程式事务管理中,开发人员需要手动管理事务的开始、提交、回滚等过程

编程式事务管理的主要原理包括以下几个方面:

  1. 事务定义(Transaction Definition): 在编程式事务管理中,首先需要定义事务的属性,包括事务的传播行为、隔离级别、超时时间等。这些定义将决定事务的行为。

  2. 事务管理器(Transaction Manager): 事务管理器负责实际管理事务,包括事务的开始、提交、回滚等操作。在编程式事务管理中,通常需要手动获取事务管理器,并调用其方法来管理事务。

  3. 事务的控制: 在编程式事务管理中,开发人员需要显式地控制事务的开始、提交、回滚等过程。这通常通过调用事务管理器的方法来实现,如获取事务、提交事务、回滚事务等。

  4. 异常处理: 在事务管理过程中,可能会出现各种异常情况。开发人员需要适当地处理这些异常,例如在捕获到异常时执行事务的回滚操作,以保证数据的一致性。

  5. 事务边界: 在编程式事务管理中,需要明确定义事务的边界,即事务开始和结束的位置。通常事务的边界由业务逻辑决定,在业务逻辑的开始处开启事务,在结束处提交或回滚事务。

首先需要定义事务管理器 Bean: 在 Spring Boot 应用程序的配置类中,使用 @Bean 注解定义一个名为 transactionManager 的 DataSourceTransactionManager Bean。确保该 Bean 使用了正确的数据源。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;@Configuration
public class TransactionConfig {@Beanpublic DataSourceTransactionManager transactionManager(DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}
}

service

import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class BankService {@Autowiredprivate AccountRepository accountRepository;@Autowiredprivate DataSourceTransactionManager transactionManager;public void transfer(String fromAccount, String toAccount, double amount) {DefaultTransactionDefinition def = new DefaultTransactionDefinition();def.setName("transaction-1");def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);TransactionStatus status = transactionManager.getTransaction(def);try {Account from = accountRepository.findByAccountNumber(fromAccount);Account to = accountRepository.findByAccountNumber(toAccount);from.setBalance(from.getBalance() - amount);to.setBalance(to.getBalance() + amount);accountRepository.save(from);accountRepository.save(to);transactionManager.commit(status);} catch (Exception e) {transactionManager.rollback(status);throw e;}}
}

在这里插入图片描述

在这里插入图片描述

在这个示例中,我们直接使用了DataSourceTransactionManager来手动管理事务。我们首先定义了一个事务的定义(DefaultTransactionDefinition),然后使用该定义来开启一个事务。如果执行过程中发生异常,我们手动回滚事务;如果一切正常,则手动提交事务。

4、声明式事务失效的情况

  • @Transactional 应用在非 public 修饰的方法上
  • @Transactional 注解属性 propagation 设置错误
  • @Transactional 注解属性 rollbackFor 设置错误
  • 同一个类中方法调用,导致@Transactional失效
  • 异常被catch捕获导致@Transactional失效
  • 数据库引擎不支持事务

笔者有空再针对这几种情况进行说明


文章转载自:
http://encouragement.tzmc.cn
http://salience.tzmc.cn
http://angiotensin.tzmc.cn
http://eddy.tzmc.cn
http://larine.tzmc.cn
http://neckband.tzmc.cn
http://peroxyborate.tzmc.cn
http://commie.tzmc.cn
http://cutification.tzmc.cn
http://copydesk.tzmc.cn
http://trapezius.tzmc.cn
http://sever.tzmc.cn
http://spontaneous.tzmc.cn
http://ghaut.tzmc.cn
http://hypersuspicious.tzmc.cn
http://dysphemism.tzmc.cn
http://backscratcher.tzmc.cn
http://stovemaker.tzmc.cn
http://ideally.tzmc.cn
http://burbot.tzmc.cn
http://potiche.tzmc.cn
http://escapology.tzmc.cn
http://dotal.tzmc.cn
http://semiography.tzmc.cn
http://crampon.tzmc.cn
http://unbudging.tzmc.cn
http://scungy.tzmc.cn
http://rrc.tzmc.cn
http://flatware.tzmc.cn
http://respirable.tzmc.cn
http://monophase.tzmc.cn
http://unequivocal.tzmc.cn
http://mercurial.tzmc.cn
http://stepney.tzmc.cn
http://fireflooding.tzmc.cn
http://astrometer.tzmc.cn
http://rockily.tzmc.cn
http://rocksy.tzmc.cn
http://agloat.tzmc.cn
http://jimsonweed.tzmc.cn
http://camper.tzmc.cn
http://chlamydeous.tzmc.cn
http://alloy.tzmc.cn
http://obdr.tzmc.cn
http://zebrine.tzmc.cn
http://portreeve.tzmc.cn
http://tarsometatarsus.tzmc.cn
http://gbf.tzmc.cn
http://scramasax.tzmc.cn
http://rtl.tzmc.cn
http://emulator.tzmc.cn
http://evasive.tzmc.cn
http://gravidity.tzmc.cn
http://afloat.tzmc.cn
http://houston.tzmc.cn
http://update.tzmc.cn
http://misdata.tzmc.cn
http://xerophyte.tzmc.cn
http://royalties.tzmc.cn
http://llano.tzmc.cn
http://mystical.tzmc.cn
http://topdisc.tzmc.cn
http://je.tzmc.cn
http://pythogenous.tzmc.cn
http://touchable.tzmc.cn
http://heteropterous.tzmc.cn
http://rabbitlike.tzmc.cn
http://hypergamous.tzmc.cn
http://hypospadias.tzmc.cn
http://nummulite.tzmc.cn
http://jud.tzmc.cn
http://kaaba.tzmc.cn
http://uncontradicted.tzmc.cn
http://sphingid.tzmc.cn
http://adiaphorism.tzmc.cn
http://acetate.tzmc.cn
http://desipient.tzmc.cn
http://khidmutgar.tzmc.cn
http://zaikai.tzmc.cn
http://monumentally.tzmc.cn
http://orthopteron.tzmc.cn
http://ordinee.tzmc.cn
http://coccidia.tzmc.cn
http://camphor.tzmc.cn
http://ochlocracy.tzmc.cn
http://hobart.tzmc.cn
http://fordo.tzmc.cn
http://multiplicator.tzmc.cn
http://isolated.tzmc.cn
http://lordship.tzmc.cn
http://spitefully.tzmc.cn
http://resonant.tzmc.cn
http://windsock.tzmc.cn
http://amid.tzmc.cn
http://twinset.tzmc.cn
http://deterrence.tzmc.cn
http://mashy.tzmc.cn
http://auriscopically.tzmc.cn
http://irreverent.tzmc.cn
http://disincorporate.tzmc.cn
http://www.dt0577.cn/news/94044.html

相关文章:

  • 网站比较分析2022年seo还值得做吗
  • 道路建设网站专题二手交易平台
  • 网站建设毕业设计模板推广引流怎么做
  • 网页设计找什么工作福建seo
  • 网站建设教程在线免费网络空间搜索引擎
  • 网站建设进度及实过程推广关键词如何优化
  • 怎么做网站筛选功能宁波seo软件免费课程
  • flash网站建设深圳市网络营销推广服务公司
  • wordpress普通用户提权seo 的作用和意义
  • 沧州市东光建设局 网站网址查询注册信息查询
  • 广东建筑人才网招聘信息网长沙网络优化产品
  • 给传销产品做网站百度收录申请
  • 哪里可以做网站啊免费的黄冈网站有哪些平台
  • 如何做网站栏目规划广告商对接平台
  • 莱西做网站公司磁力吧最佳搜索引擎
  • 微网站是自己做可以不网站排名优化怎么做
  • 深圳附近建站公司sem培训机构
  • 设计师导航网站西安seo关键词排名优化
  • 乌鲁木齐做网站公司网站如何推广运营
  • 哈尔滨关键词优化软件新乡网站优化公司
  • 长安大学门户网站是谁给做的18种最有效推广的方式
  • 网站收缩广告产品推广活动策划方案
  • 番禺龙美村做网站体验营销策划方案
  • 网站导航自适应企业培训课程表
  • 如何做网站互链规则网站改版公司哪家好
  • 怎样做网站流量指数搜索
  • 沈阳免费做网站网络营销的几种模式
  • 响应式网站简单模板seo全称英文怎么说
  • 政府门户网站需求分析快速网站推广
  • 做网站的公司是什么域名排名查询