Program: 49492
Project: 49492
Buggy source:
/** 
 * Remove the given entitlement and clean up.
 * @param entitlement entitlement to remove
 * @param regenModified should we look for modified entitlements that are affectedand regenerated. False if we're mass deleting all the entitlements for a consumer anyhow, true otherwise. Prevents a deadlock issue on mysql (at least).
 */
@Transactional void removeEntitlement(Entitlement entitlement,boolean regenModified){
  Consumer consumer=entitlement.getConsumer();
  Pool pool=entitlement.getPool();
  pool=poolCurator.lockAndLoad(pool);
  consumer.removeEntitlement(entitlement);
  Set<Pool> deletablePools=new HashSet<Pool>();
  for (  Pool p : poolCurator.listBySourceEntitlement(entitlement)) {
    Set<Entitlement> deletableEntitlements=new HashSet<Entitlement>();
    for (    Entitlement e : p.getEntitlements()) {
      deletableEntitlements.add(e);
    }
    for (    Entitlement de : deletableEntitlements) {
      this.revokeEntitlement(de);
    }
    deletablePools.add(p);
  }
  for (  Pool dp : deletablePools) {
    deletePool(dp);
  }
  pool.getEntitlements().remove(entitlement);
  poolCurator.merge(pool);
  entitlementCurator.delete(entitlement);
  Event event=eventFactory.entitlementDeleted(entitlement);
  pool.setConsumed(pool.getConsumed() - entitlement.getQuantity());
  if (consumer.getType().isManifest()) {
    pool.setExported(pool.getExported() - entitlement.getQuantity());
  }
  if (!"String_Node_Str".equals(pool.getAttributeValue("String_Node_Str")) && pool.hasProductAttribute("String_Node_Str")) {
    Pool stackedSubPool=poolCurator.getSubPoolForStackId(consumer,pool.getProductAttributeValue("String_Node_Str"));
    if (stackedSubPool != null) {
      boolean updated=updatePoolFromStack(stackedSubPool,consumer,pool.getProductAttributeValue("String_Node_Str"));
      if (updated) {
        poolCurator.merge(stackedSubPool);
      }
 else {
        deletePool(stackedSubPool);
      }
    }
  }
  PoolHelper poolHelper=new PoolHelper(this,productCache,entitlement);
  enforcer.postUnbind(consumer,poolHelper,entitlement);
  if (regenModified) {
    this.regenerateCertificatesOf(entitlementCurator.listModifying(entitlement),true);
  }
  ComplianceStatus compliance=complianceRules.getStatus(consumer,new Date());
  consumer.setEntitlementStatus(compliance.getStatus());
  consumerCurator.update(consumer);
  sink.sendEvent(event);
}

Fixed version (ground truth):
/** 
 * Remove the given entitlement and clean up.
 * @param entitlement entitlement to remove
 * @param regenModified should we look for modified entitlements that are affectedand regenerated. False if we're mass deleting all the entitlements for a consumer anyhow, true otherwise. Prevents a deadlock issue on mysql (at least).
 */
@Transactional void removeEntitlement(Entitlement entitlement,boolean regenModified){
  Consumer consumer=entitlement.getConsumer();
  Pool pool=entitlement.getPool();
  pool=poolCurator.lockAndLoad(pool);
  consumer.removeEntitlement(entitlement);
  Set<Pool> deletablePools=new HashSet<Pool>();
  for (  Pool p : poolCurator.listBySourceEntitlement(entitlement)) {
    Set<Entitlement> deletableEntitlements=new HashSet<Entitlement>();
    for (    Entitlement e : p.getEntitlements()) {
      deletableEntitlements.add(e);
    }
    for (    Entitlement de : deletableEntitlements) {
      this.revokeEntitlement(de);
    }
    deletablePools.add(p);
  }
  for (  Pool dp : deletablePools) {
    deletePool(dp);
  }
  pool.getEntitlements().remove(entitlement);
  poolCurator.merge(pool);
  entitlementCurator.delete(entitlement);
  Event event=eventFactory.entitlementDeleted(entitlement);
  pool.setConsumed(pool.getConsumed() - entitlement.getQuantity());
  if (consumer.getType().isManifest()) {
    pool.setExported(pool.getExported() - entitlement.getQuantity());
  }
  if (!"String_Node_Str".equals(pool.getAttributeValue("String_Node_Str")) && pool.hasProductAttribute("String_Node_Str")) {
    String stackId=pool.getProductAttributeValue("String_Node_Str");
    Pool stackedSubPool=poolCurator.getSubPoolForStackId(consumer,stackId);
    if (stackedSubPool != null) {
      List<Entitlement> stackedEnts=this.entitlementCurator.findByStackId(consumer,stackId);
      if (stackedEnts.isEmpty()) {
        deletePool(stackedSubPool);
      }
 else {
        updatePoolFromStackedEntitlements(stackedSubPool,consumer,stackId,stackedEnts);
        poolCurator.merge(stackedSubPool);
      }
    }
  }
  PoolHelper poolHelper=new PoolHelper(this,productCache,entitlement);
  enforcer.postUnbind(consumer,poolHelper,entitlement);
  if (regenModified) {
    this.regenerateCertificatesOf(entitlementCurator.listModifying(entitlement),true);
  }
  ComplianceStatus compliance=complianceRules.getStatus(consumer,new Date());
  consumer.setEntitlementStatus(compliance.getStatus());
  consumerCurator.update(consumer);
  sink.sendEvent(event);
}