aspose file tools*
The moose likes Spring and the fly likes Can we set autocommit to false in spring for jdbctemplate.batchUpdate()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Can we set autocommit to false in spring for jdbctemplate.batchUpdate()?" Watch "Can we set autocommit to false in spring for jdbctemplate.batchUpdate()?" New topic
Author

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

kiran nyala
Ranch Hand

Joined: Sep 21, 2009
Posts: 47

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

Joined: Mar 07, 2010
Posts: 1680
    
    7

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())

[How To Ask Questions][Read before you PM me]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we set autocommit to false in spring for jdbctemplate.batchUpdate()?