实体使用了@Prev注解,uid具有唯一索引
@Data
public class Foo {
@Id
@Column
private long id;
@Name
@Column
@Prev(
els = {@EL("$me.uid()")}
)
private String uid;
public String uid() {
if (Strings.isBlank(uid)) {
return UUID.randomUUID().toString();
} else {
return uid;
}
}
}
并发调用insert
List<Foo> foos= new ArrayList<>();
for (int i = 0; i < 20; i++) {
Foo foo = new Foo();
foo .setUid(i + "-" + System.currentTimeMillis());
foos.add(foo);
}
final List<CompletableFuture<Void>> futures = new ArrayList<>();
for (int i = 0; i < 4; i++) {
final int finalI = i;
futures.add(
CompletableFuture.runAsync(
() -> {
final List<Foo> input = foos.subList(5 * finalI, 5* (finalI + 1));
for (final Foo foo: input) {
dao().insert(movie);
}
}));
}
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
.exceptionally(
e -> {
log.warn(">>>>>>>>>" + e.getMessage());
return null;
}).join();
因为之前uid是否手动赋值都会报错 Duplicate entry