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

今夕是何夕123 2018-03-26 04:27:07
报错: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
}
找了很久,没有找到有效的解决方案,求大神提供一个解决办法
...全文
262 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

50,550

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧