Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can we set autocommit to false in spring for jdbctemplate.batchUpdate()?

 
kiran nyala
Ranch Hand
Posts: 52
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

According to conventional JDBC we can set autocommit to false before executeBatch(). When this method returns an int array we will cross check the length of array to identify the number of records affected then we will commit the transaction. But JdbcTemplate.batchUpdate(String sql, BatchPreparedStatementSetter pss) will insert the records and commits automatically then returning int array. Can I cross check the records updated with this before committing?

Here is my jdbctemplate configuration
spring.xml



Update.java
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My first suggestion is to set the default auto commit false on the data source itself. If you are using Spring to manage your transactions (say using the @Trasactional annotation) most likely it is being set false for you anyway. If that is not the case you will need to do it via the connection.

Code like this generally smells since you should be able to let Spring manage that for you, and I cant think of a good reason to have autocommit on at all, but if you really need access to the connection you can use the DataSourceUtils class.

DataSourceUtils.getConnection(jdbcTemplate.getDataSource())
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic