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
What does @Transactional do?
提问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.begin
and transaction.commit
encompassing 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.begin
并transaction.commit
包含实际操作并调用它们将导致两个事务(如果先前的 DAO 方法也应该回滚,则可能会出现回滚问题)。
But if you use @transactional
on your method then all those DAO calls will be wrapped in a single begin()
- commit()
cycle. Of course, in case you use @Transactional
the DAOs must not use the begin()
and commit()
methods I think.
但是,如果你用@transactional
你的方法,那么所有这些DAO通话将被包装在一个单一的begin()
-commit()
周期。当然,如果你使用@Transactional
DAOs 一定不要使用我认为的begin()
和commit()
方法。
回答by chubock
You can handle your Transactions
in two ways: Programmatically
and Declarative
.
您可以Transactions
通过两种方式处理您的:Programmatically
和Declarative
.
When you're using transaction.begin
and transaction.commit
and ..., you are hanling your Transactions
programmatically. This way you have more control on your Transaction
boundaries but you will end up with lots of similar codes (Cross Cutting Concerns
) scattered all over your project.
当您使用transaction.begin
andtransaction.commit
和 ... 时,您正在以Transactions
编程方式处理您的操作。通过这种方式,您可以更好地控制自己的Transaction
边界,但最终会Cross Cutting Concerns
在您的项目中散布许多类似的代码 ( )。
But in Declarative
way, the codes that handle the Transactions
are 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.
您还应该看到关于事务的相同属性(传播、回滚等),如果您使用这些属性,事务行为可能会改变。