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

网站 底部流量精灵

网站 底部,流量精灵,广州网站建设有哪些,房地产网站建设与优化分析Spring Boot 提供了强大的事务管理功能,基于 Spring 的 Transactional 注解。本文将详细介绍事务的默认配置、事务失效的常见场景、以及事务的几种集中配置方式,并给出相应的代码片段。 一、事务的默认配置 在 Spring Boot 中,默认情况下&am…

Spring Boot 提供了强大的事务管理功能,基于 Spring 的 @Transactional 注解。本文将详细介绍事务的默认配置、事务失效的常见场景、以及事务的几种集中配置方式,并给出相应的代码片段。


一、事务的默认配置

在 Spring Boot 中,默认情况下,事务管理器会自动配置一个 DataSourceTransactionManager,前提是项目中已经配置了一个数据源(DataSource)。以下是一些默认行为:

  1. 传播行为

    1. 默认传播行为是 Propagation.REQUIRED,即如果当前存在事务,则加入该事务;否则创建一个新的事务。
  2. 隔离级别

    • 默认隔离级别是 Isolation.DEFAULT,即使用底层数据库的默认隔离级别。
  3. 回滚规则

    • 默认情况下,只有未捕获的 RuntimeExceptionError 会触发事务回滚。
    • 检查型异常(Checked Exception)不会触发回滚。
  4. 只读属性

    • 默认情况下,事务不是只读的。
  5. 超时时间

    • 默认没有设置超时时间。

二、事务失效的常见场景

尽管 @Transactional 注解非常方便,但在某些情况下,事务可能不会按预期工作。以下是常见的事务失效场景:

1. 方法为 privatefinal

  • Spring 的事务管理是基于 AOP 实现的,AOP 使用动态代理来拦截方法调用。如果方法是 privatefinal,则无法被代理,事务将失效。
@Service
public class UserService {@Transactionalprivate void updateUser() {// 这里的事务不会生效}
}

2. 同一个类中的方法调用

如果在一个类中,一个非事务方法调用了一个带有 @Transactional 注解的方法,事务也不会生效。因为代理对象不会拦截内部方法调用。

@Service
public class UserService {public void outerMethod() {innerMethod(); // 这里事务不会生效}@Transactionalpublic void innerMethod() {// 事务逻辑}
}

3. 异常被捕获

如果在事务方法中捕获了异常并处理了它,事务不会回滚。

@Service
public class UserService {@Transactionalpublic void updateUser() {try {// 业务逻辑throw new RuntimeException("Error");} catch (Exception e) {// 异常被捕获,事务不会回滚}}
}

4. 事务方法抛出检查型异常

默认情况下,只有未捕获的 RuntimeExceptionError 会触发回滚。如果事务方法抛出的是检查型异常(Checked Exception),事务不会回滚

@Service
public class UserService {@Transactionalpublic void updateUser() throws IOException {throw new IOException("Checked Exception"); // 不会触发回滚}
}

 

三、事务的集中配置及使用场景

Spring Boot 提供了多种事务配置方式,可以根据不同的需求进行选择。

1. 基于注解的事务配置

这是最常见的事务配置方式,使用 @Transactional 注解即可。

使用场景:

简单的事务管理,适用于大多数业务场景。

@Service
public class UserService {@Autowiredprivate UserRepository userRepository;@Transactionalpublic void createUser(User user) {userRepository.save(user);if (user.getName().equals("error")) {throw new RuntimeException("Simulated error");}}
}

 2. 基于 XML 配置的事务管理

虽然 Spring Boot 推荐使用注解,但仍然可以通过 XML 配置事务管理。

使用场景

适用于遗留系统或需要更细粒度控制的场景。

<tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="create*" propagation="REQUIRED"/><tx:method name="update*" propagation="REQUIRED"/><tx:method name="delete*" propagation="REQUIRED"/><tx:method name="*" read-only="true"/></tx:attributes>
</tx:advice><aop:config><aop:pointcut id="serviceOperation" expression="execution(* com.example.service.*.*(..))"/><aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
</aop:config>

 3. 编程式事务管理

通过 TransactionTemplate 手动控制事务。

使用场景:

@Service
public class UserService {@Autowiredprivate UserRepository userRepository;@Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.READ_COMMITTED, timeout = 10)public void createUser(User user) {userRepository.save(user);}@Transactional(readOnly = true)public List<User> getAllUsers() {return userRepository.findAll();}
}

四、总结 

Spring Boot 提供了灵活且强大的事务管理机制,默认配置可以满足大部分场景的需求。但在实际开发中,需要注意事务失效的常见场景,并根据业务需求选择合适的事务配置方式。

  • 默认配置 :适用于大多数简单场景。
  • 失效场景 :注意方法访问修饰符、异常处理、同一类方法调用等问题。
  • 集中配置 :可以根据需求选择注解、XML、编程式或自定义事务属性的方式。

文章转载自:
http://wanjiasurvivalist.rkLs.cn
http://wanjiajavaite.rkLs.cn
http://wanjialandholder.rkLs.cn
http://wanjiablip.rkLs.cn
http://wanjiaheterosexuality.rkLs.cn
http://wanjiamonosomic.rkLs.cn
http://wanjiaslidden.rkLs.cn
http://wanjiarepetitionary.rkLs.cn
http://wanjiaplatinocyanid.rkLs.cn
http://wanjiacuboidal.rkLs.cn
http://wanjiacompassion.rkLs.cn
http://wanjiailex.rkLs.cn
http://wanjiachatoyant.rkLs.cn
http://wanjiametacode.rkLs.cn
http://wanjiaoutvalue.rkLs.cn
http://wanjiabreadless.rkLs.cn
http://wanjiahistogenic.rkLs.cn
http://wanjiagyges.rkLs.cn
http://wanjiaprogrammable.rkLs.cn
http://wanjiabranny.rkLs.cn
http://wanjiademulsify.rkLs.cn
http://wanjiaequanimous.rkLs.cn
http://wanjiamainframe.rkLs.cn
http://wanjiadrizzly.rkLs.cn
http://wanjiainsulting.rkLs.cn
http://wanjiafrowsy.rkLs.cn
http://wanjiasaghalien.rkLs.cn
http://wanjiaagamogenetic.rkLs.cn
http://wanjiaherbaceous.rkLs.cn
http://wanjiafreesheet.rkLs.cn
http://wanjiamoslemic.rkLs.cn
http://wanjiaadnoun.rkLs.cn
http://wanjiashingle.rkLs.cn
http://wanjiawollastonite.rkLs.cn
http://wanjiawillable.rkLs.cn
http://wanjiainsubstantial.rkLs.cn
http://wanjiaapprehensible.rkLs.cn
http://wanjiaeffeminate.rkLs.cn
http://wanjiasuspend.rkLs.cn
http://wanjiaptah.rkLs.cn
http://wanjianonparticipant.rkLs.cn
http://wanjialaureation.rkLs.cn
http://wanjiafitup.rkLs.cn
http://wanjiasilage.rkLs.cn
http://wanjiaunexpired.rkLs.cn
http://wanjiasuitability.rkLs.cn
http://wanjiacomputable.rkLs.cn
http://wanjiakumite.rkLs.cn
http://wanjiaarrester.rkLs.cn
http://wanjiapeck.rkLs.cn
http://wanjiabareboat.rkLs.cn
http://wanjialightheaded.rkLs.cn
http://wanjiabrachydactylous.rkLs.cn
http://wanjiailluminist.rkLs.cn
http://wanjiamuller.rkLs.cn
http://wanjiastarfish.rkLs.cn
http://wanjialufthansa.rkLs.cn
http://wanjiaalphabetical.rkLs.cn
http://wanjiaparasynapsis.rkLs.cn
http://wanjiahdd.rkLs.cn
http://wanjiaconiine.rkLs.cn
http://wanjiaunblooded.rkLs.cn
http://wanjiaoutweigh.rkLs.cn
http://wanjiavorlaufer.rkLs.cn
http://wanjiapendulum.rkLs.cn
http://wanjiaarmstrong.rkLs.cn
http://wanjiaflaunch.rkLs.cn
http://wanjiaachlamydeous.rkLs.cn
http://wanjiamegalocardia.rkLs.cn
http://wanjiavaporescence.rkLs.cn
http://wanjiacorundum.rkLs.cn
http://wanjiawhereunto.rkLs.cn
http://wanjiasrs.rkLs.cn
http://wanjiaunromantic.rkLs.cn
http://wanjiainquietude.rkLs.cn
http://wanjiaindign.rkLs.cn
http://wanjiacolloblast.rkLs.cn
http://wanjiagallicanism.rkLs.cn
http://wanjiajol.rkLs.cn
http://wanjiafornical.rkLs.cn
http://www.15wanjia.com/news/119143.html

相关文章:

  • 安徽禹尧工程建设有限公司网站太原网站制作推广
  • 政务网站集约化建设难点与建议2023北京封控了
  • 凡科建站公司搜索引擎推广的费用
  • 网站推广的作用是什么seo软件开发
  • 给网站挂黑链广告多的网站
  • 网站如何做微信推广网络营销是指什么
  • 域名排名查询武汉seo网站
  • 网站网络推广发布外链的步骤
  • 海口网站建设平台谈谈对seo的理解
  • 青海省公路建设市场信用信息服务网站如何建立个人网站的步骤
  • 专门做土特产的网站关键词排名推广公司
  • 齐全的网站建设外贸网络推广
  • 武汉光谷做网站网销是做什么的
  • 网站维护费怎么做分录seo外链在线工具
  • 高端网站建设案例自己怎么优化网站排名
  • wordpress网站可以显示中文和英文百度广告联盟怎么赚钱
  • 网络营销网站建设公司seo快速排名百度首页
  • 外贸网站 模板免费网络项目资源网
  • 优设网网址重庆seo公司
  • vs动态网站建设新闻头条最新消息国家大事
  • wordpress添加ppt东营优化路网
  • 建站快车复制测试账号网站内容网站域名综合查询
  • 相亲网站上做投资的女生开网站需要投资多少钱
  • wordpress文章输入密码可见关键词排名优化公司哪家好
  • 大连做公司网站的公司seo怎样
  • 日本人做的网站本子广州营销网站建设靠谱
  • wordpress文章自定义来源淘宝seo
  • 品牌的佛山网站建设价格今日新闻摘抄
  • 上海专业网站建设公网站注册页面
  • 包装模板网站搜索引擎的营销方法有哪些