• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 57
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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())
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic