spring-boot - 如何使 Spring boot post api 幂等

标签 spring-boot post locking idempotent sql-server-2019

我在我的 Spring boot 应用程序中使用 Spring Data JPA 创建了简单的 CRUD api。我在 Controller 中的 Post 方法如下所示:-

@RequestMapping(value = "/article", method = RequestMethod.POST, produces = "application/json")
public Article createArticle(@RequestBody Article article) {
    return service.createArticle(article);
}

服务方式如下:-

@Override
public Article createArticle(Article articleModel) {
    return repository.save(articleModel);
}

我的 JsonPayload 如下所示:

{
   "article_nm":"A1",
   "article_identifier":"unique identifier"
}

现在我想让我的 POST 请求成为幂等的,这样即使我再次获得具有相同 article_identifier 的 json 负载,它也不会在数据库中创建记录。

我无法在数据库中进行任何方案/约束更改,article_identifier 字段也不是表中的主键。

我知道首先我可以检查数据库并返回已保存的记录作为响应,如果它已经存在但是如果多个请求(原始请求和重复请求)同时出现,两者都会检查数据库并且找不到任何记录使用该标识符并将创建 2 条记录(每个记录一条)。此外,由于它是一个分布式应用程序,我如何保持跨多个数据库事务的一致性。

我如何使用某种锁定机制,以便永远不会有 2 条具有相同 article_identifier 的记录。有人可以建议一些引用如何在 Spring boot 中实现它吗?

最佳答案

在这种情况下需要幂等性来解决回发(或双发请求)。最简单的方法就是在服务级别检查是否存在具有给定信息的帖子(正如您所指出的)。您可以为此使用 repository.exists() 变体。

I understand that first I can check in database and return the saved record in response if it already exists

至于

if multiple request (original and duplicate request) comes at same time, both will check in database and would not find any record with that identifier and will create 2 record (one for each)

如果它是单个数据库,则需要将事务彼此隔离(我知道你说过不是,但我正在尝试解释我的推理,所以请耐心等待)。对于那个 spring 有以下注释:@Transactional(isolation = Isolation.SERIALIZABLE)。虽然在这种情况下 @Transactional(isolation = Isolation.REPEATABLE_READ) 就足够了。

Also as it's a distributed application how can i maintain the consistency across multiple database transactions.

它是如何分布的?您首先需要考虑数据库。它是主从 mysql/postgress/mongodb 吗?它是一些奇怪的全局分布式系统吗?假设是传统的主从设置,那么写事务将由master处理(据我所知,属于该事务的所有selects也会在那里)所以应该没有问题。然而,只有提供更多细节才能真正给出答案。

关于spring-boot - 如何使 Spring boot post api 幂等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63001566/

相关文章:

spring-boot - 为什么 thymeleaf 消息显示不正确?

javascript - Tiny Box POST 表单序列化为数组会导致错误

java - Spring MVC 中无法返回 HTML View

java - 在 REST 服务中验证 token ST Apereo CAS(无状态)

jquery - 从本地 Web 服务器而非本地文件系统运行时,AJAX 发布时出错

java - 为什么非阻塞并发优于阻塞并发

mysql - 在执行 SELECT 后跟 INSERT 时是否需要锁定 MySQL 表?

oracle - Oracle:更新语句不等待/不失败

Angular/Spring Boot/Azure Ingress - URL 重写

php - curl 跟随 POST 位置