If we switch to using Spring's BatchSqlUpdate do we need to somehow disable autocommit for this call to gain the full benefit? I read lots of posts about this but they all give examples with you are building your own queries and using your connection directly like this and many more: does my batch really execute as expected if autocommit is set to true?
But I can't find anyone talking about when doing it the "spring way". The connection will default to autocommit=true and that's good for all of our other methods. But now we have just a couple of batch ones and I don't see a method to set autocommit to false for a specific method/class.
I'll give more details, but does anyone know if we should disable autocommit for this (like it sounds from other posts) and if so how?
The details are we have this in the applicationContext.xml:
PROPAGATION_SUPPORTS
PROPAGATION_SUPPORTS
PROPAGATION_SUPPORTS
PROPAGATION_REQUIRED
and the datasource comes from wildfly standalone.xml. This all has been working great. Now we are working on using batch mode. Our sql update methods are like this (somewhat simplified)
protected class UpdateUserUpdate extends SqlUpdate {
public UpdateUserUpdate(DataSource ds) {
setDataSource(ds);
setSql("update whatever.. set value = ?")
declareParameter(new SqlParameter(Types.VARCHAR));
compile();
}
public int myupdate(String s) {
Object[] params = new Object[] { s };
return update(params);
}
}
And if we simply change the parent class to BatchSqlUpdate, it does work fine:
protected class UpdateUserUpdateBatch extends BatchSqlUpdate {
...copy as before but now add...
setBatchSize(1000);
}
And we do see some improvement. But would we see more if we can somehow disable the autocommit or is that even possible?
Or would it be faster to just create a method, feed it our array of entries, get a new connection from the datasource, and use PreparedStatement, like:
con = dataSource.getConnection();
con.setAutoCommit(false);
PreparedStatement pstmt = con.prepareStatement(sql);
do my add batch/executebatch in a 1000 loop...
pstmt.executeBatch();
con.commit();
con.setAutoCommit(false);
That is not the spring way but is it faster because I turned of autocommit or can you turn it of on BatchSqlUpdate?
Thank you!
0 comments:
Post a Comment
Thanks