Spring管理的事务@Transactional批注 [英] Spring managed transactions @Transactional annotation
问题描述
需要传播设置.
@Transactional(propagation = Propagation.REQUIRED)
交易是可读写的.
在哪些情况下使用了那些?请给我举例说明
In which scenario those are used? Please give me explain with example
推荐答案
Spring交易默认值为
Spring transaction default is
@Transactional(propagation = Propagation.REQUIRED)
因此,您无需指定传播属性.
So you do not need to specify the propagation property.
那么,弹簧组件的@Transactional
注释是什么意思?
So, What does it mean by @Transactional
annotation for a spring component ?
-
Spring框架将启动一个新事务并执行所有方法,最后提交该事务.
Spring framework will start a new transaction and executes all the method and finally commit the transaction.
但是,如果application context
中不存在任何事务,那么spring容器将开始新的事务.
But If no transaction is exists in the application context
then spring container will start a new transaction.
那么,结果是什么?
结果是,如果任何嵌套事务失败,则整个事务将失败并回滚(不要在db中插入任何值),而不是提交.
So, What is the result ?
The result is if any nested transaction fail, then the whole transaction will fail and rolled back (do not insert any value in db) instead of commit.
示例:
@Service
public class ServiceA{
@Transactional(propagation = Propagation.REQUIRED)
public void foo(){
fooB();
}
@Transactional(propagation = Propagation.REQUIRED)
public void fooB(){
//some operation
}
}
说明:
在此示例中,foo()
方法分配了事务性行为,并且在foo()
内部分配了另一个也称为事务性的方法fooB()
.
根据foo()
,此处fooB()
充当嵌套事务.如果fooB()
由于任何原因失败,则foo()
也会提交失败.而是回滚.
Explanation :
In this example foo()
method assigned a transactional behavior and inside foo()
another method fooB()
called which is also transactional.
Here the fooB()
act as nested transaction in terms of foo()
. If fooB()
fails for any reason then foo()
also failed to commit. Rather it roll back.
这篇关于Spring管理的事务@Transactional批注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!