Spring管理的事务@Transactional批注 [英] Spring managed transactions @Transactional annotation

查看:122
本文介绍了Spring管理的事务@Transactional批注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要传播设置.

@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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆