Program: 106907
Project: 106907
Buggy source:
@DB protected DomainRouterVO findOrCreateDhcpServer(Network guestNetwork,DeployDestination dest,Account owner) throws InsufficientCapacityException, ConcurrentOperationException {
  DataCenterDeployment plan=null;
  long dcId=dest.getDataCenter().getId();
  DataCenter dc=dest.getDataCenter();
  DomainRouterVO router=null;
  Long podId=dest.getPod().getId();
  if ((dc.getNetworkType() == NetworkType.Basic || guestNetwork.isSecurityGroupEnabled()) && guestNetwork.getTrafficType() == TrafficType.Guest) {
    router=_routerDao.findByNetworkAndPod(guestNetwork.getId(),podId);
    plan=new DataCenterDeployment(dcId,podId,null,null,null);
  }
 else {
    router=_routerDao.findByNetwork(guestNetwork.getId());
    plan=new DataCenterDeployment(dcId);
  }
  if (router != null) {
    return router;
  }
  long id=_routerDao.getNextInSequence(Long.class,"String_Node_Str");
  if (s_logger.isDebugEnabled()) {
    s_logger.debug("String_Node_Str" + id);
  }
  List<NetworkOfferingVO> offerings=_networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemControlNetwork);
  NetworkOfferingVO controlOffering=offerings.get(0);
  NetworkVO controlConfig=_networkMgr.setupNetwork(_systemAcct,controlOffering,plan,null,null,false,false).get(0);
  List<Pair<NetworkVO,NicProfile>> networks=new ArrayList<Pair<NetworkVO,NicProfile>>(3);
  NicProfile gatewayNic=new NicProfile();
  gatewayNic.setDefaultNic(true);
  networks.add(new Pair<NetworkVO,NicProfile>((NetworkVO)guestNetwork,gatewayNic));
  networks.add(new Pair<NetworkVO,NicProfile>(controlConfig,null));
  VMTemplateVO template=_templateDao.findRoutingTemplate(dest.getCluster().getHypervisorType());
  Transaction txn=Transaction.currentTxn();
  txn.start();
  Network network=_networkDao.lockRow(guestNetwork.getId(),true);
  if (network == null) {
    throw new ConcurrentOperationException("String_Node_Str" + guestNetwork.getId());
  }
  if ((dc.getNetworkType() == NetworkType.Basic || guestNetwork.isSecurityGroupEnabled()) && guestNetwork.getTrafficType() == TrafficType.Guest) {
    router=_routerDao.findByNetworkAndPod(guestNetwork.getId(),podId);
    plan=new DataCenterDeployment(dcId,podId,null,null,null);
  }
 else {
    router=_routerDao.findByNetwork(guestNetwork.getId());
    plan=new DataCenterDeployment(dcId);
  }
  if (router != null) {
    return router;
  }
  router=new DomainRouterVO(id,_offering.getId(),VirtualMachineName.getRouterName(id,_instance),template.getId(),template.getHypervisorType(),template.getGuestOSId(),owner.getDomainId(),owner.getId(),guestNetwork.getId(),_offering.getOfferHA());
  router.setRole(Role.DHCP_USERDATA);
  router=_itMgr.allocate(router,template,_offering,networks,plan,null,owner);
  UserStatisticsVO stats=_userStatsDao.findBy(owner.getId(),dcId,router.getNetworkId(),null,router.getId(),router.getType().toString());
  if (stats == null) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + owner.getId() + "String_Node_Str"+ router.getId());
    }
    stats=new UserStatisticsVO(owner.getId(),dcId,null,router.getId(),router.getType().toString(),guestNetwork.getId());
    _userStatsDao.persist(stats);
  }
  txn.commit();
  return router;
}

Fixed version (ground truth):
@DB protected DomainRouterVO findOrCreateDhcpServer(Network guestNetwork,DeployDestination dest,Account owner) throws InsufficientCapacityException, ConcurrentOperationException {
  DataCenterDeployment plan=null;
  long dcId=dest.getDataCenter().getId();
  DataCenter dc=dest.getDataCenter();
  DomainRouterVO router=null;
  Long podId=dest.getPod().getId();
  if ((dc.getNetworkType() == NetworkType.Basic || guestNetwork.isSecurityGroupEnabled()) && guestNetwork.getTrafficType() == TrafficType.Guest) {
    router=_routerDao.findByNetworkAndPod(guestNetwork.getId(),podId);
    plan=new DataCenterDeployment(dcId,podId,null,null,null);
  }
 else {
    router=_routerDao.findByNetwork(guestNetwork.getId());
    plan=new DataCenterDeployment(dcId);
  }
  if (router != null) {
    return router;
  }
  long id=_routerDao.getNextInSequence(Long.class,"String_Node_Str");
  if (s_logger.isDebugEnabled()) {
    s_logger.debug("String_Node_Str" + id);
  }
  List<NetworkOfferingVO> offerings=_networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemControlNetwork);
  NetworkOfferingVO controlOffering=offerings.get(0);
  NetworkVO controlConfig=_networkMgr.setupNetwork(_systemAcct,controlOffering,plan,null,null,false,false).get(0);
  List<Pair<NetworkVO,NicProfile>> networks=new ArrayList<Pair<NetworkVO,NicProfile>>(3);
  NicProfile gatewayNic=new NicProfile();
  gatewayNic.setDefaultNic(true);
  networks.add(new Pair<NetworkVO,NicProfile>((NetworkVO)guestNetwork,gatewayNic));
  networks.add(new Pair<NetworkVO,NicProfile>(controlConfig,null));
  VMTemplateVO template=_templateDao.findRoutingTemplate(dest.getCluster().getHypervisorType());
  Transaction txn=Transaction.currentTxn();
  Network network=_networkDao.acquireInLockTable(guestNetwork.getId());
  if (network == null) {
    throw new ConcurrentOperationException("String_Node_Str" + guestNetwork.getId());
  }
  try {
    txn.start();
    if ((dc.getNetworkType() == NetworkType.Basic || guestNetwork.isSecurityGroupEnabled()) && guestNetwork.getTrafficType() == TrafficType.Guest) {
      router=_routerDao.findByNetworkAndPod(guestNetwork.getId(),podId);
      plan=new DataCenterDeployment(dcId,podId,null,null,null);
    }
 else {
      router=_routerDao.findByNetwork(guestNetwork.getId());
      plan=new DataCenterDeployment(dcId);
    }
    if (router != null) {
      return router;
    }
    router=new DomainRouterVO(id,_offering.getId(),VirtualMachineName.getRouterName(id,_instance),template.getId(),template.getHypervisorType(),template.getGuestOSId(),owner.getDomainId(),owner.getId(),guestNetwork.getId(),_offering.getOfferHA());
    router.setRole(Role.DHCP_USERDATA);
    router=_itMgr.allocate(router,template,_offering,networks,plan,null,owner);
    UserStatisticsVO stats=_userStatsDao.findBy(owner.getId(),dcId,router.getNetworkId(),null,router.getId(),router.getType().toString());
    if (stats == null) {
      if (s_logger.isDebugEnabled()) {
        s_logger.debug("String_Node_Str" + owner.getId() + "String_Node_Str"+ router.getId());
      }
      stats=new UserStatisticsVO(owner.getId(),dcId,null,router.getId(),router.getType().toString(),guestNetwork.getId());
      _userStatsDao.persist(stats);
    }
    txn.commit();
  }
  finally {
    if (network != null) {
      _networkDao.releaseFromLockTable(network.getId());
    }
  }
  return router;
}