springboot 事务管理

news/2024/7/7 19:35:11

很多操作数据库的工具可以保证事务性,这里不讲。
这里讲的是,业务逻辑相关的东西,举例:你更新表A中的字段status,同时希望改变表B中的status字段,如果中间更新失败,则都不更新。

首先在应用类上添加注解@EnableTransactionManagement,开启springboot事务管理功能

@SpringBootApplication
@EnableTransactionManagement
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}

然后在接口上添加@Transactional注解

demo如下:

    @Transactional(rollbackFor = Exception.class)
    public ResponseBean setStatusById(FlowVo flowVo) {
        String source = flowVo.getSource();
        Integer status = flowVo.getStatus();
        String id = flowVo.getId();
        if (source == null || id == null || status == null){
            return ResponseBean.builder().code(-1).data("param error").build();
        }
        Query query = new Query(Criteria.where("id").is(flowVo.getId()));
        Update update = new Update();
        update.set("status", status);
        // 更新A表中的status
        boolean ret = mongoUtils.update(query, update, FlowEntity.class, getCollection(source));

        Query query1 = new Query(Criteria.where("flowId").is(flowVo.getId()));
        // 更新B表中的status
        mongoUtils.update(query1, update, FlowRelationEntity.class, getFlowRelationCollection(source));
        return ResponseBean.builder().code(100).data(ret).build();
    }

详细使用参考:https://www.cnblogs.com/xd502djj/p/10940627.html


http://www.niftyadmin.cn/n/3656136.html

相关文章

最早的Tangram预览

最早的Tangram预览几天前与好友在msn上聊天,朋友给我一个惊喜,他提供给我2001年我发布的Tangram相关的信息,这些已经迷失的东西,看起来十分亲切,原始地址是http://www.vchelp.net:9090/dtool/submit/vdd_pa.htm&#x…

Tangram商业版本预览(二):Tabbed MDI 界面

Tangram商业版本预览(二):Tabbed MDI 界面 Tangram商业版本的目标是允许用户不用写程序代码,直接用XML描述直接得到商业质量的、高度可定制化的软件框架,事实上,软件框架部分代码基本与软件的功能是不相关的,如同高档写字楼一样&…

mongodb 聚合 按月分组统计

需求 先说下我的需求:查询用户每个月发布文章的天数,并以列表的形式返回。 举例:某用户在2020年12月份12号、8号、15号、22号曾经发布过文章,把所有发布文章的日期在数组中返回。 实现 以下是使用mongodb聚合实现命令。 [{$ma…

Tangram商业版本预览(一)

Tangram商业版本预览(一)Tangram商业版本计划于最近交付,其主要特点是包含其他版本的全部功能之外,提供一个高质量的UI定制能力,内置的Skin引擎支持多达2000多种流行的Skin,同时可以提供Microsoft Office系…

can‘t convert from BSON type long to Date

{$project: {year: {$year: "$createTime"},_id: 1,createTime: 1} }以上语句在查询mongodb时报错:cant convert from BSON type long to Date,截图如下: 原因:数据库存储的createTime为long类型,而project语句需要一个date类型&a…

Windows应用程序与配置文件

Windows应用程序与配置文件为每个应用程序提供一个配置文件已经不是什么新鲜的想法了,在早期的Windows中,许多程序通常都提供一个扩展名为“.ini”或“.dat”之类的物件以定制化程序或为应用程序提供扩展描述。Microsoft公布.NET框架后,基于W…

springboot 多个数据库配置

文章目录一、pom文件依赖二、yaml文件配置三、代码实现1、main函数配置2、创建mongodb工厂3、主数据源配置4、副数据源配置5、工具封装6、使用四、踩的坑1、MongoClientURI找不到2、找不到mongoTemplate一、pom文件依赖 <dependency><groupId>org.springframework…