简体   繁体   English

如何在@Transactional中处理提交和回滚?

[英]How to handle commit and rollback in @Transactional?

Can somebody tell me how to handle commit and rollback using Spring's @Transactional annotation? 有人可以告诉我如何使用Spring的@Transactional注释处理提交和回滚吗? are they automatically handled by spring or are they required to be handled manually? 它们是由弹簧自动处理还是需要手动处理?

The whole point of having the @Transactional annotation is to have the container (in this case Spring) handle all the transaction related stuff automatically. 具有@Transactional批注的全部目的是使容器(在本例中为Spring)自动处理所有与事务相关的东西。 What this means is that if all goes well and no exceptions are thrown the container will commit the transaction upon exit from the @Transactional code, or the transaction will be rolled back if an exception occurs. 这意味着如果一切顺利,并且没有引发任何异常,则容器将在退出@Transactional代码后提交事务,或者如果发生异常,则事务将回滚。

In case you where wondering, @Transaction in Spring is implemented using AOP, because it is a crosscutting concern. 如果您想知道,Spring中的@Transaction是使用AOP实现的,因为这是一个横切关注点。 You can read the documentation for all the details 您可以阅读文档以获取所有详细信息

@Transactional annotation will reduce the burden for the developer.As transaction code is a cross cutting concern spring itself provides us an annotation internally developed using aop concepts if we declare this annotation around a method. @Transactional注释将减轻开发人员的负担。由于事务代码是一个跨领域的关注点,如果我们在方法周围声明该注释,spring本身会为我们提供一个使用aop概念内部开发的注释。 Spring will take care of transaction,if any exception raises it will issue a rollback on transaction manager otherwise commit on transaction manager Spring将处理事务,如果引发任何异常,它将在事务管理器上发出回滚,否则在事务管理器上提交

If you want to learn some more internals of spring transactons go through this doc spring transactions 如果您想了解更多有关Spring Transacton的内部信息,请阅读本文档Spring Transactions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM