java @Transactional 有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17564653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:20:08  来源:igfitidea点击:

What does @Transactional do?

javaspringtransactionsspring-jdbc

提问by osh

I know this is probably a duplicate and, ironically, before I started reading here and there about it, I thoughtI knew what it was for (needless to saybut I'll still say it, please correct me where I am wrong):

我知道这可能是重复的,具有讽刺意味的是,在我开始到处阅读之前,我以为我知道它的用途(不用说,但我仍然会说,请纠正我的错误):

It relieves the programmer of having to use transaction.begin()and commit().
If you have a method that calls two DAO methods which normally would each have a transaction.beginand transaction.commitencompassing the real operations and call them it would result in two transactions ( and there might be rollback issues if the previous DAO method was supposed to be rolled-back too).

它使程序员不必使用transaction.begin()commit()
如果您有一个方法调用两个 DAO 方法,通常每个方法都有一个transaction.begintransaction.commit包含实际操作并调用它们将导致两个事务(如果先前的 DAO 方法也应该回滚,则可能会出现回滚问题)。

But if you use @transactionalon your method then all those DAO calls will be wrapped in a single begin()- commit()cycle. Of course, in case you use @Transactionalthe DAOs must not use the begin()and commit()methods I think.

但是,如果你用@transactional你的方法,那么所有这些DAO通话将被包装在一个单一的begin()-commit()周期。当然,如果你使用@TransactionalDAOs 一定不要使用我认为的begin()commit()方法。

回答by chubock

You can handle your Transactionsin two ways: Programmaticallyand Declarative.

您可以Transactions通过两种方式处理您的:ProgrammaticallyDeclarative.

When you're using transaction.beginand transaction.commitand ..., you are hanling your Transactionsprogrammatically. This way you have more control on your Transactionboundaries but you will end up with lots of similar codes (Cross Cutting Concerns) scattered all over your project.

当您使用transaction.beginandtransaction.commit和 ... 时,您正在以Transactions编程方式处理您的操作。通过这种方式,您可以更好地控制自己的Transaction边界,但最终会Cross Cutting Concerns在您的项目中散布许多类似的代码 ( )。

But in Declarativeway, the codes that handle the Transactionsare separated from your businesses logic and will not be scattered all over your project. It's one of the main concepts of Aspect Oriented Programming.

但在Declarative某种程度上,处理这些的代码Transactions与您的业务逻辑分离,不会分散在您的项目中。它是 的主要概念之一Aspect Oriented Programming

回答by Xstian

I suggest you this link that explain everything on Spring Transaction.

我建议您使用这个链接来解释 Spring Transaction 的所有内容。

http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/transaction.html

http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/transaction.html

You should see also the same attribute about Transactional (propagation, rollbackFor, etc), transaction behavior could change if you use those attributes.

您还应该看到关于事务的相同属性(传播、回滚等),如果您使用这些属性,事务行为可能会改变。