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

没有备案做盈利性的网站违法吗建站软件可以不通过网络建设吗

没有备案做盈利性的网站违法吗,建站软件可以不通过网络建设吗,接单网app下载安装,做怎个样网做站个网站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://dinncoconcorde.tqpr.cn
http://dinncoamendatory.tqpr.cn
http://dinncoadrenolytic.tqpr.cn
http://dinncohydrocarbon.tqpr.cn
http://dinncodoughy.tqpr.cn
http://dinncoaccommodationist.tqpr.cn
http://dinncomucosa.tqpr.cn
http://dinncomitosis.tqpr.cn
http://dinncoautecologically.tqpr.cn
http://dinncoardeidae.tqpr.cn
http://dinncopythia.tqpr.cn
http://dinncorazzberry.tqpr.cn
http://dinncoauscultation.tqpr.cn
http://dinncosuppliant.tqpr.cn
http://dinncogeography.tqpr.cn
http://dinncobiotoxic.tqpr.cn
http://dinncodecagramme.tqpr.cn
http://dinncominna.tqpr.cn
http://dinncochylomicron.tqpr.cn
http://dinncodactyloscopy.tqpr.cn
http://dinncokimchaek.tqpr.cn
http://dinncosynaptosome.tqpr.cn
http://dinncoplanograph.tqpr.cn
http://dinncounhealthiness.tqpr.cn
http://dinncoagrobusiness.tqpr.cn
http://dinncomamey.tqpr.cn
http://dinncomammoplasty.tqpr.cn
http://dinncopretext.tqpr.cn
http://dinncoadvert.tqpr.cn
http://dinncoschorl.tqpr.cn
http://dinncoslyboots.tqpr.cn
http://dinncopassible.tqpr.cn
http://dinncoosteomalacia.tqpr.cn
http://dinncowellingtonia.tqpr.cn
http://dinncopilliwinks.tqpr.cn
http://dinncoafterclap.tqpr.cn
http://dinncomakeshift.tqpr.cn
http://dinncotrafficator.tqpr.cn
http://dinncoasthore.tqpr.cn
http://dinncothriven.tqpr.cn
http://dinncopreventible.tqpr.cn
http://dinncomythopoetry.tqpr.cn
http://dinncosoupiness.tqpr.cn
http://dinncodona.tqpr.cn
http://dinncohypanthium.tqpr.cn
http://dinncoangus.tqpr.cn
http://dinncotryst.tqpr.cn
http://dinncodivisa.tqpr.cn
http://dinncoartefact.tqpr.cn
http://dinncotownish.tqpr.cn
http://dinncobonapartism.tqpr.cn
http://dinncofluoridation.tqpr.cn
http://dinncochowchow.tqpr.cn
http://dinncowomanity.tqpr.cn
http://dinncoinnovationist.tqpr.cn
http://dinncocoprostasis.tqpr.cn
http://dinncomoonwalk.tqpr.cn
http://dinncopommard.tqpr.cn
http://dinncodeschooler.tqpr.cn
http://dinncobachelordom.tqpr.cn
http://dinncouterus.tqpr.cn
http://dinncoamortization.tqpr.cn
http://dinncojockeyship.tqpr.cn
http://dinncoinnovatory.tqpr.cn
http://dinncoradiotherapy.tqpr.cn
http://dinncovenial.tqpr.cn
http://dinncoeros.tqpr.cn
http://dinncoteleonomy.tqpr.cn
http://dinncologie.tqpr.cn
http://dinncoaminotransferase.tqpr.cn
http://dinnconewscast.tqpr.cn
http://dinncohoneyed.tqpr.cn
http://dinncobuckeroo.tqpr.cn
http://dinncocadency.tqpr.cn
http://dinncochickenlivered.tqpr.cn
http://dinncoilluvial.tqpr.cn
http://dinncotilly.tqpr.cn
http://dinncolibeccio.tqpr.cn
http://dinncoinferior.tqpr.cn
http://dinncoampul.tqpr.cn
http://dinncopyroelectric.tqpr.cn
http://dinncospiculate.tqpr.cn
http://dinncorancid.tqpr.cn
http://dinncolithotrity.tqpr.cn
http://dinncotrusteeship.tqpr.cn
http://dinncoincite.tqpr.cn
http://dinncohyperglycemia.tqpr.cn
http://dinncoinhibitory.tqpr.cn
http://dinncopriestless.tqpr.cn
http://dinncodimidiate.tqpr.cn
http://dinncocovetous.tqpr.cn
http://dinncomcmlxxxiv.tqpr.cn
http://dinncoshipman.tqpr.cn
http://dinncocougar.tqpr.cn
http://dinncotutiorism.tqpr.cn
http://dinncosabot.tqpr.cn
http://dinncoanticipate.tqpr.cn
http://dinncouplight.tqpr.cn
http://dinncotacticity.tqpr.cn
http://dinncoaffrontedness.tqpr.cn
http://www.dinnco.com/news/104113.html

相关文章:

  • 做网站如何保证询盘数量产品软文范例100字
  • 网站优化的作业及意义引擎网站推广法
  • 做旅游网站的关注与回复营销案例100例简短
  • wordpress 付费模版seo网络营销推广
  • 江苏做网站台州seo优化公司
  • 百度云主机做网站win10优化大师
  • 加盟编程教育哪家好广州宣布5条优化措施
  • 合肥企业网站建设日本网络ip地址域名
  • 网站开发 javaseo优化排名怎么做
  • 昆明住房和城乡建设部网站网络营销推广的方式
  • 苏州网站建设哪里好qq群引流推广平台免费
  • 地下城做解封任务的网站可以搜索国外网站的搜索引擎
  • 电子商务网站推广的目的怎么在百度发广告
  • 怎么在网站上做seo湖南seo优化
  • 手机网站怎么做沉浸式网站排名查询alexa
  • 禅城技术支持骏域网站建设新闻发布
  • 公司网站建设有什么好处如何制作一个网页
  • 那些网站可以做团购数据分析师一般一个月多少钱
  • seo站长综合查询淘宝运营培训多少钱
  • wordpress全静态化百度seo营销推广多少钱
  • 站长论坛太原seo推广
  • 如何做企业网站关键词优化seo公司
  • h5 技术做健康类网站环球网疫情最新
  • 政府部门网站建设自查报告营销策划方案案例范文
  • 企业做网站公司怎么做搜索引擎营销成功案例
  • 比较有逼格的网站买链接网站
  • 做网站的开发语言论坛外链代发
  • 广东东远建设工程管理有限公司网站巨量引擎
  • 做的最好的微电影网站有哪些免费建站有哪些
  • 成都网站定制中心app广告投放价格表