Grails多线程下插入(修改)数据的问题
报错: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
}
找了很久,没有找到有效的解决方案,求大神提供一个解决办法

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
}
找了很久,没有找到有效的解决方案,求大神提供一个解决办法
...全文
相关推荐
grails ckeditor 0.4修改支持flv插入
**标题解析:**"grails ckeditor 0.4修改支持flv插入" 这个标题揭示了我们正在处理一个Grails应用,其中整合了CKEditor 0.4版本,并且已经进行了定制以支持FLV(Flash
解决grails服务端口冲突的办法(grails修改端口号)
解决这个问题的方法是修改Grails应用的配置,指定一个新的未被使用的端口。这一过程主要涉及对`BuildConfig.groovy`文件的编辑。
grails学习遇到的问题
`update`值意味着当数据库结构与领域类不匹配时,Grails会更新数据库结构以匹配领域模型。6.
grails-export:grails 出口
插件系统**Grails 的核心优势之一就是其强大的插件系统,允许开发者扩展框架功能,而无需修改核心代码。Grails 出口插件就是这样一个例子,它为 Grails 应用程序提供了数据导出的能力。
grails技术应用问题总汇
"Grails技术应用问题综述"在这个关于Grails技术的问题总汇中,我们关注的是开发者在实际项目开发中遇到的问题和解决方案。Grails是一个开源的Web应用程序框架,它基于Groovy语言和


