gpt4 book ai didi

multithreading - Spring data Couchbase生成的查询方法不是线程安全的

转载 作者:行者123 更新时间:2023-12-03 01:29:08 24 4
gpt4 key购买 nike

当我扩展 CouchbaseRepository 接口(interface)以执行 findByField exp findByProvider --> 生成的方法不是线程安全的:

我的界面:

public interface WarehouseRepository extends CouchbaseRepository<WarehouseEntity, String> {

List<WarehouseEntity> findByProvider(String provider);

}

我创建了一个线程池来测试它:

int threads = 3;
ExecutorService service = Executors.newFixedThreadPool(threads);
Collection<Future<Boolean>> futures = new ArrayList<>(threads);
for (int thread = 0; thread < threads; ++thread) {
String provider ="P1";
if(thread == 0)
provider = "P1";
if (thread == 1)
provider = "P2";
if(thread == 2)
provider = "P3";
String expectedProvider = provider;
Callable<Boolean> booleanSupplier = () -> {
String foundProvider = warehouseService.findByProvider(expectedProvider).get(0).getProvider().toString();
System.out.println("expectedProvider = " + expectedProvider + " foundProvider = " + foundProvider);
return expectedProvider.equals(foundProvider);//should never get false
};

futures.add(service.submit(booleanSupplier));
}
Set<Boolean> foundProviders = new HashSet<>();
for (Future<Boolean> future: futures) {
foundProviders.add(future.get());
}
if (1 != foundProviders.size()) {
System.out.println("got bad provider response for my query");
}

这是我执行一次的结果:

2019/07/03 13:30:39.056 DEBUG Executing N1QL query: {\"args\":[\"P1\"],\"statement\":\"SELECT META(`oms`).id AS _ID, META(`oms`).cas AS _CAS, `oms`.* FROM `oms` WHERE (`provider` = $1) AND `_class` = \\"com.cmt.WarehouseEntity\\"\",\"scan_consistency\":\"statement_plus\"}
2019/07/03 13:30:39.058 DEBUG Executing N1QL query: {\"args\":[\"P3\"],\"statement\":\"SELECT META(`oms`).id AS _ID, META(`oms`).cas AS _CAS, `oms`.* FROM `oms` WHERE (`provider` = $1) AND `_class` = \\"com.cmt.WarehouseEntity\\"\",\"scan_consistency\":\"statement_plus\"}
2019/07/03 13:30:39.056 DEBUG Executing N1QL query: {\"args\":[\"P1\"],\"statement\":\"SELECT META(`oms`).id AS _ID, META(`oms`).cas AS _CAS, `oms`.* FROM `oms` WHERE (`provider` = $1) AND `_class` = \\"com.cmt.WarehouseEntity\\"\",\"scan_consistency\":\"statement_plus\"}
expectedProvider = P2 foundProvider = P1
expectedProvider = P3 foundProvider = P3
expectedProvider = P1 foundProvider = P1
got bad provider for my query

对于第一行的查询expectedProvider = P2foundProvider=P1,我们得到了仓库p1的结果,用于查找仓库p2的查询

但是当我将 @Query 添加到我的方法中时,它神奇地变得线程安全。

@Query("#{#n1ql.selectEntity} WHERE #{#n1ql.filter} AND $1 = provider")
List<WarehouseEntity> findByProvider(String provider);

这怎么可能?

谢谢

最佳答案

这个问题似乎是由 this change 引入的2018 年 11 月。

将 spring-data-couchbase 降级到 3.1.2.RELEASE。

关于multithreading - Spring data Couchbase生成的查询方法不是线程安全的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56877609/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com