如何正确使用AddBatch / withBatch将XML标记值批量插入到数据库表中 [英] How to use AddBatch/withBatch properly for bulk inserting xml tag value to database table

查看:395
本文介绍了如何正确使用AddBatch / withBatch将XML标记值批量插入到数据库表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

'innerXml'是一个xml文件,包含大量的xml标签。我试图获取标签值并将它们转储到数据库表中。
我已经尝试了下面的代码,它工作正常。

  innerXml.Row.each {Row  - > 
$ b $ sql.execute(INSERT INTO tab1(col1,col2)VALUES($ {Row.Column0.text()},$ {Row.Column1.text()}))

但是,由于存在大量xml标签,导致性能问题逐一插入记录。正如专家建议我尝试使用withBatch来提高性能。请找到下面的代码,我尝试与批次:

  sql.withBatch (386,插入到tab1(col1,col2)值(?,?)){ps-> innerXml.Row.each {Row  - > ps.addBatch($ {Row.Column0.text()},$ {Row.Column1.text()})}} 

但是我得到的错误是:

  groovy。 lang.MissingMethodException:没有方法签名:Script41。$()适用于参数类型:(Script41 $ _run_closure2_closure3_closure4)values:[Script41 $ _run_closure2_closure3_closure4 @ 23724e8d]可能的解决方案:is(java.lang.Object),run(), 38行$($ b $ / code> 

)上的run(),any(),use([Ljava.lang.Object;),任何(groovy.lang.Closure)

请帮忙

在此先感谢!!

解决方案

您需要做的:

  // 386是一个奇数批量大小? 
sql.withBatch(386,'insert into tab1(col1,col2)values(?,?)'){ps - >
innerXml.Row.each {row - >
ps.addBatch(row.Column0.text(),row.Column1.text())
}
}


'innerXml' is a xml file with huge number of xml tags . I am trying fetch the tag values and dump them into database table . I have tried below code and it is working fine .

innerXml.Row.each { Row ->

sql.execute("INSERT INTO tab1(col1,col2) VALUES (${Row.Column0.text()},${Row.Column1.text()} )")    

But as there is huge number of xml tags its causing performance issue to insert record one by one . As the experts suggested I tried using withBatch to improve performance.Please find the below code which I tried with withBatch :

sql.withBatch(386, """  insert into tab1(col1,col2) values (?, ?) """) { ps ->   innerXml.Row.each { Row ->ps.addBatch(${Row.Column0.text()} ,${Row.Column1.text()})  }}

But I am getting below error :

groovy.lang.MissingMethodException: No signature of method: Script41.$() is applicable for argument types: (Script41$_run_closure2_closure3_closure4) values: [Script41$_run_closure2_closure3_closure4@23724e8d] Possible solutions: is(java.lang.Object), run(), run(), any(), use([Ljava.lang.Object;), any(groovy.lang.Closure) error at line: 38

Please help

Thanks in Advance!!

解决方案

You need to do:

// 386 is an odd batch size?
sql.withBatch(386, 'insert into tab1(col1,col2) values (?, ?)') { ps ->
    innerXml.Row.each { row ->
        ps.addBatch(row.Column0.text(), row.Column1.text())
    }
}

这篇关于如何正确使用AddBatch / withBatch将XML标记值批量插入到数据库表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆