cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dar...@apache.org
Subject [09/20] New Transaction API
Date Wed, 16 Oct 2013 16:50:20 GMT
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f62e28c1/server/src/com/cloud/network/NetworkServiceImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/NetworkServiceImpl.java b/server/src/com/cloud/network/NetworkServiceImpl.java
index 61c070a..e0e90d9 100755
--- a/server/src/com/cloud/network/NetworkServiceImpl.java
+++ b/server/src/com/cloud/network/NetworkServiceImpl.java
@@ -41,7 +41,6 @@ import javax.inject.Inject;
 import javax.naming.ConfigurationException;
 
 import org.apache.log4j.Logger;
-
 import org.apache.cloudstack.acl.ControlledEntity.ACLType;
 import org.apache.cloudstack.acl.SecurityChecker.AccessType;
 import org.apache.cloudstack.api.command.admin.network.DedicateGuestVlanRangeCmd;
@@ -159,9 +158,15 @@ import com.cloud.utils.db.Filter;
 import com.cloud.utils.db.JoinBuilder;
 import com.cloud.utils.db.SearchBuilder;
 import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.db.TransactionCallback;
+import com.cloud.utils.db.TransactionCallbackNoReturn;
+import com.cloud.utils.db.TransactionCallbackWithException;
+import com.cloud.utils.db.TransactionLegacy;
+import com.cloud.utils.db.TransactionStatus;
 import com.cloud.utils.db.SearchCriteria.Op;
 import com.cloud.utils.db.Transaction;
 import com.cloud.utils.exception.CloudRuntimeException;
+import com.cloud.utils.exception.ExceptionUtil;
 import com.cloud.utils.net.NetUtils;
 import com.cloud.vm.Nic;
 import com.cloud.vm.NicSecondaryIp;
@@ -635,11 +640,8 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
 
 
     @Override
-    public NicSecondaryIp allocateSecondaryGuestIP (Account ipOwner, long zoneId, Long nicId, Long networkId, String requestedIp) throws InsufficientAddressCapacityException {
+    public NicSecondaryIp allocateSecondaryGuestIP (Account ipOwner, long zoneId, final Long nicId, final Long networkId, String requestedIp) throws InsufficientAddressCapacityException {
 
-        Long accountId = null;
-        Long domainId = null;
-        Long vmId = null;
         String ipaddr = null;
 
         if (networkId == null) {
@@ -649,7 +651,7 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
         Account caller = CallContext.current().getCallingAccount();
 
         //check whether the nic belongs to user vm.
-        NicVO nicVO = _nicDao.findById(nicId);
+        final NicVO nicVO = _nicDao.findById(nicId);
         if (nicVO == null) {
             throw new InvalidParameterValueException("There is no nic for the " + nicId);
         }
@@ -670,8 +672,8 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
         if (network == null) {
             throw new InvalidParameterValueException("Invalid network id is given");
         }
-        accountId = ipOwner.getAccountId();
-        domainId = ipOwner.getDomainId();
+        final Long accountId = ipOwner.getAccountId();
+        final Long domainId = ipOwner.getDomainId();
 
         // Validate network offering
         NetworkOfferingVO ntwkOff = _networkOfferingDao.findById(network.getNetworkOfferingId());
@@ -716,26 +718,29 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
             return null;
         }
 
-        NicSecondaryIpVO secondaryIpVO;
         if (ipaddr != null) {
             // we got the ip addr so up the nics table and secodary ip
-            Transaction txn = Transaction.currentTxn();
-            txn.start();
-
-            boolean nicSecondaryIpSet = nicVO.getSecondaryIp();
-            if (!nicSecondaryIpSet) {
-                nicVO.setSecondaryIp(true);
-                // commit when previously set ??
-                s_logger.debug("Setting nics table ...");
-                _nicDao.update(nicId, nicVO);
-            }
-
-            s_logger.debug("Setting nic_secondary_ip table ...");
-            vmId = nicVO.getInstanceId();
-            secondaryIpVO = new NicSecondaryIpVO(nicId, ipaddr, vmId, accountId, domainId, networkId);
-            _nicSecondaryIpDao.persist(secondaryIpVO);
-            txn.commit();
-           return  getNicSecondaryIp(secondaryIpVO.getId());
+            final String addrFinal = ipaddr;
+            long id = Transaction.execute(new TransactionCallback<Long>() {
+                @Override
+                public Long doInTransaction(TransactionStatus status) {
+                    boolean nicSecondaryIpSet = nicVO.getSecondaryIp();
+                    if (!nicSecondaryIpSet) {
+                        nicVO.setSecondaryIp(true);
+                        // commit when previously set ??
+                        s_logger.debug("Setting nics table ...");
+                        _nicDao.update(nicId, nicVO);
+                    }
+        
+                    s_logger.debug("Setting nic_secondary_ip table ...");
+                    Long vmId = nicVO.getInstanceId();
+                    NicSecondaryIpVO secondaryIpVO = new NicSecondaryIpVO(nicId, addrFinal, vmId, accountId, domainId, networkId);
+                    _nicSecondaryIpDao.persist(secondaryIpVO);
+                    return secondaryIpVO.getId();
+                }
+            });
+
+           return getNicSecondaryIp(id);
         } else {
             return null;
         }
@@ -805,13 +810,15 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
                 throw new InvalidParameterValueException("Can' remove the ip " + secondaryIp + "is associate with static NAT rule public IP address id " + publicIpVO.getId());
             }
         } else if (dc.getNetworkType() == NetworkType.Basic || ntwkOff.getGuestType()  == Network.GuestType.Shared) {
-            IPAddressVO ip = _ipAddressDao.findByIpAndNetworkId(secIpVO.getNetworkId(), secIpVO.getIp4Address());
+            final IPAddressVO ip = _ipAddressDao.findByIpAndNetworkId(secIpVO.getNetworkId(), secIpVO.getIp4Address());
             if (ip != null) {
-                Transaction txn = Transaction.currentTxn();
-                txn.start();
-                _ipAddrMgr.markIpAsUnavailable(ip.getId());
-                _ipAddressDao.unassignIpAddress(ip.getId());
-                txn.commit();
+                Transaction.execute(new TransactionCallbackNoReturn() {
+                    @Override
+                    public void doInTransactionWithoutResult(TransactionStatus status) {
+                        _ipAddrMgr.markIpAsUnavailable(ip.getId());
+                        _ipAddressDao.unassignIpAddress(ip.getId());
+                    }
+                });
             }
         } else {
             throw new InvalidParameterValueException("Not supported for this network now");
@@ -821,22 +828,24 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
         return success;
     }
 
-    boolean removeNicSecondaryIP(NicSecondaryIpVO ipVO, boolean lastIp) {
-        Transaction txn = Transaction.currentTxn();
-        long nicId = ipVO.getNicId();
-        NicVO nic = _nicDao.findById(nicId);
+    boolean removeNicSecondaryIP(final NicSecondaryIpVO ipVO, final boolean lastIp) {
+        final long nicId = ipVO.getNicId();
+        final NicVO nic = _nicDao.findById(nicId);
 
-        txn.start();
+        Transaction.execute(new TransactionCallbackNoReturn() {
+            @Override
+            public void doInTransactionWithoutResult(TransactionStatus status) {
+                if (lastIp) {
+                    nic.setSecondaryIp(false);
+                    s_logger.debug("Setting nics secondary ip to false ...");
+                    _nicDao.update(nicId, nic);
+                }
 
-        if (lastIp) {
-            nic.setSecondaryIp(false);
-            s_logger.debug("Setting nics secondary ip to false ...");
-            _nicDao.update(nicId, nic);
-        }
+                s_logger.debug("Revoving nic secondary ip entry ...");
+                _nicSecondaryIpDao.remove(ipVO.getId());
+            }
+        });
 
-        s_logger.debug("Revoving nic secondary ip entry ...");
-        _nicSecondaryIpDao.remove(ipVO.getId());
-        txn.commit();
         return true;
     }
 
@@ -1255,67 +1264,10 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
             throw ex;
         }
 
-        Transaction txn = Transaction.currentTxn();
-        txn.start();
-
-        Long sharedDomainId = null;
-        if (isDomainSpecific) {
-            if (domainId != null) {
-                sharedDomainId = domainId;
-            } else {
-                sharedDomainId = _domainMgr.getDomain(Domain.ROOT_DOMAIN).getId();
-                subdomainAccess = true;
-            }
-        }
-
-        // default owner to system if network has aclType=Domain
-        if (aclType == ACLType.Domain) {
-            owner = _accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM);
-        }
-
-        //Create guest network
-        Network network = null;
-        if (vpcId != null) {
-            if (!_configMgr.isOfferingForVpc(ntwkOff)){
-                throw new InvalidParameterValueException("Network offering can't be used for VPC networks");
-            }
-
-            if(aclId != null){
-                NetworkACL acl = _networkACLDao.findById(aclId);
-                if(acl == null){
-                    throw new InvalidParameterValueException("Unable to find specified NetworkACL");
-                }
-
-                if(aclId != NetworkACL.DEFAULT_DENY && aclId != NetworkACL.DEFAULT_ALLOW) {
-                    //ACL is not default DENY/ALLOW
-                    // ACL should be associated with a VPC
-                    if(!vpcId.equals(acl.getVpcId())){
-                        throw new InvalidParameterValueException("ACL: "+aclId+" do not belong to the VPC");
-                    }
-                }
-            }
-            network = _vpcMgr.createVpcGuestNetwork(networkOfferingId, name, displayText, gateway, cidr, vlanId,
-                    networkDomain, owner, sharedDomainId, pNtwk, zoneId, aclType, subdomainAccess, vpcId, aclId, caller, displayNetwork);
-        } else {
-            if (_configMgr.isOfferingForVpc(ntwkOff)){
-                throw new InvalidParameterValueException("Network offering can be used for VPC networks only");
-            }
-            if (ntwkOff.getInternalLb()) {
-                throw new InvalidParameterValueException("Internal Lb can be enabled on vpc networks only");
-            }
-
-            network = _networkMgr.createGuestNetwork(networkOfferingId, name, displayText, gateway, cidr, vlanId,
-            		networkDomain, owner, sharedDomainId, pNtwk, zoneId, aclType, subdomainAccess, vpcId,
-            		ip6Gateway, ip6Cidr, displayNetwork, isolatedPvlan);
-        }
-
-        if (caller.getType() == Account.ACCOUNT_TYPE_ADMIN && createVlan) {
-            // Create vlan ip range
-            _configMgr.createVlanAndPublicIpRange(pNtwk.getDataCenterId(), network.getId(), physicalNetworkId,
-                    false, null, startIP, endIP, gateway, netmask, vlanId, null, startIPv6, endIPv6, ip6Gateway, ip6Cidr);
-        }
-
-        txn.commit();
+        Network network = commitNetwork(networkOfferingId, gateway, startIP, endIP, netmask, networkDomain, vlanId,
+                name, displayText, caller, physicalNetworkId, zoneId, domainId, isDomainSpecific, subdomainAccess,
+                vpcId, startIPv6, endIPv6, ip6Gateway, ip6Cidr, displayNetwork, aclId, isolatedPvlan, ntwkOff, pNtwk,
+                aclType, owner, cidr, createVlan);
 
         // if the network offering has persistent set to true, implement the network
         if ( ntwkOff.getIsPersistent() ) {
@@ -1344,6 +1296,87 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
         return network;
     }
 
+    private Network commitNetwork(final Long networkOfferingId, final String gateway, final String startIP, final String endIP, final String netmask,
+            final String networkDomain, final String vlanId, final String name, final String displayText, final Account caller,
+            final Long physicalNetworkId, final Long zoneId, final Long domainId, final boolean isDomainSpecific, final Boolean subdomainAccessFinal,
+            final Long vpcId, final String startIPv6, final String endIPv6, final String ip6Gateway, final String ip6Cidr, final Boolean displayNetwork,
+            final Long aclId, final String isolatedPvlan, final NetworkOfferingVO ntwkOff, final PhysicalNetwork pNtwk, final ACLType aclType,
+            final Account ownerFinal, final String cidr, final boolean createVlan) throws InsufficientCapacityException,
+            ResourceAllocationException {
+        try {
+            return Transaction.executeWithException(new TransactionCallbackWithException<Network>() {
+                @Override
+                public Network doInTransaction(TransactionStatus status) throws Exception {
+                    Account owner = ownerFinal;
+                    Boolean subdomainAccess = subdomainAccessFinal;
+                    
+                    Long sharedDomainId = null;
+                    if (isDomainSpecific) {
+                        if (domainId != null) {
+                            sharedDomainId = domainId;
+                        } else {
+                            sharedDomainId = _domainMgr.getDomain(Domain.ROOT_DOMAIN).getId();
+                            subdomainAccess = true;
+                        }
+                    }
+            
+                    // default owner to system if network has aclType=Domain
+                    if (aclType == ACLType.Domain) {
+                        owner = _accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM);
+                    }
+            
+                    //Create guest network
+                    Network network = null;
+                    if (vpcId != null) {
+                        if (!_configMgr.isOfferingForVpc(ntwkOff)){
+                            throw new InvalidParameterValueException("Network offering can't be used for VPC networks");
+                        }
+            
+                        if(aclId != null){
+                            NetworkACL acl = _networkACLDao.findById(aclId);
+                            if(acl == null){
+                                throw new InvalidParameterValueException("Unable to find specified NetworkACL");
+                            }
+            
+                            if(aclId != NetworkACL.DEFAULT_DENY && aclId != NetworkACL.DEFAULT_ALLOW) {
+                                //ACL is not default DENY/ALLOW
+                                // ACL should be associated with a VPC
+                                if(!vpcId.equals(acl.getVpcId())){
+                                    throw new InvalidParameterValueException("ACL: "+aclId+" do not belong to the VPC");
+                                }
+                            }
+                        }
+                        network = _vpcMgr.createVpcGuestNetwork(networkOfferingId, name, displayText, gateway, cidr, vlanId,
+                                networkDomain, owner, sharedDomainId, pNtwk, zoneId, aclType, subdomainAccess, vpcId, aclId, caller, displayNetwork);
+                    } else {
+                        if (_configMgr.isOfferingForVpc(ntwkOff)){
+                            throw new InvalidParameterValueException("Network offering can be used for VPC networks only");
+                        }
+                        if (ntwkOff.getInternalLb()) {
+                            throw new InvalidParameterValueException("Internal Lb can be enabled on vpc networks only");
+                        }
+            
+                        network = _networkMgr.createGuestNetwork(networkOfferingId, name, displayText, gateway, cidr, vlanId,
+                        		networkDomain, owner, sharedDomainId, pNtwk, zoneId, aclType, subdomainAccess, vpcId,
+                        		ip6Gateway, ip6Cidr, displayNetwork, isolatedPvlan);
+                    }
+            
+                    if (caller.getType() == Account.ACCOUNT_TYPE_ADMIN && createVlan) {
+                        // Create vlan ip range
+                        _configMgr.createVlanAndPublicIpRange(pNtwk.getDataCenterId(), network.getId(), physicalNetworkId,
+                                false, null, startIP, endIP, gateway, netmask, vlanId, null, startIPv6, endIPv6, ip6Gateway, ip6Cidr);
+                    }
+                    return network;
+                }
+            }, Exception.class);
+        } catch (Exception e) {
+            ExceptionUtil.rethrowRuntime(e);
+            ExceptionUtil.rethrow(e, InsufficientCapacityException.class);
+            ExceptionUtil.rethrow(e, ResourceAllocationException.class);
+            throw new IllegalStateException(e);
+        }
+    }
+
     @Override
     public List<? extends Network> searchForNetworks(ListNetworksCmd cmd) {
         Long id = cmd.getId();
@@ -1915,13 +1948,13 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
     @Override
     @DB
     @ActionEvent(eventType = EventTypes.EVENT_NETWORK_UPDATE, eventDescription = "updating network", async = true)
-    public Network updateGuestNetwork(long networkId, String name, String displayText, Account callerAccount,
-            User callerUser, String domainSuffix, Long networkOfferingId, Boolean changeCidr, String guestVmCidr, Boolean displayNetwork) {
+    public Network updateGuestNetwork(final long networkId, String name, String displayText, Account callerAccount,
+            User callerUser, String domainSuffix, final Long networkOfferingId, Boolean changeCidr, String guestVmCidr, Boolean displayNetwork) {
 
         boolean restartNetwork = false;
 
         // verify input parameters
-        NetworkVO network = _networksDao.findById(networkId);
+        final NetworkVO network = _networksDao.findById(networkId);
         if (network == null) {
             // see NetworkVO.java
             InvalidParameterValueException ex = new InvalidParameterValueException("Specified network id doesn't exist in the system");
@@ -1975,7 +2008,7 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
 
         boolean networkOfferingChanged = false;
 
-        long oldNetworkOfferingId = network.getNetworkOfferingId();
+        final long oldNetworkOfferingId = network.getNetworkOfferingId();
         NetworkOffering oldNtwkOff = _networkOfferingDao.findByIdIncludingRemoved(oldNetworkOfferingId);
         NetworkOfferingVO networkOffering = _networkOfferingDao.findById(networkOfferingId);
         if (networkOfferingId != null) {
@@ -2024,10 +2057,8 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
             }
         }
 
-        Map<String, String> newSvcProviders = new HashMap<String, String>();
-        if (networkOfferingChanged) {
-            newSvcProviders = _networkMgr.finalizeServicesAndProvidersForNetwork(_entityMgr.findById(NetworkOffering.class, networkOfferingId), network.getPhysicalNetworkId());
-        }
+        final Map<String, String> newSvcProviders = networkOfferingChanged ? _networkMgr.finalizeServicesAndProvidersForNetwork(_entityMgr.findById(NetworkOffering.class, networkOfferingId), network.getPhysicalNetworkId())
+                : new HashMap<String, String>();
 
         // don't allow to modify network domain if the service is not supported
         if (domainSuffix != null) {
@@ -2201,29 +2232,31 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
 
         if (networkOfferingId != null) {
             if (networkOfferingChanged) {
-                Transaction txn = Transaction.currentTxn();
-                txn.start();
-                network.setNetworkOfferingId(networkOfferingId);
-                _networksDao.update(networkId, network, newSvcProviders);
-                // get all nics using this network
-                // log remove usage events for old offering
-                // log assign usage events for new offering
-                List<NicVO> nics = _nicDao.listByNetworkId(networkId);
-                for (NicVO nic : nics) {
-                    long vmId = nic.getInstanceId();
-                    VMInstanceVO vm = _vmDao.findById(vmId);
-                    if (vm == null) {
-                        s_logger.error("Vm for nic " + nic.getId() + " not found with Vm Id:" + vmId);
-                        continue;
+                Transaction.execute(new TransactionCallbackNoReturn() {
+                    @Override
+                    public void doInTransactionWithoutResult(TransactionStatus status) {
+                        network.setNetworkOfferingId(networkOfferingId);
+                        _networksDao.update(networkId, network, newSvcProviders);
+                        // get all nics using this network
+                        // log remove usage events for old offering
+                        // log assign usage events for new offering
+                        List<NicVO> nics = _nicDao.listByNetworkId(networkId);
+                        for (NicVO nic : nics) {
+                            long vmId = nic.getInstanceId();
+                            VMInstanceVO vm = _vmDao.findById(vmId);
+                            if (vm == null) {
+                                s_logger.error("Vm for nic " + nic.getId() + " not found with Vm Id:" + vmId);
+                                continue;
+                            }
+                            long isDefault = (nic.isDefaultNic()) ? 1 : 0;
+                            String nicIdString = Long.toString(nic.getId());
+                            UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_REMOVE, vm.getAccountId(), vm.getDataCenterId(),
+                                    vm.getId(), nicIdString, oldNetworkOfferingId, null, isDefault, VirtualMachine.class.getName(), vm.getUuid());
+                            UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_ASSIGN, vm.getAccountId(), vm.getDataCenterId(),
+                                    vm.getId(), nicIdString, networkOfferingId, null, isDefault, VirtualMachine.class.getName(), vm.getUuid());
+                        }
                     }
-                    long isDefault = (nic.isDefaultNic()) ? 1 : 0;
-                    String nicIdString = Long.toString(nic.getId());
-                    UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_REMOVE, vm.getAccountId(), vm.getDataCenterId(),
-                            vm.getId(), nicIdString, oldNetworkOfferingId, null, isDefault, VirtualMachine.class.getName(), vm.getUuid());
-                    UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_ASSIGN, vm.getAccountId(), vm.getDataCenterId(),
-                            vm.getId(), nicIdString, networkOfferingId, null, isDefault, VirtualMachine.class.getName(), vm.getUuid());
-                }
-                txn.commit();
+                });
             }   else {
                 network.setNetworkOfferingId(networkOfferingId);
                 _networksDao.update(networkId, network, _networkMgr.finalizeServicesAndProvidersForNetwork(_entityMgr.findById(NetworkOffering.class, networkOfferingId), network.getPhysicalNetworkId()));
@@ -2379,8 +2412,8 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
     @Override
     @DB
     @ActionEvent(eventType = EventTypes.EVENT_PHYSICAL_NETWORK_CREATE, eventDescription = "Creating Physical Network", create = true)
-    public PhysicalNetwork createPhysicalNetwork(Long zoneId, String vnetRange, String networkSpeed, List<String>
-    isolationMethods, String broadcastDomainRangeStr, Long domainId, List<String> tags, String name) {
+    public PhysicalNetwork createPhysicalNetwork(final Long zoneId, final String vnetRange, final String networkSpeed, final List<String>
+    isolationMethods, String broadcastDomainRangeStr, final Long domainId, final List<String> tags, final String name) {
 
         // Check if zone exists
         if (zoneId == null) {
@@ -2445,39 +2478,42 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
             }
         }
 
-        Transaction txn = Transaction.currentTxn();
         try {
-            txn.start();
-            // Create the new physical network in the database
-            long id = _physicalNetworkDao.getNextInSequence(Long.class, "id");
-            PhysicalNetworkVO pNetwork = new PhysicalNetworkVO(id, zoneId, vnetRange, networkSpeed, domainId, broadcastDomainRange, name);
-            pNetwork.setTags(tags);
-            pNetwork.setIsolationMethods(isolationMethods);
-
-            pNetwork = _physicalNetworkDao.persist(pNetwork);
-
-            // Add vnet entries for the new zone if zone type is Advanced
-            if (vnetRange != null) {
-                addOrRemoveVnets(vnetRange.split(","), pNetwork);
-            }
-
-            // add VirtualRouter as the default network service provider
-            addDefaultVirtualRouterToPhysicalNetwork(pNetwork.getId());
+            final BroadcastDomainRange broadcastDomainRangeFinal = broadcastDomainRange;
+            return Transaction.execute(new TransactionCallback<PhysicalNetworkVO>() {
+                @Override
+                public PhysicalNetworkVO doInTransaction(TransactionStatus status) {
+                    // Create the new physical network in the database
+                    long id = _physicalNetworkDao.getNextInSequence(Long.class, "id");
+                    PhysicalNetworkVO pNetwork = new PhysicalNetworkVO(id, zoneId, vnetRange, networkSpeed, domainId, broadcastDomainRangeFinal, name);
+                    pNetwork.setTags(tags);
+                    pNetwork.setIsolationMethods(isolationMethods);
+
+                    pNetwork = _physicalNetworkDao.persist(pNetwork);
+
+                    // Add vnet entries for the new zone if zone type is Advanced
+                    if (vnetRange != null) {
+                        addOrRemoveVnets(vnetRange.split(","), pNetwork);
+                    }
 
-            // add security group provider to the physical network
-            addDefaultSecurityGroupProviderToPhysicalNetwork(pNetwork.getId());
+                    // add VirtualRouter as the default network service provider
+                    addDefaultVirtualRouterToPhysicalNetwork(pNetwork.getId());
 
-            // add VPCVirtualRouter as the defualt network service provider
-            addDefaultVpcVirtualRouterToPhysicalNetwork(pNetwork.getId());
+                    // add security group provider to the physical network
+                    addDefaultSecurityGroupProviderToPhysicalNetwork(pNetwork.getId());
 
-            // add baremetal as the defualt network service provider
-            addDefaultBaremetalProvidersToPhysicalNetwork(pNetwork.getId());
+                    // add VPCVirtualRouter as the defualt network service provider
+                    addDefaultVpcVirtualRouterToPhysicalNetwork(pNetwork.getId());
 
-            //Add Internal Load Balancer element as a default network service provider
-            addDefaultInternalLbProviderToPhysicalNetwork(pNetwork.getId());
+                    // add baremetal as the defualt network service provider
+                    addDefaultBaremetalProvidersToPhysicalNetwork(pNetwork.getId());
 
-            txn.commit();
-            return pNetwork;
+                    //Add Internal Load Balancer element as a default network service provider
+                    addDefaultInternalLbProviderToPhysicalNetwork(pNetwork.getId());
+                    
+                    return pNetwork;
+                }
+            });
         } catch (Exception ex) {
             s_logger.warn("Exception: ", ex);
             throw new CloudRuntimeException("Fail to create a physical network");
@@ -2568,7 +2604,7 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
     }
 
     @DB
-    public void addOrRemoveVnets(String [] listOfRanges,  PhysicalNetworkVO network) {
+    public void addOrRemoveVnets(String [] listOfRanges, final PhysicalNetworkVO network) {
         List<String>  addVnets = null;
         List<String>  removeVnets =null;
         HashSet<String> tempVnets = new HashSet<String>();
@@ -2611,22 +2647,27 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
             }
             network.setVnet(comaSeperatedStingOfVnetRanges);
 
-            Transaction txn = Transaction.currentTxn();
-            txn.start();
-            if (addVnets != null) {
-                s_logger.debug("Adding vnet range " + addVnets.toString()+ " for the physicalNetwork id= " + network.getId() + " and zone id=" + network.getDataCenterId()
-                        + " as a part of updatePhysicalNetwork call");
-                //add vnet takes a list of strings to be added. each string is a vnet.
-                _dcDao.addVnet(network.getDataCenterId(), network.getId(), addVnets);
-            }
-            if (removeVnets != null) {
-                s_logger.debug("removing vnet range " + removeVnets.toString()+ " for the physicalNetwork id= " + network.getId() + " and zone id=" + network.getDataCenterId()
-                        + " as a part of updatePhysicalNetwork call");
-                //deleteVnets  takes a list of strings to be removed. each string is a vnet.
-                _datacneter_vnet.deleteVnets(txn, network.getDataCenterId(), network.getId(), removeVnets);
-            }
-            _physicalNetworkDao.update(network.getId(), network);
-            txn.commit();
+            final List<String> addVnetsFinal = addVnets;
+            final List<String> removeVnetsFinal = removeVnets;
+            Transaction.execute(new TransactionCallbackNoReturn() {
+                @Override
+                public void doInTransactionWithoutResult(TransactionStatus status) {
+                    if (addVnetsFinal != null) {
+                        s_logger.debug("Adding vnet range " + addVnetsFinal.toString()+ " for the physicalNetwork id= " + network.getId() + " and zone id=" + network.getDataCenterId()
+                                + " as a part of updatePhysicalNetwork call");
+                        //add vnet takes a list of strings to be added. each string is a vnet.
+                        _dcDao.addVnet(network.getDataCenterId(), network.getId(), addVnetsFinal);
+                    }
+                    if (removeVnetsFinal != null) {
+                        s_logger.debug("removing vnet range " + removeVnetsFinal.toString()+ " for the physicalNetwork id= " + network.getId() + " and zone id=" + network.getDataCenterId()
+                                + " as a part of updatePhysicalNetwork call");
+                        //deleteVnets  takes a list of strings to be removed. each string is a vnet.
+                        _datacneter_vnet.deleteVnets(TransactionLegacy.currentTxn(), network.getDataCenterId(), network.getId(), removeVnetsFinal);
+                    }
+                    _physicalNetworkDao.update(network.getId(), network);
+                }
+            });
+
             _physicalNetworkDao.releaseFromLockTable(network.getId());
         }
     }
@@ -2771,7 +2812,7 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
     @Override
     @ActionEvent(eventType = EventTypes.EVENT_PHYSICAL_NETWORK_DELETE, eventDescription = "deleting physical network", async = true)
     @DB
-    public boolean deletePhysicalNetwork(Long physicalNetworkId) {
+    public boolean deletePhysicalNetwork(final Long physicalNetworkId) {
 
         // verify input parameters
         PhysicalNetworkVO pNetwork = _physicalNetworkDao.findById(physicalNetworkId);
@@ -2783,48 +2824,47 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
 
         checkIfPhysicalNetworkIsDeletable(physicalNetworkId);
 
-        Transaction txn = Transaction.currentTxn();
-        txn.start();
-        // delete vlans for this zone
-        List<VlanVO> vlans = _vlanDao.listVlansByPhysicalNetworkId(physicalNetworkId);
-        for (VlanVO vlan : vlans) {
-            _vlanDao.remove(vlan.getId());
-        }
-
-        // Delete networks
-        List<NetworkVO> networks = _networksDao.listByPhysicalNetwork(physicalNetworkId);
-        if (networks != null && !networks.isEmpty()) {
-            for (NetworkVO network : networks) {
-                _networksDao.remove(network.getId());
-            }
-        }
-
-        // delete vnets
-        _dcDao.deleteVnet(physicalNetworkId);
-
-        // delete service providers
-        List<PhysicalNetworkServiceProviderVO> providers = _pNSPDao.listBy(physicalNetworkId);
-
-        for(PhysicalNetworkServiceProviderVO provider : providers){
-            try {
-                deleteNetworkServiceProvider(provider.getId());
-            }catch (ResourceUnavailableException e) {
-                s_logger.warn("Unable to complete destroy of the physical network provider: " + provider.getProviderName() + ", id: "+ provider.getId(), e);
-                return false;
-            } catch (ConcurrentOperationException e) {
-                s_logger.warn("Unable to complete destroy of the physical network provider: " + provider.getProviderName() + ", id: "+ provider.getId(), e);
-                return false;
+        return Transaction.execute(new TransactionCallback<Boolean>() {
+            @Override
+            public Boolean doInTransaction(TransactionStatus status) {
+                // delete vlans for this zone
+                List<VlanVO> vlans = _vlanDao.listVlansByPhysicalNetworkId(physicalNetworkId);
+                for (VlanVO vlan : vlans) {
+                    _vlanDao.remove(vlan.getId());
+                }
+        
+                // Delete networks
+                List<NetworkVO> networks = _networksDao.listByPhysicalNetwork(physicalNetworkId);
+                if (networks != null && !networks.isEmpty()) {
+                    for (NetworkVO network : networks) {
+                        _networksDao.remove(network.getId());
+                    }
+                }
+        
+                // delete vnets
+                _dcDao.deleteVnet(physicalNetworkId);
+        
+                // delete service providers
+                List<PhysicalNetworkServiceProviderVO> providers = _pNSPDao.listBy(physicalNetworkId);
+        
+                for(PhysicalNetworkServiceProviderVO provider : providers){
+                    try {
+                        deleteNetworkServiceProvider(provider.getId());
+                    }catch (ResourceUnavailableException e) {
+                        s_logger.warn("Unable to complete destroy of the physical network provider: " + provider.getProviderName() + ", id: "+ provider.getId(), e);
+                        return false;
+                    } catch (ConcurrentOperationException e) {
+                        s_logger.warn("Unable to complete destroy of the physical network provider: " + provider.getProviderName() + ", id: "+ provider.getId(), e);
+                        return false;
+                    }
+                }
+        
+                // delete traffic types
+                _pNTrafficTypeDao.deleteTrafficTypes(physicalNetworkId);
+        
+                return _physicalNetworkDao.remove(physicalNetworkId);
             }
-        }
-
-        // delete traffic types
-        _pNTrafficTypeDao.deleteTrafficTypes(physicalNetworkId);
-
-        boolean success = _physicalNetworkDao.remove(physicalNetworkId);
-
-        txn.commit();
-
-        return success;
+        });
     }
 
     @DB
@@ -2882,7 +2922,7 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
                 selectSql += " AND taken IS NOT NULL";
             }
 
-            Transaction txn = Transaction.currentTxn();
+            TransactionLegacy txn = TransactionLegacy.currentTxn();
             try {
                 PreparedStatement stmt = txn.prepareAutoCloseStatement(selectSql);
                 stmt.setLong(1, physicalNetworkId);
@@ -3045,12 +3085,9 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
             accountGuestVlanMapVO.setGuestVlanRange(updatedVlanRange);
             _accountGuestVlanMapDao.update(guestVlanMapId, accountGuestVlanMapVO);
         } else {
-            Transaction txn = Transaction.currentTxn();
-            txn.start();
             accountGuestVlanMapVO = new AccountGuestVlanMapVO(vlanOwner.getAccountId(), physicalNetworkId);
             accountGuestVlanMapVO.setGuestVlanRange(startVlan + "-" +  endVlan);
             _accountGuestVlanMapDao.persist(accountGuestVlanMapVO);
-            txn.commit();
         }
         // For every guest vlan set the corresponding account guest vlan map id
         List<Integer> finaVlanTokens = getVlanFromRange(accountGuestVlanMapVO.getGuestVlanRange());
@@ -3276,9 +3313,7 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
             services = new ArrayList<Service>(element.getCapabilities().keySet());
         }
 
-        Transaction txn = Transaction.currentTxn();
         try {
-            txn.start();
             // Create the new physical network in the database
             PhysicalNetworkServiceProviderVO nsp = new PhysicalNetworkServiceProviderVO(physicalNetworkId, providerName);
             // set enabled services
@@ -3289,7 +3324,6 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
             }
             nsp = _pNSPDao.persist(nsp);
 
-            txn.commit();
             return nsp;
         } catch (Exception ex) {
             s_logger.warn("Exception: ", ex);
@@ -3537,9 +3571,7 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
             }
         }
 
-        Transaction txn = Transaction.currentTxn();
         try {
-            txn.start();
             // Create the new traffic type in the database
             if (xenLabel == null) {
                 xenLabel = getDefaultXenNetworkLabel(trafficType);
@@ -3547,7 +3579,6 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
             PhysicalNetworkTrafficTypeVO pNetworktrafficType = new PhysicalNetworkTrafficTypeVO(physicalNetworkId, trafficType, xenLabel, kvmLabel, vmwareLabel, simulatorLabel, vlan);
             pNetworktrafficType = _pNTrafficTypeDao.persist(pNetworktrafficType);
 
-            txn.commit();
             return pNetworktrafficType;
         } catch (Exception ex) {
             s_logger.warn("Exception: ", ex);
@@ -3809,11 +3840,11 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
 
 
     @Override @DB
-    public Network createPrivateNetwork(String networkName, String displayText, long physicalNetworkId,
-            String broadcastUriString, String startIp, String endIp, String gateway, String netmask, long networkOwnerId, Long vpcId, Boolean sourceNat, Long networkOfferingId)
+    public Network createPrivateNetwork(final String networkName, final String displayText, long physicalNetworkId,
+            String broadcastUriString, final String startIp, String endIp, final String gateway, String netmask, final long networkOwnerId, final Long vpcId, final Boolean sourceNat, final Long networkOfferingId)
                     throws ResourceAllocationException, ConcurrentOperationException, InsufficientCapacityException {
 
-        Account owner = _accountMgr.getAccount(networkOwnerId);
+        final Account owner = _accountMgr.getAccount(networkOwnerId);
 
         // Get system network offering
         NetworkOfferingVO ntwkOff = null;
@@ -3827,7 +3858,7 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
         }
 
         // Validate physical network
-        PhysicalNetwork pNtwk = _physicalNetworkDao.findById(physicalNetworkId);
+        final PhysicalNetwork pNtwk = _physicalNetworkDao.findById(physicalNetworkId);
         if (pNtwk == null) {
             InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find a physical network" +
                     " having the given id");
@@ -3846,7 +3877,6 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
             throw new InvalidParameterValueException("Invalid format for the endIp address parameter");
         }
 
-        String cidr = null;
         if (!NetUtils.isValidIp(gateway)) {
             throw new InvalidParameterValueException("Invalid gateway");
         }
@@ -3854,10 +3884,10 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
             throw new InvalidParameterValueException("Invalid netmask");
         }
 
-        cidr = NetUtils.ipAndNetMaskToCidr(gateway, netmask);
+        final String cidr = NetUtils.ipAndNetMaskToCidr(gateway, netmask);
 
         URI uri = BroadcastDomainType.fromString(broadcastUriString);
-        String uriString = uri.toString();
+        final String uriString = uri.toString();
         BroadcastDomainType tiep = BroadcastDomainType.getSchemeValue(uri);
         // numeric vlan or vlan uri are ok for now
         // TODO make a test for any supported scheme
@@ -3866,49 +3896,58 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService {
             throw new InvalidParameterValueException("unsupported type of broadcastUri specified: " + broadcastUriString);
         }
 
-        Transaction txn = Transaction.currentTxn();
-        txn.start();
-
-        //lock datacenter as we need to get mac address seq from there
-        DataCenterVO dc = _dcDao.lockRow(pNtwk.getDataCenterId(), true);
-
-        //check if we need to create guest network
-        Network privateNetwork = _networksDao.getPrivateNetwork(uriString, cidr,
-                networkOwnerId, pNtwk.getDataCenterId(), networkOfferingId);
-        if (privateNetwork == null) {
-            //create Guest network
-            privateNetwork = _networkMgr.createGuestNetwork(ntwkOff.getId(), networkName, displayText, gateway, cidr, uriString,
-                    null, owner, null, pNtwk, pNtwk.getDataCenterId(), ACLType.Account, null, vpcId, null, null, true, null);
-            s_logger.debug("Created private network " + privateNetwork);
-        } else {
-            s_logger.debug("Private network already exists: " + privateNetwork);
-            //Do not allow multiple private gateways with same Vlan within a VPC
-            if(vpcId.equals(privateNetwork.getVpcId())){
-                throw new InvalidParameterValueException("Private network for the vlan: " + uriString + " and cidr  "+ cidr +"  already exists " +
-                        "for Vpc "+vpcId+" in zone " + _entityMgr.findById(DataCenter.class, pNtwk.getDataCenterId()).getName());
-            }
-        }
-
-        //add entry to private_ip_address table
-        PrivateIpVO privateIp = _privateIpDao.findByIpAndSourceNetworkIdAndVpcId(privateNetwork.getId(), startIp, vpcId);
-        if (privateIp != null) {
-            throw new InvalidParameterValueException("Private ip address " + startIp + " already used for private gateway" +
-                    " in zone " + _entityMgr.findById(DataCenter.class, pNtwk.getDataCenterId()).getName());
+        final NetworkOfferingVO ntwkOffFinal = ntwkOff; 
+        try {
+            return Transaction.executeWithException(new TransactionCallbackWithException<Network>() {
+                @Override
+                public Network doInTransaction(TransactionStatus status) throws ResourceAllocationException, InsufficientCapacityException {
+                    //lock datacenter as we need to get mac address seq from there
+                    DataCenterVO dc = _dcDao.lockRow(pNtwk.getDataCenterId(), true);
+            
+                    //check if we need to create guest network
+                    Network privateNetwork = _networksDao.getPrivateNetwork(uriString, cidr,
+                            networkOwnerId, pNtwk.getDataCenterId(), networkOfferingId);
+                    if (privateNetwork == null) {
+                        //create Guest network
+                        privateNetwork = _networkMgr.createGuestNetwork(ntwkOffFinal.getId(), networkName, displayText, gateway, cidr, uriString,
+                                null, owner, null, pNtwk, pNtwk.getDataCenterId(), ACLType.Account, null, vpcId, null, null, true, null);
+                        s_logger.debug("Created private network " + privateNetwork);
+                    } else {
+                        s_logger.debug("Private network already exists: " + privateNetwork);
+                        //Do not allow multiple private gateways with same Vlan within a VPC
+                        if(vpcId.equals(privateNetwork.getVpcId())){
+                            throw new InvalidParameterValueException("Private network for the vlan: " + uriString + " and cidr  "+ cidr +"  already exists " +
+                                    "for Vpc "+vpcId+" in zone " + _entityMgr.findById(DataCenter.class, pNtwk.getDataCenterId()).getName());
+                        }
+                    }
+            
+                    //add entry to private_ip_address table
+                    PrivateIpVO privateIp = _privateIpDao.findByIpAndSourceNetworkIdAndVpcId(privateNetwork.getId(), startIp, vpcId);
+                    if (privateIp != null) {
+                        throw new InvalidParameterValueException("Private ip address " + startIp + " already used for private gateway" +
+                                " in zone " + _entityMgr.findById(DataCenter.class, pNtwk.getDataCenterId()).getName());
+                    }
+            
+                    Long mac = dc.getMacAddress();
+                    Long nextMac = mac + 1;
+                    dc.setMacAddress(nextMac);
+            
+                    privateIp = new PrivateIpVO(startIp, privateNetwork.getId(), nextMac, vpcId, sourceNat);
+                    _privateIpDao.persist(privateIp);
+            
+                    _dcDao.update(dc.getId(), dc);
+            
+                    s_logger.debug("Private network " + privateNetwork + " is created");
+            
+                    return privateNetwork;
+                }
+            }, Exception.class);
+        } catch (Exception e) {
+            ExceptionUtil.rethrowRuntime(e);
+            ExceptionUtil.rethrow(e, ResourceAllocationException.class);
+            ExceptionUtil.rethrow(e, InsufficientCapacityException.class);
+            throw new IllegalStateException(e);
         }
-
-        Long mac = dc.getMacAddress();
-        Long nextMac = mac + 1;
-        dc.setMacAddress(nextMac);
-
-        privateIp = new PrivateIpVO(startIp, privateNetwork.getId(), nextMac, vpcId, sourceNat);
-        _privateIpDao.persist(privateIp);
-
-        _dcDao.update(dc.getId(), dc);
-
-        txn.commit();
-        s_logger.debug("Private network " + privateNetwork + " is created");
-
-        return privateNetwork;
     }
 
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f62e28c1/server/src/com/cloud/network/NetworkUsageManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/NetworkUsageManagerImpl.java b/server/src/com/cloud/network/NetworkUsageManagerImpl.java
index 41c1bc2..0f2feb0 100755
--- a/server/src/com/cloud/network/NetworkUsageManagerImpl.java
+++ b/server/src/com/cloud/network/NetworkUsageManagerImpl.java
@@ -88,6 +88,8 @@ import com.cloud.utils.db.GlobalLock;
 import com.cloud.utils.db.JoinBuilder;
 import com.cloud.utils.db.SearchBuilder;
 import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.db.TransactionCallbackNoReturn;
+import com.cloud.utils.db.TransactionStatus;
 import com.cloud.utils.db.SearchCriteria.Op;
 import com.cloud.utils.db.Transaction;
 import com.cloud.utils.exception.CloudRuntimeException;
@@ -290,11 +292,11 @@ public class NetworkUsageManagerImpl extends ManagerBase implements NetworkUsage
             return false;
         }
 
-        private boolean collectDirectNetworkUsage(HostVO host){
+        private boolean collectDirectNetworkUsage(final HostVO host){
             s_logger.debug("Direct Network Usage stats collector is running...");
 
-            long zoneId = host.getDataCenterId();
-            DetailVO lastCollectDetail = _detailsDao.findDetail(host.getId(),"last_collection");
+            final long zoneId = host.getDataCenterId();
+            final DetailVO lastCollectDetail = _detailsDao.findDetail(host.getId(),"last_collection");
             if(lastCollectDetail == null){
                 s_logger.warn("Last collection time not available. Skipping direct usage collection for Traffic Monitor: "+host.getId());
                 return false;
@@ -309,7 +311,7 @@ public class NetworkUsageManagerImpl extends ManagerBase implements NetworkUsage
             // This coule be made configurable
 
             rightNow.add(Calendar.HOUR_OF_DAY, -2);
-            Date now = rightNow.getTime();  
+            final Date now = rightNow.getTime();  
             
             if(lastCollection.after(now)){
                 s_logger.debug("Current time is less than 2 hours after last collection time : " + lastCollection.toString() + ". Skipping direct network usage collection");
@@ -361,7 +363,7 @@ public class NetworkUsageManagerImpl extends ManagerBase implements NetworkUsage
 
             }
 
-            List<UserStatisticsVO> collectedStats = new ArrayList<UserStatisticsVO>();
+            final List<UserStatisticsVO> collectedStats = new ArrayList<UserStatisticsVO>();
             
             //Get usage for Ips which were assigned for the entire duration
             if(fullDurationIpUsage.size() > 0){
@@ -431,28 +433,26 @@ public class NetworkUsageManagerImpl extends ManagerBase implements NetworkUsage
             	return false;
             }
             //Persist all the stats and last_collection time in a single transaction
-            Transaction txn = Transaction.open(Transaction.CLOUD_DB);
-            try {
-                txn.start();
-                for(UserStatisticsVO stat : collectedStats){
-                    UserStatisticsVO stats = _statsDao.lock(stat.getAccountId(), stat.getDataCenterId(), 0L, null, host.getId(), "DirectNetwork");
-                    if (stats == null) {
-                        stats = new UserStatisticsVO(stat.getAccountId(), zoneId, null, host.getId(), "DirectNetwork", 0L);
-                        stats.setCurrentBytesSent(stat.getCurrentBytesSent());
-                        stats.setCurrentBytesReceived(stat.getCurrentBytesReceived());
-                        _statsDao.persist(stats);
-                    } else {
-                        stats.setCurrentBytesSent(stats.getCurrentBytesSent() + stat.getCurrentBytesSent());
-                        stats.setCurrentBytesReceived(stats.getCurrentBytesReceived() + stat.getCurrentBytesReceived());
-                        _statsDao.update(stats.getId(), stats);
+            Transaction.execute(new TransactionCallbackNoReturn() {
+                @Override
+                public void doInTransactionWithoutResult(TransactionStatus status) {
+                    for(UserStatisticsVO stat : collectedStats){
+                        UserStatisticsVO stats = _statsDao.lock(stat.getAccountId(), stat.getDataCenterId(), 0L, null, host.getId(), "DirectNetwork");
+                        if (stats == null) {
+                            stats = new UserStatisticsVO(stat.getAccountId(), zoneId, null, host.getId(), "DirectNetwork", 0L);
+                            stats.setCurrentBytesSent(stat.getCurrentBytesSent());
+                            stats.setCurrentBytesReceived(stat.getCurrentBytesReceived());
+                            _statsDao.persist(stats);
+                        } else {
+                            stats.setCurrentBytesSent(stats.getCurrentBytesSent() + stat.getCurrentBytesSent());
+                            stats.setCurrentBytesReceived(stats.getCurrentBytesReceived() + stat.getCurrentBytesReceived());
+                            _statsDao.update(stats.getId(), stats);
+                        }
                     }
+                    lastCollectDetail.setValue(""+now.getTime());
+                    _detailsDao.update(lastCollectDetail.getId(), lastCollectDetail);
                 }
-                lastCollectDetail.setValue(""+now.getTime());
-                _detailsDao.update(lastCollectDetail.getId(), lastCollectDetail);
-                txn.commit();
-            } finally {
-                txn.close();
-            }
+            });
 
             return true;
         }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f62e28c1/server/src/com/cloud/network/PortProfileManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/PortProfileManagerImpl.java b/server/src/com/cloud/network/PortProfileManagerImpl.java
index f17ee6f..c7315979 100644
--- a/server/src/com/cloud/network/PortProfileManagerImpl.java
+++ b/server/src/com/cloud/network/PortProfileManagerImpl.java
@@ -55,18 +55,7 @@ public class PortProfileManagerImpl {
     	// Else, go ahead and create the port profile.
     	PortProfileVO portProfileObj = new PortProfileVO(portProfName, vsmId, vlanId, pType, bType);
 
-    	Transaction txn = Transaction.currentTxn();
-    	try {
-    		txn.start();
-    		_portProfileDao.persist(portProfileObj);
-    		txn.commit();
-    	} catch (Exception e) {
-    		txn.rollback();
-    		throw new CloudRuntimeException(e.getMessage());
-    	}
-
-    	// Return the PortProfileVO object created.
-        return portProfileObj;        
+		return _portProfileDao.persist(portProfileObj);
     }
     
     @DB    
@@ -95,18 +84,7 @@ public class PortProfileManagerImpl {
     	// Else, go ahead and create the port profile.
     	portProfileObj = new PortProfileVO(portProfName, vsmId, lowVlanId, highVlanId, pType, bType);
 
-    	Transaction txn = Transaction.currentTxn();
-    	try {
-    		txn.start();
-    		_portProfileDao.persist(portProfileObj);
-    		txn.commit();
-    	} catch (Exception e) {
-    		txn.rollback();
-    		throw new CloudRuntimeException(e.getMessage());
-    	}
-
-    	// Return the PortProfileVO object created.
-        return portProfileObj;        
+		return _portProfileDao.persist(portProfileObj);
     }
     
     @DB
@@ -121,16 +99,7 @@ public class PortProfileManagerImpl {
         // TODO: Should we be putting any checks here before removing
         // the port profile record from the db?
         
-        Transaction txn = Transaction.currentTxn();
-        try {
-            txn.start();
-            // Remove the VSM entry in CiscoNexusVSMDeviceVO's table.            
-            _portProfileDao.remove(portProfileId);            
-            txn.commit();
-        } catch (Exception e) {
-        	s_logger.info("Caught exception when trying to delete Port Profile record.." + e.getMessage());        	
-        	throw new CloudRuntimeException("Failed to delete Port Profile");
-        }        
-        return true;
+        // Remove the VSM entry in CiscoNexusVSMDeviceVO's table.            
+        return _portProfileDao.remove(portProfileId);            
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f62e28c1/server/src/com/cloud/network/StorageNetworkManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/StorageNetworkManagerImpl.java b/server/src/com/cloud/network/StorageNetworkManagerImpl.java
index c0cb289..73b45ab 100755
--- a/server/src/com/cloud/network/StorageNetworkManagerImpl.java
+++ b/server/src/com/cloud/network/StorageNetworkManagerImpl.java
@@ -26,7 +26,6 @@ import javax.inject.Inject;
 
 import org.apache.log4j.Logger;
 import org.springframework.stereotype.Component;
-
 import org.apache.cloudstack.api.command.admin.network.CreateStorageNetworkIpRangeCmd;
 import org.apache.cloudstack.api.command.admin.network.DeleteStorageNetworkIpRangeCmd;
 import org.apache.cloudstack.api.command.admin.network.ListStorageNetworkIpRangeCmd;
@@ -46,8 +45,12 @@ import com.cloud.network.dao.NetworkVO;
 import com.cloud.utils.component.ManagerBase;
 import com.cloud.utils.db.DB;
 import com.cloud.utils.db.QueryBuilder;
+import com.cloud.utils.db.TransactionCallbackNoReturn;
+import com.cloud.utils.db.TransactionCallbackWithException;
+import com.cloud.utils.db.TransactionStatus;
 import com.cloud.utils.db.SearchCriteria.Op;
 import com.cloud.utils.db.Transaction;
+import com.cloud.utils.db.TransactionLegacy;
 import com.cloud.utils.exception.CloudRuntimeException;
 import com.cloud.utils.net.NetUtils;
 import com.cloud.vm.SecondaryStorageVmVO;
@@ -93,7 +96,7 @@ public class StorageNetworkManagerImpl extends ManagerBase implements StorageNet
         }
     }
 
-    private void createStorageIpEntires(Transaction txn, long rangeId, String startIp, String endIp, long zoneId) throws SQLException {
+    private void createStorageIpEntires(TransactionLegacy txn, long rangeId, String startIp, String endIp, long zoneId) throws SQLException {
         long startIPLong = NetUtils.ip2Long(startIp);
         long endIPLong = NetUtils.ip2Long(endIp);
         String insertSql = "INSERT INTO `cloud`.`op_dc_storage_network_ip_address` (range_id, ip_address, mac_address, taken) VALUES (?, ?, (select mac_address from `cloud`.`data_center` where id=?), ?)";
@@ -120,11 +123,11 @@ public class StorageNetworkManagerImpl extends ManagerBase implements StorageNet
     @Override
     @DB
     public StorageNetworkIpRange updateIpRange(UpdateStorageNetworkIpRangeCmd cmd) {
-        Integer vlan = cmd.getVlan();
-        Long rangeId = cmd.getId();
+        final Integer vlan = cmd.getVlan();
+        final Long rangeId = cmd.getId();
         String startIp = cmd.getStartIp();
         String endIp = cmd.getEndIp();
-        String netmask = cmd.getNetmask();
+        final String netmask = cmd.getNetmask();
 
         if (netmask != null && !NetUtils.isValidNetmask(netmask)) {
             throw new CloudRuntimeException("Invalid netmask:" + netmask);
@@ -147,45 +150,50 @@ public class StorageNetworkManagerImpl extends ManagerBase implements StorageNet
             checkOverlapStorageIpRange(podId, startIp, endIp);
         }
 
-        Transaction txn = Transaction.currentTxn();
-        txn.start();
-        try {
-            range = _sNwIpRangeDao.acquireInLockTable(range.getId());
-            if (range == null) {
-                throw new CloudRuntimeException("Cannot acquire lock on storage ip range " + rangeId);
-            }
-            StorageNetworkIpRangeVO vo = _sNwIpRangeDao.createForUpdate();
-            if (vlan != null) {
-                vo.setVlan(vlan);
-            }
-            if (startIp != null) {
-                vo.setStartIp(startIp);
-            }
-            if (endIp != null) {
-                vo.setEndIp(endIp);
-            }
-            if (netmask != null) {
-                vo.setNetmask(netmask);
-            }
-            _sNwIpRangeDao.update(rangeId, vo);
-        } finally {
-            if (range != null) {
-                _sNwIpRangeDao.releaseFromLockTable(range.getId());
+        final String startIpFinal = startIp;
+        final String endIpFinal = endIp;
+        Transaction.execute(new TransactionCallbackNoReturn() {
+            @Override
+            public void doInTransactionWithoutResult(TransactionStatus status) {
+                StorageNetworkIpRangeVO range = null;
+                try {
+                    range = _sNwIpRangeDao.acquireInLockTable(rangeId);
+                    if (range == null) {
+                        throw new CloudRuntimeException("Cannot acquire lock on storage ip range " + rangeId);
+                    }
+                    StorageNetworkIpRangeVO vo = _sNwIpRangeDao.createForUpdate();
+                    if (vlan != null) {
+                        vo.setVlan(vlan);
+                    }
+                    if (startIpFinal != null) {
+                        vo.setStartIp(startIpFinal);
+                    }
+                    if (endIpFinal != null) {
+                        vo.setEndIp(endIpFinal);
+                    }
+                    if (netmask != null) {
+                        vo.setNetmask(netmask);
+                    }
+                    _sNwIpRangeDao.update(rangeId, vo);
+                } finally {
+                    if (range != null) {
+                        _sNwIpRangeDao.releaseFromLockTable(range.getId());
+                    }
+                }
             }
-        }
-        txn.commit();
+        });
 
         return _sNwIpRangeDao.findById(rangeId);
     }
 
     @Override
     @DB
-    public StorageNetworkIpRange createIpRange(CreateStorageNetworkIpRangeCmd cmd) throws SQLException {
-        Long podId = cmd.getPodId();
-        String startIp = cmd.getStartIp();
+    public StorageNetworkIpRange createIpRange(final CreateStorageNetworkIpRangeCmd cmd) throws SQLException {
+        final Long podId = cmd.getPodId();
+        final String startIp = cmd.getStartIp();
         String endIp = cmd.getEndIp();
-        Integer vlan = cmd.getVlan();
-        String netmask = cmd.getNetmask();
+        final Integer vlan = cmd.getVlan();
+        final String netmask = cmd.getNetmask();
 
         if (endIp == null) {
             endIp = startIp;
@@ -199,7 +207,7 @@ public class StorageNetworkManagerImpl extends ManagerBase implements StorageNet
         if (pod == null) {
             throw new CloudRuntimeException("Cannot find pod " + podId);
         }
-        Long zoneId = pod.getDataCenterId();
+        final Long zoneId = pod.getDataCenterId();
 
         List<NetworkVO> nws = _networkDao.listByZoneAndTrafficType(zoneId, TrafficType.Storage);
         if (nws.size() == 0) {
@@ -208,34 +216,35 @@ public class StorageNetworkManagerImpl extends ManagerBase implements StorageNet
         if (nws.size() > 1) {
             throw new CloudRuntimeException("Find more than one storage network in zone " + zoneId + "," + nws.size() + " found");
         }
-        NetworkVO nw = nws.get(0);
+        final NetworkVO nw = nws.get(0);
 
         checkOverlapPrivateIpRange(podId, startIp, endIp);
         checkOverlapStorageIpRange(podId, startIp, endIp);
 
-        Transaction txn = Transaction.currentTxn();
         StorageNetworkIpRangeVO range = null;
 
-        txn.start();
-        range = new StorageNetworkIpRangeVO(zoneId, podId, nw.getId(), startIp, endIp, vlan, netmask, cmd.getGateWay());
-        _sNwIpRangeDao.persist(range);
-        try {
-            createStorageIpEntires(txn, range.getId(), startIp, endIp, zoneId);
-        } catch (SQLException e) {
-            txn.rollback();
-            StringBuilder err = new StringBuilder();
-            err.append("Create storage network range failed.");
-            err.append("startIp=" + startIp);
-            err.append("endIp=" + endIp);
-            err.append("netmask=" + netmask);
-            err.append("zoneId=" + zoneId);
-            s_logger.debug(err.toString(), e);
-            throw e;
-        }
-
-        txn.commit();
+        final String endIpFinal = endIp;
+        return Transaction.executeWithException(new TransactionCallbackWithException<StorageNetworkIpRangeVO>() {
+            @Override
+            public StorageNetworkIpRangeVO doInTransaction(TransactionStatus status) throws SQLException {
+                StorageNetworkIpRangeVO range = new StorageNetworkIpRangeVO(zoneId, podId, nw.getId(), startIp, endIpFinal, vlan, netmask, cmd.getGateWay());
+                _sNwIpRangeDao.persist(range);
+                try {
+                    createStorageIpEntires(TransactionLegacy.currentTxn(), range.getId(), startIp, endIpFinal, zoneId);
+                } catch (SQLException e) {
+                    StringBuilder err = new StringBuilder();
+                    err.append("Create storage network range failed.");
+                    err.append("startIp=" + startIp);
+                    err.append("endIp=" + endIpFinal);
+                    err.append("netmask=" + netmask);
+                    err.append("zoneId=" + zoneId);
+                    s_logger.debug(err.toString(), e);
+                    throw e;
+                }
 
-        return range;
+                return range;
+            }
+        }, SQLException.class);
     }
 
     private String getInUseIpAddress(long rangeId) {
@@ -251,7 +260,7 @@ public class StorageNetworkManagerImpl extends ManagerBase implements StorageNet
     @Override
     @DB
     public void deleteIpRange(DeleteStorageNetworkIpRangeCmd cmd) {
-        long rangeId = cmd.getId();
+        final long rangeId = cmd.getId();
         StorageNetworkIpRangeVO range = _sNwIpRangeDao.findById(rangeId);
         if (range == null) {
             throw new CloudRuntimeException("Can not find storage network ip range " + rangeId);
@@ -261,26 +270,30 @@ public class StorageNetworkManagerImpl extends ManagerBase implements StorageNet
             throw new CloudRuntimeException(getInUseIpAddress(rangeId));
         }
 
-        final Transaction txn = Transaction.currentTxn();
-        txn.start();
-        try {
-            range = _sNwIpRangeDao.acquireInLockTable(rangeId);
-            if (range == null) {
-                String msg = "Unable to acquire lock on storage network ip range id=" + rangeId + ", delete failed";
-                s_logger.warn(msg);
-                throw new CloudRuntimeException(msg);
-            }
-            /*
-             * entries in op_dc_storage_network_ip_address will be deleted automatically due to
-             * fk_storage_ip_address__range_id constraint key
-             */
-            _sNwIpRangeDao.remove(rangeId);
-        } finally {
-            if (range != null) {
-                _sNwIpRangeDao.releaseFromLockTable(rangeId);
+        Transaction.execute(new TransactionCallbackNoReturn() {
+            @Override
+            public void doInTransactionWithoutResult(TransactionStatus status) {
+                StorageNetworkIpRangeVO range = null;
+                try {
+                    range = _sNwIpRangeDao.acquireInLockTable(rangeId);
+                    if (range == null) {
+                        String msg = "Unable to acquire lock on storage network ip range id=" + rangeId + ", delete failed";
+                        s_logger.warn(msg);
+                        throw new CloudRuntimeException(msg);
+                    }
+                    /*
+                     * entries in op_dc_storage_network_ip_address will be deleted automatically due to
+                     * fk_storage_ip_address__range_id constraint key
+                     */
+                    _sNwIpRangeDao.remove(rangeId);
+                } finally {
+                    if (range != null) {
+                        _sNwIpRangeDao.releaseFromLockTable(rangeId);
+                    }
+                }
             }
-        }
-        txn.commit();
+        });
+
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f62e28c1/server/src/com/cloud/network/as/AutoScaleManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/as/AutoScaleManagerImpl.java b/server/src/com/cloud/network/as/AutoScaleManagerImpl.java
index 859211b..58a90bb 100644
--- a/server/src/com/cloud/network/as/AutoScaleManagerImpl.java
+++ b/server/src/com/cloud/network/as/AutoScaleManagerImpl.java
@@ -95,8 +95,10 @@ import com.cloud.utils.db.GenericDao;
 import com.cloud.utils.db.JoinBuilder;
 import com.cloud.utils.db.SearchBuilder;
 import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.db.TransactionCallback;
 import com.cloud.utils.db.SearchCriteria.Op;
 import com.cloud.utils.db.Transaction;
+import com.cloud.utils.db.TransactionStatus;
 import com.cloud.utils.net.NetUtils;
 
 @Local(value = { AutoScaleService.class, AutoScaleManager.class })
@@ -428,9 +430,9 @@ public class AutoScaleManagerImpl<Type> extends ManagerBase implements AutoScale
     }
 
     @DB
-    protected AutoScalePolicyVO checkValidityAndPersist(AutoScalePolicyVO autoScalePolicyVO, List<Long> conditionIds) {
-        int duration = autoScalePolicyVO.getDuration();
-        int quietTime = autoScalePolicyVO.getQuietTime();
+    protected AutoScalePolicyVO checkValidityAndPersist(final AutoScalePolicyVO autoScalePolicyVOFinal, final List<Long> conditionIds) {
+        final int duration = autoScalePolicyVOFinal.getDuration();
+        final int quietTime = autoScalePolicyVOFinal.getQuietTime();
 
         if (duration < 0) {
             throw new InvalidParameterValueException("duration is an invalid value: " + duration);
@@ -440,48 +442,49 @@ public class AutoScaleManagerImpl<Type> extends ManagerBase implements AutoScale
             throw new InvalidParameterValueException("quiettime is an invalid value: " + quietTime);
         }
 
-        final Transaction txn = Transaction.currentTxn();
-        txn.start();
-
-        autoScalePolicyVO = _autoScalePolicyDao.persist(autoScalePolicyVO);
-
-        if (conditionIds != null) {
-            SearchBuilder<ConditionVO> conditionsSearch = _conditionDao.createSearchBuilder();
-            conditionsSearch.and("ids", conditionsSearch.entity().getId(), Op.IN);
-            conditionsSearch.done();
-            SearchCriteria<ConditionVO> sc = conditionsSearch.create();
-
-            sc.setParameters("ids", conditionIds.toArray(new Object[0]));
-            List<ConditionVO> conditions = _conditionDao.search(sc, null);
-
-            ControlledEntity[] sameOwnerEntities = conditions.toArray(new ControlledEntity[conditions.size() + 1]);
-            sameOwnerEntities[sameOwnerEntities.length - 1] = autoScalePolicyVO;
-            _accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, true, sameOwnerEntities);
-
-            if (conditionIds.size() != conditions.size()) {
-                // TODO report the condition id which could not be found
-                throw new InvalidParameterValueException("Unable to find the condition specified");
-            }
-
-            ArrayList<Long> counterIds = new ArrayList<Long>();
-            for (ConditionVO condition : conditions) {
-                if (counterIds.contains(condition.getCounterid())) {
-                    throw new InvalidParameterValueException("atleast two conditions in the conditionids have the same counter. It is not right to apply two different conditions for the same counter");
+        return Transaction.execute(new TransactionCallback<AutoScalePolicyVO>() {
+            @Override
+            public AutoScalePolicyVO doInTransaction(TransactionStatus status) {
+                AutoScalePolicyVO autoScalePolicyVO = _autoScalePolicyDao.persist(autoScalePolicyVOFinal);
+        
+                if (conditionIds != null) {
+                    SearchBuilder<ConditionVO> conditionsSearch = _conditionDao.createSearchBuilder();
+                    conditionsSearch.and("ids", conditionsSearch.entity().getId(), Op.IN);
+                    conditionsSearch.done();
+                    SearchCriteria<ConditionVO> sc = conditionsSearch.create();
+        
+                    sc.setParameters("ids", conditionIds.toArray(new Object[0]));
+                    List<ConditionVO> conditions = _conditionDao.search(sc, null);
+        
+                    ControlledEntity[] sameOwnerEntities = conditions.toArray(new ControlledEntity[conditions.size() + 1]);
+                    sameOwnerEntities[sameOwnerEntities.length - 1] = autoScalePolicyVO;
+                    _accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, true, sameOwnerEntities);
+        
+                    if (conditionIds.size() != conditions.size()) {
+                        // TODO report the condition id which could not be found
+                        throw new InvalidParameterValueException("Unable to find the condition specified");
+                    }
+        
+                    ArrayList<Long> counterIds = new ArrayList<Long>();
+                    for (ConditionVO condition : conditions) {
+                        if (counterIds.contains(condition.getCounterid())) {
+                            throw new InvalidParameterValueException("atleast two conditions in the conditionids have the same counter. It is not right to apply two different conditions for the same counter");
+                        }
+                        counterIds.add(condition.getCounterid());
+                    }
+        
+                    /* For update case remove the existing mappings and create fresh ones */
+                    _autoScalePolicyConditionMapDao.removeByAutoScalePolicyId(autoScalePolicyVO.getId());
+        
+                    for (Long conditionId : conditionIds) {
+                        AutoScalePolicyConditionMapVO policyConditionMapVO = new AutoScalePolicyConditionMapVO(autoScalePolicyVO.getId(), conditionId);
+                        _autoScalePolicyConditionMapDao.persist(policyConditionMapVO);
+                    }
                 }
-                counterIds.add(condition.getCounterid());
-            }
 
-            /* For update case remove the existing mappings and create fresh ones */
-            _autoScalePolicyConditionMapDao.removeByAutoScalePolicyId(autoScalePolicyVO.getId());
-
-            for (Long conditionId : conditionIds) {
-                AutoScalePolicyConditionMapVO policyConditionMapVO = new AutoScalePolicyConditionMapVO(autoScalePolicyVO.getId(), conditionId);
-                _autoScalePolicyConditionMapDao.persist(policyConditionMapVO);
+                return autoScalePolicyVO;
             }
-        }
-
-        txn.commit();
-        return autoScalePolicyVO;
+        });
     }
 
     @Override
@@ -511,7 +514,7 @@ public class AutoScaleManagerImpl<Type> extends ManagerBase implements AutoScale
     @Override
     @DB
     @ActionEvent(eventType = EventTypes.EVENT_AUTOSCALEPOLICY_DELETE, eventDescription = "deleting autoscale policy")
-    public boolean deleteAutoScalePolicy(long id) {
+    public boolean deleteAutoScalePolicy(final long id) {
         /* Check if entity is in database */
         getEntityInDatabase(CallContext.current().getCallingAccount(), "AutoScale Policy", id, _autoScalePolicyDao);
 
@@ -519,23 +522,25 @@ public class AutoScaleManagerImpl<Type> extends ManagerBase implements AutoScale
             throw new InvalidParameterValueException("Cannot delete AutoScale Policy when it is in use by one or more AutoScale Vm Groups");
         }
 
-        Transaction txn = Transaction.currentTxn();
-        txn.start();
+        return Transaction.execute(new TransactionCallback<Boolean>() {
+            @Override
+            public Boolean doInTransaction(TransactionStatus status) {
+                boolean success = true;
+                success = _autoScalePolicyDao.remove(id);
+                if (!success) {
+                    s_logger.warn("Failed to remove AutoScale Policy db object");
+                    return false;
+                }
+                success = _autoScalePolicyConditionMapDao.removeByAutoScalePolicyId(id);
+                if (!success) {
+                    s_logger.warn("Failed to remove AutoScale Policy Condition mappings");
+                    return false;
+                }
+                s_logger.info("Successfully deleted autoscale policy id : " + id);
 
-        boolean success = true;
-        success = _autoScalePolicyDao.remove(id);
-        if (!success) {
-            s_logger.warn("Failed to remove AutoScale Policy db object");
-            return false;
-        }
-        success = _autoScalePolicyConditionMapDao.removeByAutoScalePolicyId(id);
-        if (!success) {
-            s_logger.warn("Failed to remove AutoScale Policy Condition mappings");
-            return false;
-        }
-        txn.commit();
-        s_logger.info("Successfully deleted autoscale policy id : " + id);
-        return true; // successful
+                return success;
+            }
+        });
     }
 
     public void checkCallerAccess(String accountName, Long domainId)
@@ -745,7 +750,7 @@ public class AutoScaleManagerImpl<Type> extends ManagerBase implements AutoScale
     @Override
     @DB
     @ActionEvent(eventType = EventTypes.EVENT_AUTOSCALEVMGROUP_DELETE, eventDescription = "deleting autoscale vm group")
-    public boolean deleteAutoScaleVmGroup(long id) {
+    public boolean deleteAutoScaleVmGroup(final long id) {
         AutoScaleVmGroupVO autoScaleVmGroupVO = getEntityInDatabase(CallContext.current().getCallingAccount(), "AutoScale Vm Group", id, _autoScaleVmGroupDao);
 
         if (autoScaleVmGroupVO.getState().equals(AutoScaleVmGroup.State_New)) {
@@ -769,24 +774,27 @@ public class AutoScaleManagerImpl<Type> extends ManagerBase implements AutoScale
             }
         }
 
-        Transaction txn = Transaction.currentTxn();
-        txn.start();
-        success = _autoScaleVmGroupDao.remove(id);
-
-        if (!success) {
-            s_logger.warn("Failed to remove AutoScale Group db object");
-            return false;
-        }
-
-        success = _autoScaleVmGroupPolicyMapDao.removeByGroupId(id);
-        if (!success) {
-            s_logger.warn("Failed to remove AutoScale Group Policy mappings");
-            return false;
-        }
+        return Transaction.execute(new TransactionCallback<Boolean>() {
+            @Override
+            public Boolean doInTransaction(TransactionStatus status) {
+                boolean success = _autoScaleVmGroupDao.remove(id);
+        
+                if (!success) {
+                    s_logger.warn("Failed to remove AutoScale Group db object");
+                    return false;
+                }
+        
+                success = _autoScaleVmGroupPolicyMapDao.removeByGroupId(id);
+                if (!success) {
+                    s_logger.warn("Failed to remove AutoScale Group Policy mappings");
+                    return false;
+                }
+        
+                s_logger.info("Successfully deleted autoscale vm group id : " + id);
+                return success; // Successfull
+            }
+        });
 
-        txn.commit();
-        s_logger.info("Successfully deleted autoscale vm group id : " + id);
-        return success; // Successfull
     }
 
     @Override
@@ -831,13 +839,13 @@ public class AutoScaleManagerImpl<Type> extends ManagerBase implements AutoScale
     }
 
     @DB
-    protected AutoScaleVmGroupVO checkValidityAndPersist(AutoScaleVmGroupVO vmGroup, List<Long> passedScaleUpPolicyIds, List<Long> passedScaleDownPolicyIds) {
+    protected AutoScaleVmGroupVO checkValidityAndPersist(final AutoScaleVmGroupVO vmGroup, final List<Long> passedScaleUpPolicyIds, final List<Long> passedScaleDownPolicyIds) {
         int minMembers = vmGroup.getMinMembers();
         int maxMembers = vmGroup.getMaxMembers();
         int interval = vmGroup.getInterval();
         List<Counter> counters = new ArrayList<Counter>();
         List<AutoScalePolicyVO> policies = new ArrayList<AutoScalePolicyVO>();
-        List<Long> policyIds = new ArrayList<Long>();
+        final List<Long> policyIds = new ArrayList<Long>();
         List<Long> currentScaleUpPolicyIds = new ArrayList<Long>();
         List<Long> currentScaleDownPolicyIds = new ArrayList<Long>();
         if (vmGroup.getCreated() != null) {
@@ -887,20 +895,23 @@ public class AutoScaleManagerImpl<Type> extends ManagerBase implements AutoScale
         sameOwnerEntities[sameOwnerEntities.length - 1] = profileVO;
         _accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, true, sameOwnerEntities);
 
-        final Transaction txn = Transaction.currentTxn();
-        txn.start();
-        vmGroup = _autoScaleVmGroupDao.persist(vmGroup);
-
-        if (passedScaleUpPolicyIds != null || passedScaleDownPolicyIds != null) {
-            _autoScaleVmGroupPolicyMapDao.removeByGroupId(vmGroup.getId());
+        return Transaction.execute(new TransactionCallback<AutoScaleVmGroupVO>() {
+            @Override
+            public AutoScaleVmGroupVO doInTransaction(TransactionStatus status) {
+                AutoScaleVmGroupVO vmGroupNew = _autoScaleVmGroupDao.persist(vmGroup);
+        
+                if (passedScaleUpPolicyIds != null || passedScaleDownPolicyIds != null) {
+                    _autoScaleVmGroupPolicyMapDao.removeByGroupId(vmGroupNew.getId());
+        
+                    for (Long policyId : policyIds) {
+                        _autoScaleVmGroupPolicyMapDao.persist(new AutoScaleVmGroupPolicyMapVO(vmGroupNew.getId(), policyId));
+                    }
+                }
 
-            for (Long policyId : policyIds) {
-                _autoScaleVmGroupPolicyMapDao.persist(new AutoScaleVmGroupPolicyMapVO(vmGroup.getId(), policyId));
+                return vmGroupNew;
             }
-        }
-        txn.commit();
+        });
 
-        return vmGroup;
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f62e28c1/server/src/com/cloud/network/firewall/FirewallManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/firewall/FirewallManagerImpl.java b/server/src/com/cloud/network/firewall/FirewallManagerImpl.java
index c86476e..e0d23ad 100644
--- a/server/src/com/cloud/network/firewall/FirewallManagerImpl.java
+++ b/server/src/com/cloud/network/firewall/FirewallManagerImpl.java
@@ -29,7 +29,6 @@ import javax.naming.ConfigurationException;
 
 import org.apache.log4j.Logger;
 import org.springframework.stereotype.Component;
-
 import org.apache.cloudstack.api.command.user.firewall.ListEgressFirewallRulesCmd;
 import org.apache.cloudstack.api.command.user.firewall.ListFirewallRulesCmd;
 import org.apache.cloudstack.context.CallContext;
@@ -89,6 +88,9 @@ import com.cloud.utils.db.Filter;
 import com.cloud.utils.db.JoinBuilder;
 import com.cloud.utils.db.SearchBuilder;
 import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.db.TransactionCallbackNoReturn;
+import com.cloud.utils.db.TransactionCallbackWithException;
+import com.cloud.utils.db.TransactionStatus;
 import com.cloud.utils.db.SearchCriteria.Op;
 import com.cloud.utils.db.Transaction;
 import com.cloud.utils.exception.CloudRuntimeException;
@@ -181,9 +183,9 @@ public class FirewallManagerImpl extends ManagerBase implements FirewallService,
     }
 
     @DB
-    protected FirewallRule createFirewallRule(Long ipAddrId, Account caller, String xId, Integer portStart,
-            Integer portEnd, String protocol, List<String> sourceCidrList, Integer icmpCode, Integer icmpType,
-            Long relatedRuleId, FirewallRule.FirewallRuleType type, Long networkId, FirewallRule.TrafficType trafficType) throws NetworkRuleConflictException {
+    protected FirewallRule createFirewallRule(final Long ipAddrId, Account caller, final String xId, final Integer portStart,
+            final Integer portEnd, final String protocol, final List<String> sourceCidrList, final Integer icmpCode, final Integer icmpType,
+            final Long relatedRuleId, final FirewallRule.FirewallRuleType type, final Long networkId, final FirewallRule.TrafficType trafficType) throws NetworkRuleConflictException {
 
         IPAddressVO ipAddress = null;
         if (ipAddrId != null){
@@ -222,25 +224,27 @@ public class FirewallManagerImpl extends ManagerBase implements FirewallService,
                 domainId = network.getDomainId();
         }
 
-        Transaction txn = Transaction.currentTxn();
-        txn.start();
-
-        FirewallRuleVO newRule = new FirewallRuleVO(xId, ipAddrId, portStart, portEnd, protocol.toLowerCase(), networkId,
-                accountId, domainId, Purpose.Firewall, sourceCidrList, icmpCode, icmpType, relatedRuleId, trafficType);
-        newRule.setType(type);
-        newRule = _firewallDao.persist(newRule);
-
-        if (type == FirewallRuleType.User)
-            detectRulesConflict(newRule);
-
-        if (!_firewallDao.setStateToAdd(newRule)) {
-            throw new CloudRuntimeException("Unable to update the state to add for " + newRule);
-        }
-        CallContext.current().setEventDetails("Rule Id: " + newRule.getId());
-
-        txn.commit();
-
-        return newRule;
+        final Long accountIdFinal = accountId;
+        final Long domainIdFinal = domainId;
+        return Transaction.executeWithException(new TransactionCallbackWithException<FirewallRuleVO>() {
+            @Override
+            public FirewallRuleVO doInTransaction(TransactionStatus status) throws NetworkRuleConflictException {
+                FirewallRuleVO newRule = new FirewallRuleVO(xId, ipAddrId, portStart, portEnd, protocol.toLowerCase(), networkId,
+                        accountIdFinal, domainIdFinal, Purpose.Firewall, sourceCidrList, icmpCode, icmpType, relatedRuleId, trafficType);
+                newRule.setType(type);
+                newRule = _firewallDao.persist(newRule);
+        
+                if (type == FirewallRuleType.User)
+                    detectRulesConflict(newRule);
+        
+                if (!_firewallDao.setStateToAdd(newRule)) {
+                    throw new CloudRuntimeException("Unable to update the state to add for " + newRule);
+                }
+                CallContext.current().setEventDetails("Rule Id: " + newRule.getId());
+                
+                return newRule;
+            }
+        }, NetworkRuleConflictException.class);
     }
 
     @Override
@@ -724,33 +728,34 @@ public class FirewallManagerImpl extends ManagerBase implements FirewallService,
 
     @Override
     @DB
-    public void revokeRule(FirewallRuleVO rule, Account caller, long userId, boolean needUsageEvent) {
+    public void revokeRule(final FirewallRuleVO rule, Account caller, long userId, final boolean needUsageEvent) {
         if (caller != null) {
             _accountMgr.checkAccess(caller, null, true, rule);
         }
 
-        Transaction txn = Transaction.currentTxn();
-        boolean generateUsageEvent = false;
-
-        txn.start();
-        if (rule.getState() == State.Staged) {
-            if (s_logger.isDebugEnabled()) {
-                s_logger.debug("Found a rule that is still in stage state so just removing it: " + rule);
-            }
-            removeRule(rule);
-            generateUsageEvent = true;
-        } else if (rule.getState() == State.Add || rule.getState() == State.Active) {
-            rule.setState(State.Revoke);
-            _firewallDao.update(rule.getId(), rule);
-            generateUsageEvent = true;
-        }
+        Transaction.execute(new TransactionCallbackNoReturn() {
+            @Override
+            public void doInTransactionWithoutResult(TransactionStatus status) {
+                boolean generateUsageEvent = false;
 
-        if (generateUsageEvent && needUsageEvent) {
-            UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_RULE_DELETE, rule.getAccountId(), 0, rule.getId(),
-                    null, rule.getClass().getName(), rule.getUuid());
-        }
+                if (rule.getState() == State.Staged) {
+                    if (s_logger.isDebugEnabled()) {
+                        s_logger.debug("Found a rule that is still in stage state so just removing it: " + rule);
+                    }
+                    removeRule(rule);
+                    generateUsageEvent = true;
+                } else if (rule.getState() == State.Add || rule.getState() == State.Active) {
+                    rule.setState(State.Revoke);
+                    _firewallDao.update(rule.getId(), rule);
+                    generateUsageEvent = true;
+                }
 
-        txn.commit();
+                if (generateUsageEvent && needUsageEvent) {
+                    UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_RULE_DELETE, rule.getAccountId(), 0, rule.getId(),
+                            null, rule.getClass().getName(), rule.getUuid());
+                }
+            }
+        });
     }
 
     @Override


Mime
View raw message