How to use AddBatch/withBatch properly for bulk inserting xml tag value to database table

杀马特。学长 韩版系。学妹 提交于 2019-12-11 01:14:00

问题


'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!!


回答1:


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())
    }
}


来源:https://stackoverflow.com/questions/43364341/how-to-use-addbatch-withbatch-properly-for-bulk-inserting-xml-tag-value-to-datab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!