0

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;
        }

    }
Vish
  • 280
  • 2
  • 18

1 Answers1

0

Hey I found the Solution, Here is what I did,

  private static final String UPDATE_CLIENT_OWNER = 
            "UPDATE clientowner SET "
          + "clientOwnerName=?,"
          + "clientOwnerPhone=?,"
          + "clientOwnerEmail=?,"
          + "lastUpdatedOn=NOW() "
          + "WHERE clientOwnerId=?";
 @Transactional
    public void updateClientOwner(int clientId, List<ClientOwnerVO> clientOwnerVoList) throws Exception {
        logger.info("Updating client Owner(s)");
        try{
           int[] count = jdbc.batchUpdate(UPDATE_CLIENT_OWNER, new BatchPreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement ps, int i) throws SQLException {

                    ClientOwnerVO clientOwnerVO = clientOwnerVoList.get(i);
                    ps.setString(1, clientOwnerVO.getClientOwnerName());
                    ps.setString(2, VariableEncryption.encrypt(clientOwnerVO.getClientOwnerPhone(), clientOwnerVO.getCreatedOn()));
                    ps.setString(3, VariableEncryption.encrypt(clientOwnerVO.getClientOwnerEmail(), clientOwnerVO.getCreatedOn()));
                    ps.setInt(4, clientOwnerVO.getClientOwnerID());

                }
                @Override
                public int getBatchSize() {
                    return clientOwnerVoList.size();
                }
            });
           logger.info("Exiting After successfully updating "+count.toString()+" client owners");


        }catch (Exception e) {
            logger.error("Error in updating client owners: " + e.getMessage(), e);
            throw e;
        }
Vish
  • 280
  • 2
  • 18