java - 如何保证删除或更新成功而不需要额外请求?

标签 java spring microservices mybatis

在某个微服务的服务层中,我向另一个微服务发出 POST 请求,以从数据库中删除一些记录:

@Override
public IdsResponse removeSomeStuff(IdsRequest request) throws NotFoundException {
    try {
        POST(SomeStuffEndpoint.createUrl(this.baseUri, REMOVE_SOME_STUFF), request, 
               IdsResponse.class);
    } catch(NotFoundException e) {
        logger.error("...", e);
        throw new NotFoundException(String.format("...: %s", e.getMessage()), e);
    }
    return new IdsResponse();
}

例如,请求在另一个微服务的端点传入:

@Path("/some-stuff")
public interface SomeStuffEndpoint {

    @POST
    @Path("/remove")
    Response removeSomeStuff(@Context MessageContext context);

    ...
}

在此微服务的 DAO 层中,我从数据库中删除(或更新)记录:

public class SomeStuffDaoImpl implements SomeStuffDao {
    private static final Logger logger = LoggerFactory.getLogger(SomeStuffDaoImpl.class);

    @Autowired
    SomeStuffMapper someStuffMapper;

    @Override
    public void removeSomeStuff(List<Long> someIds, List<Long> someAnotherIds) {
        try {
            someStuffMapper.removeSomeStuff(someIds, someAnotherIds);
        } catch (InvocationTargetException | PSQLException | BadSqlGrammarException e) {
            logger.error("...");
        }
    }
    ...
}   

我使用MyBatis 3作为持久化框架。移除方法如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
                        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="some.company.appname.mservicename.model.mapper.SomeStuffMapper">

    <delete id="removeSomeStuff" parameterType="map">
        delete from some_stuff where some_id in
        <foreach collection="someIds" item="someId" 
                index="index" 
                open="(" separator="," close=")">
            #{someId}
        </foreach>

        and another_stuff in

        <foreach collection="someAnotherIds" item="someAnotherId" 
                 index="index" 
                 open="(" separator="," close=")">
            #{someAnotherId}
        </foreach>
    </delete>
... 

如何确保删除或更新成功并且不会提出额外的检查请求?

例如,请求包含不正确的标识符组合并且未找到记录(不会引发异常)?

最佳答案

您可以因更新/删除而返回一些信息。在最简单的情况下,它可以是受影响的记录数。例如,这样客户端就会知道没有记录被删除。

为此,请修改映射器中 removeSomeStuff 的签名以返回 int。 Mybatis 将返回受影响的记录数。这可以对任何修改操作完成。

在某些情况下,返回有关受影响记录的更多信息(例如已删除记录的 ID)会很有帮助。您尚未指定您正在使用什么 RDBMS。在 postgres 中,您可以将删除转换为:

<select id="removeSomeStuff" parameterType="map" resultType="long" flushCache="true">

    delete from some_stuff where some_id in
    <foreach collection="someIds" item="someId" 
            index="index" 
            open="(" separator="," close=")">
        #{someId}
    </foreach>

    and another_stuff in

    <foreach collection="someAnotherIds" item="someAnotherId" 
             index="index" 
             open="(" separator="," close=")">
        #{someAnotherId}
    </foreach>
    RETURNING id
</select>

关于java - 如何保证删除或更新成功而不需要额外请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52961514/

相关文章:

java - Linux 上的自定义 TG2480H USB

java - Spring-Boot:错误注入(inject)多个bean

python - K8s 中微服务利用率低的性能问题(也对开发和 DevOps 产生影响)

docker - 在 docker-compose 中通过 tcp 与 Nest.js 微服务对话

java - Apache Camel FTP 组件

java - 如何从二维数组(java)中打印出奇数索引元素?

java - HazelCast分布式二级缓存和更新失效

java - 在 Spring 4.1 中注入(inject)泛型列表

java - 如何使用 Spring Social 1.1.0 发送应用程序邀请

microservices - GraphQL:对来自不同数据源的嵌套实体进行过滤、排序和分页?