当调用ElasticsearchClient的query方法时, static字段丢失或者改变, 这是为什么呀

Java API client version: 7.17.10
Java version: jdk-17.0.3.1
Elasticsearch Version: 7.17

我有个代码仓库可以复现这个问题: GitHub - xiaochunyong/elasticsearch-threadlocal-reference-changed

有一个类UserHolder, 里面有个ThreadLocal变量

public class UserHolder {

    public static ThreadLocal<String> holder = new ThreadLocal<>();

}

当在PostController中调用ElasticsearchClient.query方法时, 进入query方法前和进入方法后, currentThread不一样

@RestController
@RequestMapping("/api/post")
public class PostController {

    @Autowired
    private ElasticsearchClient client;

    public static Thread currentThread;

    public static String welcome = "hello1";

    @GetMapping
    public Object query() throws IOException {
        currentThread = Thread.currentThread();
        welcome = "hello2";

        SearchResponse<ObjectNode> search = client.search(s -> s
                        .index("post")
                        .query(b -> b
                                .matchAll(b2 -> b2)
                        ),
                ObjectNode.class
        );

        return search.hits().hits();
    }

}

I found the reason. I used spring-boot-devtools. The class loader used by the class under src is different from the class loader in the jar package.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.