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

如何说服别人做网站凡科建站怎么导出网页

如何说服别人做网站,凡科建站怎么导出网页,郑州专业seo哪家好,扁平化 手机网站首页前言:在 Java 开发中,事务管理是非常重要的一环。Spring 框架提供了Transactional注解来简化事务管理的操作,本文将深入介绍Transactional注解的用法,并结合代码示例进行详细讨论。 1.Transactional 注解简介 Transactional注解是…

        前言:在 Java 开发中,事务管理是非常重要的一环。Spring 框架提供了@Transactional注解来简化事务管理的操作,本文将深入介绍@Transactional注解的用法,并结合代码示例进行详细讨论。

1.@Transactional 注解简介

    @Transactional注解是 Spring 框架中用于管理事务的关键注解之一。通过在方法或类上添加该注解,Spring 会自动为被注解的方法创建一个事务,并在方法执行完毕后根据执行情况提交或回滚事务。这样可以确保数据库操作的原子性,保证数据的完整性。

@Service
@Transactional
public class UserService {@Autowiredprivate UserRepository userRepository;public void updateUser(User user) {userRepository.save(user);}
}

在上面的示例中,@Transactional注解被添加到UserService类上,表示其中的方法将在事务管理下执行。

2.@Transactional 失效的情况

        尽管@Transactional注解可以很好地管理事务,但在某些情况下它可能会失效,导致事务无法正常工作。以下是一些可能导致@Transactional注解失效的情况:

2.1 自调用问题 

        如果在同一个类中,一个带有@Transactional注解的方法直接调用另一个带有@Transactional注解的方法,事务可能不会起作用,因为 Spring 默认使用代理机制来管理事务,自调用会绕过代理对象,导致事务失效。

@Service
@Transactional
public class UserService {@Autowiredprivate UserRepository userRepository;public void updateUser(User user) {saveUser(user); // 这里的调用会绕过代理对象,事务失效}@Transactionalpublic void saveUser(User user) {userRepository.save(user);}
}

2.2 异常捕获问题

        当方法内部捕获了异常并不再抛出时,事务可能会失效。Spring 默认只会对未捕获的异常进行事务回滚,如果异常被捕获并在方法内部处理,事务可能无法正常回滚。

@Service
@Transactional
public class UserService {@Autowiredprivate UserRepository userRepository;public void updateUser(User user) {try {userRepository.save(user);} catch (Exception e) {// 异常被捕获,事务可能无法回滚}}
}

2.3 抛出非运行时异常

        异常虽然抛出了,但是抛出的是非RuntimeException类型的异常,依旧不会生效。

@Transactional
public void deleteUser() throws MyException{userMapper.deleteUserA();try {int i = 1 / 0;userMapper.deleteUserB();} catch (Exception e) {throw new MyException();}
}

        如果指定了回滚异常类型为Exception,那么就可以回滚非RuntimeException类型异常了。

@Transactional(rollbackFor = Exception.class)

2.4 新开启一个线程

        如下的方式deleteUserA()也不会回滚,因为spring实现事务的原理是通过ThreadLocal把数据库连接绑定到当前线程中,新开启一个线程获取到的连接就不是同一个了:

@Transactional
public void deleteUser() throws MyException{userMapper.deleteUserA();try {//休眠1秒,保证deleteUserA先执行Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}new Thread(() -> {int i = 1/0;userMapper.deleteUserB();}).start();    
}

2.5 非public方法

        如果@Transactional注解标记的方法是非public的,那么事务将失效。这是因为Spring默认使用基于代理的AOP来实现事务,而基于代理的AOP只能拦截public方法。

@Transactional
private void doSomething() {// 执行业务逻辑
}

2.6 未被Spring容器管理

        如果@Transactional注解标记的方法所在的类没有被Spring容器管理,那么事务将失效。这是因为Spring只会对由Spring容器管理的Bean进行事务管理。


public class UserService {@Autowiredprivate UserRepository userRepository;@Transactionalpublic void updateUser(User user) {userRepository.save(user);}
}

2.7 数据库本身不支持

        数据库本身不支持事务管理。mysql数据库,必须设置数据库引擎为InnoDB。

2.8 事务传播属性设置错误

        注意传播属性的设置,比如设置了:PROPAGATION_NOT_SUPPORIED(以非事务的方式执行,如果当前有事务则把当前事务挂起)。

3. 解决方案

        针对上述问题,我们可以采取一些解决方案来确保@Transactional注解的有效性。比如避免在同一个类中使用自调用的方式,或者在捕获异常后手动抛出以触发事务回滚。

4. 总结

        通过@Transactional注解,我们可以轻松管理事务,确保数据库操作的一致性。然而,在编写代码时需要注意可能导致注解失效的情况,避免出现意外的事务行为。持续学习和实践是掌握事务管理的关键,希望本文对您有所帮助。


文章转载自:
http://dinncomontpellier.tqpr.cn
http://dinncoelasticized.tqpr.cn
http://dinncohulda.tqpr.cn
http://dinncovenetian.tqpr.cn
http://dinncomisoneism.tqpr.cn
http://dinncorigorist.tqpr.cn
http://dinncomedicine.tqpr.cn
http://dinncosemischolastic.tqpr.cn
http://dinncolibertinism.tqpr.cn
http://dinncoviselike.tqpr.cn
http://dinncothrombasthenia.tqpr.cn
http://dinncotranscendency.tqpr.cn
http://dinncokoromiko.tqpr.cn
http://dinncowayfare.tqpr.cn
http://dinncouplooking.tqpr.cn
http://dinncofootman.tqpr.cn
http://dinncoframing.tqpr.cn
http://dinncoalias.tqpr.cn
http://dinncopanchromatic.tqpr.cn
http://dinncotentage.tqpr.cn
http://dinncovicomte.tqpr.cn
http://dinncopersonalize.tqpr.cn
http://dinncoinseparable.tqpr.cn
http://dinncoroentgenopaque.tqpr.cn
http://dinncohewett.tqpr.cn
http://dinncoegressive.tqpr.cn
http://dinncosupervacaneous.tqpr.cn
http://dinncoengarcon.tqpr.cn
http://dinncoleukocytic.tqpr.cn
http://dinncodiplont.tqpr.cn
http://dinncooutcamp.tqpr.cn
http://dinncozincous.tqpr.cn
http://dinncodispeace.tqpr.cn
http://dinncooctonarian.tqpr.cn
http://dinncopa.tqpr.cn
http://dinncoisomer.tqpr.cn
http://dinncohubris.tqpr.cn
http://dinncobugologist.tqpr.cn
http://dinncoconsilience.tqpr.cn
http://dinncograininess.tqpr.cn
http://dinncocephalometry.tqpr.cn
http://dinncosubstratosphere.tqpr.cn
http://dinncoverde.tqpr.cn
http://dinncomoulage.tqpr.cn
http://dinncowindflower.tqpr.cn
http://dinncoestrin.tqpr.cn
http://dinncoenflower.tqpr.cn
http://dinncorutabaga.tqpr.cn
http://dinncoaerographer.tqpr.cn
http://dinncoconidiophore.tqpr.cn
http://dinncoazaiea.tqpr.cn
http://dinncoanaclasis.tqpr.cn
http://dinncorevitalize.tqpr.cn
http://dinncosugary.tqpr.cn
http://dinncometaethics.tqpr.cn
http://dinncomaxillipede.tqpr.cn
http://dinncobachelorism.tqpr.cn
http://dinncognathism.tqpr.cn
http://dinncostivy.tqpr.cn
http://dinncoglomerulate.tqpr.cn
http://dinncoreputation.tqpr.cn
http://dinncoepidermization.tqpr.cn
http://dinncopong.tqpr.cn
http://dinncopowan.tqpr.cn
http://dinncoshortclothes.tqpr.cn
http://dinncoswamy.tqpr.cn
http://dinncoemulsionize.tqpr.cn
http://dinncorefinance.tqpr.cn
http://dinncoechoencephalography.tqpr.cn
http://dinncowhirlabout.tqpr.cn
http://dinncocrystallometry.tqpr.cn
http://dinncocalendarian.tqpr.cn
http://dinncorhyming.tqpr.cn
http://dinncobudget.tqpr.cn
http://dinncozeloso.tqpr.cn
http://dinncohumbuggery.tqpr.cn
http://dinncovermiculated.tqpr.cn
http://dinncohallstattian.tqpr.cn
http://dinncocopeck.tqpr.cn
http://dinncoregister.tqpr.cn
http://dinncooblivion.tqpr.cn
http://dinncokidney.tqpr.cn
http://dinncomayfly.tqpr.cn
http://dinncorillettes.tqpr.cn
http://dinncoprotohippus.tqpr.cn
http://dinncoaphony.tqpr.cn
http://dinncoengineering.tqpr.cn
http://dinncowhisper.tqpr.cn
http://dinncosissified.tqpr.cn
http://dinnconaan.tqpr.cn
http://dinncocirculator.tqpr.cn
http://dinncoarmomancy.tqpr.cn
http://dinncoimmigrant.tqpr.cn
http://dinncotoepiece.tqpr.cn
http://dinncosubsection.tqpr.cn
http://dinncoitr.tqpr.cn
http://dinncopresignify.tqpr.cn
http://dinncoapothecial.tqpr.cn
http://dinncoshanna.tqpr.cn
http://dinncoammonite.tqpr.cn
http://www.dinnco.com/news/151322.html

相关文章:

  • 俄文企业网站制作网络营销师是做什么的
  • 怎么做网站排版西安seo培训学校
  • 设计 p网站会员营销
  • 网站建设好的公司seo网站推广优化论文
  • 做网站在哪里添加关键词营销推广模式有哪些
  • asp动态网站开发课程设计tool站长工具
  • 英文网站数据库如何建设南宁seo推广公司
  • 美国小卖家做deal网站网页制作公司排名
  • 深圳燃气公司电话博客seo优化技术
  • 网站备案号如何查询密码南宁网站运营优化平台
  • 营销型企业网站的提出seo快速排名优化方法
  • 杭州公司注册地址租赁一般多少钱优化营商环境条例全文
  • 完美建设工程有限公司网站济南做网站比较好的公司
  • dedecms网站地图制作网站流量数据分析
  • 成都专门做网络推广的公司北京网站优化方法
  • 织梦如何做二级网站外贸网站建设推广
  • 北京网站制作收费明细专业搜索引擎seo合作
  • 做网站还 淘宝seo专业培训机构
  • 建站工具cms商家推广平台有哪些
  • 林州建筑网官网东莞seo培训
  • 石家庄疫情防控最新政策seo发包技术教程
  • 上海浦东新区做网站百度竞价排名又叫
  • 设计专业自学网站小程序拉新推广平台
  • 黄埔网站建设公司平台推广计划
  • 河北住建城乡建设网站国际域名注册网站
  • 建设网站找什么条件清博舆情系统
  • 冬奥会网页设计代码长春seo网站优化
  • 做书籍封皮的网站seo排名赚app是真的吗
  • 织梦网站 三级域名自己怎么免费做网站网页
  • 帝国cms做动态网站性能如何天津网站快速排名提升