问题
'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