Grails多线程下插入(修改)数据的问题

今夕是何夕123 今夕是何夕123 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
报错:Row was updated or deleted by another transaction

controller里面多线程这块代码
List<Integer> list = db.rows(communitySql).asList()
ExecutorService evaluatePool = Executors.newFixedThreadPool(10)
List<Future<List<Object>>> futures = new ArrayList<>(10);

for (int i = 0;i<10;i++){
try {
futures.add(evaluatePool.submit(new ComputeThreadService(list,this.computeDistanceService)))
}
catch(Exception e) {
e.printStackTrace()
}
}

try {
//处理
List<Object> result = new ArrayList<>();
int i = 0
for(Future<List<Object>> future : futures){
//合并操作
result.addAll(future.get())
}
println "result.size()=========="+result.size()
//批量插入
result.each{
it.save()
i++
if(i % 1000 == 0){
sessionFactory.currentSession.flush()
sessionFactory.currentSession.clear()
}
}
} catch (Exception e) {
e.printStackTrace()
}
ComputeThreadService中只调用这个:
@Override
List<Object> call() throws Exception {

def obj = null
List<Object> retList = new ArrayList<Object>();
for(int i=0;i<list.size();i++){
obj = list.get(i)?.community_id
//处理逻辑
def distance = computeDistanceService.communityDistance(obj.toString())
retList.addAll(distance)
}
//返回处理结果
return retList;
}
service中只有这一处会连接数据库插入:Community community = null
if(Community.findByCommunityId(communityId) == null){
community = new Community()
}else{
community = Community.findByCommunityId(communityId)
}

community.communityId = communityResult[0]?.community_id
community.name = communityResult[0]?.communityname
if(City.findByCityId(communityResult[0]?.city_id))
{
community.city = City.findByCityId(communityResult[0]?.city_id)
}
else
{
def city = new City()
city.cityId = communityResult[0]?.city_id
city.cityCode = communityResult[0]?.city_code
city.save()
community.city = city
}
if(community.validate())
{
community.save(flush:true)
}
else
{
println community.errors
}
找了很久,没有找到有效的解决方案,求大神提供一个解决办法
...全文

相关推荐

评论
收藏 已收藏
分享
返回顶部
今夕是何夕123
Java 51,407 成员 86,130 内容