简体   繁体   English

Spring 数据 Redis。 JPA 存储库 findBy 有时无法获取现有记录

[英]Spring Data Redis. JPA Repository findBy sometimes fails to fetch existing record

I see some weird case.我看到了一些奇怪的案例。 Sometimes my findBy...() method returns null instead of some object inserted and fetched successfully before.有时我的 findBy...() 方法返回 null 而不是之前成功插入和获取的一些 object。 After that the needed object fetches fine.之后,所需的 object 就可以正常获取了。 In other words sometimes the search is not working.换句话说,有时搜索不起作用。

Spring Boot edition: 1.5.2.RELEASE Spring 引导版:1.5.2.RELEASE

spring-boot-starter-data-redis: 1.5.22.RELEASE spring-boot-starter-data-redis:1.5.22.RELEASE

"maxmemory-policy" setting is set to "noeviction" “maxmemory-policy”设置设置为“noeviction”

My obj declaration:我的 obj 声明:

@RedisHash("session")
public class Session implements Serializable {

    @Id
    private String id;

    @Indexed
    private Long internalChatId;
    @Indexed
    private boolean active;
    @Indexed
    private String chatId;
}

JPA Repository: JPA 存储库:

@Repository
public interface SessionRepository extends CrudRepository<Session, String> {

    Session findByInternalChatIdAndActive(Long internalChatId, Boolean isActive);
}

Redis config: Redis 配置:

@Bean
    public LettuceConnectionFactory redisConnectionFactory(
        RedisProperties redisProperties) {
        return new LettuceConnectionFactory(
            redisProperties.getRedisHost(),
            redisProperties.getRedisPort());
    }

    @Bean
    public RedisTemplate<?, ?> redisTemplate(LettuceConnectionFactory connectionFactory) {
        RedisTemplate<byte[], byte[]> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        return template;
    }

Thanx in advance for any assist.提前感谢任何帮助。

We have recently seen similar behavior.我们最近看到了类似的行为。 In our scenario, we can have multiple threads that read and write to the same repository.在我们的场景中,我们可以有多个线程读取和写入同一个存储库。 Our null return occurs when one thread is doing a save to an object while another is doing a findById for that same object.我们的 null 返回发生在一个线程正在保存到 object 而另一个线程正在为相同的 object 执行 findById 时。 The findById will occasionally fail. findById 偶尔会失败。 It appears that the save implementation does a delete followed by an add;看来 save 实现先删除,然后再添加; if the findById gets in during the delete, the null result is returned.如果 findById 在删除过程中进入,则返回 null 结果。

We've had good luck so far in our test programs that can reproduce the null return using a Java Semaphore to gate all access (read, write, delete) to the repository.到目前为止,我们的测试程序很幸运,它可以使用 Java 信号量重现 null 返回,以控制对存储库的所有访问(读取、写入、删除)。 When the repository access methods are all gated by the same semaphore, we have not seen a null return.当存储库访问方法都由同一个信号量控制时,我们没有看到 null 返回。 Our next step is to try adding the synchronized keyword to the methods in the class that access the repository (as an alternative to using the Semaphore).我们的下一步是尝试将 synchronized 关键字添加到 class 中访问存储库的方法(作为使用信号量的替代方法)。

This should not happen I don't what is reason.这不应该发生我不知道是什么原因。 But you can use Option class and if it returns null at least you can avoid exception.但是您可以使用选项 class 并且如果它返回 null 至少可以避免异常。 Something like:就像是:

    Optional<Session> findByInternalChatIdAndActive(Long internalChatId, Boolean isActive);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM