I have an issue that suppose I have say 100 records initially, and I shown them on UI as a list of users. Now I have given the provision to deactivate number users by clicking "deactivate" button which is placed against every single record, I then capture all the "deactivated" users in a list and send it to the DAO layer.[the logic of deactivating user is just to set 'isDeleted' flag to true, i.e.soft delete So it is as good as I am updating multiple records whose Ids I have Placed into the list],There is a simple solution that, write a for loop->iterate through the list-> and for each record fire a query to update isDeleted flag to true, but its not feasible solution if I have say 5000 records to be deleted at once. I have heard and implemented the batchUpdate concept for "Inserting" multiple records at once, but I dont understand how can I use batch Update to update several records at only one DB call, Please help, The batch update code for insertion is as follows,
private static final String INSERT_USER_PERMISSION =
"INSERT INTO permission_transaction(permissionId,userId,isDeleted) "
+ "VALUES(?,?,?)";
@Transactional
public void addPermission(final UserVO userVo, final List<PermissionVO> permissionVoList)
throws Exception {
logger.info("Adding user permission, for userId: "+userVo.getUserId());
try {
jdbc.batchUpdate(INSERT_USER_PERMISSION, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
PermissionVO permissionVo = permissionVoList.get(i);
ps.setInt(1, permissionVo.getPermissionId());
ps.setInt(2, userVo.getUserId());
ps.setBoolean(3, false);
}
@Override
public int getBatchSize() {
return permissionVoList.size();
}
});
logger.info("Exiting addPermission, for userId: "+userVo.getUserId());
}catch (Exception e) {
logger.error("Error in adding user permission: " + e.getMessage(), e);
throw e;
}
}