当我扩展CouchbaseRepository接口以执行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 = P2 foundProvider = P1 ,我们获得了仓库p1的结果以进行查询以查找仓库p2
但是当我将@Query添加到我的方法中时,通过魔术线程安全就变得可以了。
@Query("#{#n1ql.selectEntity} WHERE #{#n1ql.filter} AND $1 = provider")
List<WarehouseEntity> findByProvider(String provider);
怎么可能?
谢谢