• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 398
  • Last Modified:


I was reading folowing lines and I did not understand clearly.
return jdbcTemplate.batchUpdate(sql, setter);
To Execute batch
 JDBC driver supports batching, the updates will be batched, creating
more efficient database access. If not, Spring will simulate batching, but the statements
will be executed individually.

Why we used jdbcTemplate. what is difference between batch execute and executing a statement.
Any ideas, resources,sample code,links, highly appreciated. thanks in advance.
2 Solutions

Well, some database servers operate differently when
you ask them separately to update 20 records of the same type
(say 20  rows in one table) one by one, or if you tell them upfront - this
is the way I want to modfy my records - these are 20 values to find these
records, these are 20 values to update these particular field of
these records , and now-  go update all 20 of them. In some smart databases, when you say
upfront - the database will first design the best way to update
and then use this way 20 times just replacing paramters, whereas if you communicate it
 to database  one by one - it
will 20 times spend time designing the best way to update it.
So if database makes such distinction in operations between
single updates and bulk updates it may save some time for bulk updates.

This  statement emphasizes the point that although jdbcTemplate in its
template (which we mentioned in another question) has this idea that if we want to do 20 updates
we first should communicate to database our intention and
the arrays of data, rather than feed them one by one.
So while jdbcTemplate knows that it should work in this mode(template),
the actual implementation - in fact the driver for the
particulr database - may either take advantage of such option
or not. So jdbcTemplate will know that it will
want to request such option, but if it is not avialbale, driver
will still do the update one by one. That again
illustrates difference between interface and implementation - independent
of databses and driver 20 records will be updated, howvere
actuual mechnism, which will be used, will depend on the
implementation in the driver.

Batching improves performance by allowing the updates to be 'precompiled' by the driver allowing just the parameters to be sent for each update.

Important to understand that batching still sends multiple updates, it just minimises the amount of processing required for each update. And you still need to add all the updates one at a time.

Also the queries sent to the database are exactly the same whether you are using a batch update or not, it will still recieve a single update for each one you send. ie. batching has nothing to do with the database server.

To achieve this the statement for all the updates needs to be the same, only the parameters can differ

you can read about jdbc batch updates here

What JdbcTemplate offers is its own inbuilt support for batching which will get used if batching is not supported. It s better than just doing updates, but no as fast as 'native' batching.


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now