java - SpringBoot JPA 存储库 saveAll 在 MongoDB 中引发写入冲突
问题描述
我有一个类似于下面给出的 Spring Boot 应用程序代码,它尝试在 MongoDB 中保存实体列表,但我收到此错误:
Exception in thread "Thread-20" org.springframework.data.mongodb.UncategorizedMongoDbException: Command failed with error 112 (WriteConflict): 'WriteConflict error: this operation conflicted with another operation. Please retry your operation or multi-document transaction.' on server 127.X.X.X:XXXXX. The full response is {"errorLabels": ["TransientTransactionError"]
Caused by: com.mongodb.MongoCommandException: Command failed with error 112 (WriteConflict): 'WriteConflict error: this operation conflicted with another operation. Please retry your operation or multi-document transaction.' on server 127.X.X.X:XXXXX. The full response is {"errorLabels": ["TransientTransactionError"]
那么,我应该怎么做才能避免这个错误/异常?
代码:
@Transactional
public synchronized void myMethod(List<MyEntity> myEntities) {
saveEntities(myEntities);
}
public void saveEntities(List<MyEntity> myEntities) {
myRepository.saveAll(myEntities);
}
解决方案
推荐阅读
- ios - Xamarin iOS 返回主按钮未显示在详细视图中
- lua - lua中的大写和小写字符
- windows - 可以解析文件夹位置以查找 BATCH 中某些字符的实例吗?
- c++ - boost directory_iterator 是否在 Windows 上按字母顺序访问文件和文件夹
- python - 如何将列表中的字符串转换为列表,从 [[list1],[list2]....[listm]]
- angularjs - 如何在 AngularJS 页面中捕获 nodejs api 数据?
- drupal - 使用来自参考字段的动态参数过滤实体参考视图
- android - 将屏幕截图保存到内部存储并同步到图库?
- angular - Angular 工作区交互
- sql - SQL 列出具有前任的客户经理