Skip to content

Cached queries reuse the EvaluationContext [DATAKV-137] #116

Closed
@spring-projects-issues

Description

@spring-projects-issues

Shijun Kong opened DATAKV-137 and commented

The following test will fail at the last assertion, with NPE.
I believe it is a bug in CachingKeyValuePartTreeQuery#cachedQuery.

After, the first findByName statement, the cachedQuery.criteria.context.variables contains User("Jack"), which later was deleted.

However, in the context, it is never been properly cleanup. So, the next findByName statement will yield NPE. Because "it" reference -> "Jack"

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(AppConfig.class)
public class UserRepoTest {

    @Autowired
    private UserRepo repo;

    @org.junit.Test
    public void testFindByName() throws Exception {
        repo.save(new User("Jack"));
        repo.save(new User("Mary"));

        User jack = repo.findByName("Jack");
        assertThat(jack.getName(), is("Jack"));

        repo.delete(jack);

        Iterable<User> all = repo.findAll();
        assertThat(StreamSupport.stream(all.spliterator(), false).count(), is(1L));
        assertThat(all.iterator().next().getName(), is("Mary"));

        User mary = repo.findByName("Mary");
        assertThat(mary.getName(), is("Mary"));
    }

}

user is a simple pojo

@KeySpace("user")
public class User {

    @Id
    private String uuid;

    private String name;

    public User(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }
}

@Configuration
@EnableMapRepositories("repositories")
public class AppConfig { }

public interface UserRepo extends CrudRepository<User, String> {

    User findByName(String name);
}

Affects: 1.1.1 (Hopper SR1)

Referenced from: pull request #20

Backported to: 1.1.2 (Hopper SR2)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions