private void renameDatasetVersionHelper(int accountId, int containerId, String datasetName,
      Dataset.VersionSchema versionSchema, String sourceVersion, String targetVersion)
      throws SQLException, AccountServiceException {
    long sourceVersionValue = getVersionBasedOnSchema(sourceVersion, versionSchema);
    long targetVersionValue = getVersionBasedOnSchema(targetVersion, versionSchema);
    String renameFromSourceVersion =
        getDatasetVersionRenameFromHelper(accountId, containerId, datasetName, sourceVersionValue);
    //sanity check for the renameFromSourceVersion
    if (renameFromSourceVersion != null) {
      getVersionBasedOnSchema(renameFromSourceVersion, versionSchema);
    }
    try {
      // Disable auto commits
      dataAccessor.setAutoCommit(false);
      PreparedStatement copyToNewDatasetVersionStatement =
          dataAccessor.getPreparedStatement(copyToNewDatasetVersionSql, true);
      //copy the source version to the new target version.
      //if source version has a rename field, set to the rename field
      String renameFromForTarget = renameFromSourceVersion != null? renameFromSourceVersion : sourceVersion;
      executeCopyToNewDatasetVersionStatement(copyToNewDatasetVersionStatement, accountId, containerId, datasetName,
          sourceVersionValue, targetVersionValue, renameFromForTarget);
      //mark the original dataset version state to renamed.
      PreparedStatement updateDatasetVersionStateAndDeletedTsStatement =
          dataAccessor.getPreparedStatement(updateDatasetVersionStateAndDeletedTsSql, true);
      executeUpdateDatasetVersionStateAndDeletedStatement(updateDatasetVersionStateAndDeletedTsStatement, accountId,
          containerId, datasetName, sourceVersionValue, DatasetVersionState.RENAMED);
      dataAccessor.commit();
    } catch (SQLException | AccountServiceException e) {
      dataAccessor.rollback();
      dataAccessor.onException(e, Write);
      if (e instanceof SQLIntegrityConstraintViolationException) {
        throw new AccountServiceException(e.getMessage(), AccountServiceErrorCode.ResourceConflict);
      }
      throw e;
    } finally {
      // Close the connection to ensure subsequent queries are made in a new transaction and return the latest data
      dataAccessor.closeActiveConnection();
    }
  }
Helper function to rename a dataset version from source version to target version.
@param accountId the id for the parent account.
@param containerId the id of the container.
@param datasetName the name of the dataset.
@param sourceVersion the source version to rename.
@param targetVersion the target version to rename.
@throws SQLException
@throws AccountServiceException
ambry-account/src/main/java/com/github/ambry/account/mysql/DatasetDao.java