JDBC prepared statement creates one extra database hit without any params

♀尐吖头ヾ 提交于 2019-12-11 04:58:46

问题


I have one annoying issue and i can not figure out why it is happening. To give you short introduction, i implemented batch processing (batch insert) using MySql and JDBC template on my Spring Boot project.

So basically batch insert is working like it should, and performance is really amazing, BUT there is this one small issue that really is annoying and it causes constraint exception when i am inserting in one table that has unique key on two columns (id, value).

So i have this code:

private String INSERT_SQL_PARAMS = "INSERT INTO item_params(p_key, p_value, item_id) values (?,?,?)"

override fun saveParams(configParams: Set<ItemParam>) {
    jdbcTemplate!!.update { connection ->
        connection.autoCommit = false
        val ps: PreparedStatement = connection.prepareStatement(INSERT_SQL_PARAMS)

        configParams.forEachIndexed { index, it ->
            ps.setLong(1, it.configurationId)
            ps.setString(2, it.pKey)
            ps.setString(3, it.pValue)
            ps.addBatch()

            if (index != 0 && index % 1000 == 0) {
                ps.executeBatch()
                connection.commit()
            }
        }

        ps.executeBatch()
        connection.commit()
        ps
    }
}

And when i am looking at the logs using datasource proxy in spring boot, i can see actual queries that are executed.

So when i want to insert for example 2 items at once i see this in log:

Batch: True, INSERT INTO item_param(item_id, p_key, p_value) values (1, 1, 1), (2, 2, 2)
Batch: False, INSERT INTO item_param(item_id, p_key, p_value) values ()

So as you can see i always at the end see that one extra/spare statement without any values, and from some reason log says Batch = False.

Can anyone see something that i am missing in my code, why is this happening and what i can do with this ?

Also i have one more question, for example if i have 1000 records, i will execute batch 2 times, one in if statement and one at the end. Is there some way where i can say ps.executeBatch only if there are params in query ?


回答1:


Because jdbcTemplate.update executes your SQL for you, so it automatically calls execute at the end of your code.

Since it calls execute exactly once, it doesn't fit your purposes. Try this tutorial that uses jdbcTemplate.batchUpdate. It implements the multiple calls to jdbcConnection for you.



来源:https://stackoverflow.com/questions/59153072/jdbc-prepared-statement-creates-one-extra-database-hit-without-any-params

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