You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by sa...@apache.org on 2014/07/04 13:11:34 UTC

git commit: updated refs/heads/master to a600d84

Repository: cloudstack
Updated Branches:
  refs/heads/master 4607c2694 -> a600d8408


Fixed Resource Leaks, null dereferences, few other issues reported by coverity


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/a600d840
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/a600d840
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/a600d840

Branch: refs/heads/master
Commit: a600d8408ea86782318139c17cf346c8497943d0
Parents: 4607c26
Author: Santhosh Edukulla <sa...@gmail.com>
Authored: Wed Jul 2 14:08:16 2014 +0530
Committer: Santhosh Edukulla <sa...@gmail.com>
Committed: Fri Jul 4 16:17:58 2014 +0530

----------------------------------------------------------------------
 .../src/com/cloud/dc/dao/VlanDaoImpl.java       |  30 ++--
 .../storage/dao/StoragePoolWorkDaoImpl.java     |  26 ++-
 .../src/com/cloud/upgrade/DatabaseCreator.java  |   4 +-
 .../cloud/usage/dao/UsageIPAddressDaoImpl.java  |  21 ++-
 .../dao/UsageLoadBalancerPolicyDaoImpl.java     |  20 ++-
 .../usage/dao/UsageNetworkOfferingDaoImpl.java  |  22 ++-
 .../dao/UsagePortForwardingRuleDaoImpl.java     |  20 ++-
 .../usage/dao/UsageSecurityGroupDaoImpl.java    |  75 +++++----
 .../cloud/usage/dao/UsageStorageDaoImpl.java    | 102 ++++++------
 .../cloud/usage/dao/UsageVPNUserDaoImpl.java    |  20 ++-
 .../com/cloud/usage/dao/UsageVolumeDaoImpl.java |  19 ++-
 .../datastore/db/PrimaryDataStoreDaoImpl.java   |  70 ++++----
 .../allocator/StorageCacheRandomAllocator.java  |   2 +-
 .../datastore/PrimaryDataStoreHelper.java       |   8 +-
 .../dao/ManagementServerHostDaoImpl.java        | 164 ++++++++++---------
 framework/db/src/com/cloud/utils/db/DbUtil.java |   2 +-
 .../src/com/cloud/utils/db/GenericDaoBase.java  |   4 +-
 .../kvm/resource/BridgeVifDriver.java           |   4 +
 .../SamplePrimaryDataStoreLifeCycleImpl.java    |   1 -
 .../com/cloud/storage/VolumeApiServiceImpl.java |   2 +-
 .../cloud/tags/TaggedResourceManagerImpl.java   |   2 +-
 .../vm/snapshot/VMSnapshotManagerImpl.java      |   2 +-
 22 files changed, 340 insertions(+), 280 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a600d840/engine/schema/src/com/cloud/dc/dao/VlanDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/dc/dao/VlanDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/VlanDaoImpl.java
index a38a96e..1003053 100755
--- a/engine/schema/src/com/cloud/dc/dao/VlanDaoImpl.java
+++ b/engine/schema/src/com/cloud/dc/dao/VlanDaoImpl.java
@@ -301,27 +301,25 @@ public class VlanDaoImpl extends GenericDaoBase<VlanVO, Long> implements VlanDao
     @Override
     @DB
     public List<VlanVO> searchForZoneWideVlans(long dcId, String vlanType, String vlanId) {
-
         StringBuilder sql = new StringBuilder(FindZoneWideVlans);
-
         TransactionLegacy txn = TransactionLegacy.currentTxn();
-        PreparedStatement pstmt = null;
-        try {
-            pstmt = txn.prepareAutoCloseStatement(sql.toString());
-            pstmt.setLong(1, dcId);
-            pstmt.setString(2, vlanType);
-            pstmt.setString(3, vlanId);
-
-            ResultSet rs = pstmt.executeQuery();
-            List<VlanVO> zoneWideVlans = new ArrayList<VlanVO>();
-
-            while (rs.next()) {
-                zoneWideVlans.add(toEntityBean(rs, false));
+        List<VlanVO> zoneWideVlans = new ArrayList<VlanVO>();
+        try(PreparedStatement pstmt = txn.prepareStatement(sql.toString());){
+            if(pstmt != null) {
+                pstmt.setLong(1, dcId);
+                pstmt.setString(2, vlanType);
+                pstmt.setString(3, vlanId);
+                try(ResultSet rs = pstmt.executeQuery();) {
+                    while (rs.next()) {
+                        zoneWideVlans.add(toEntityBean(rs, false));
+                    }
+                }catch (SQLException e) {
+                    throw new CloudRuntimeException("searchForZoneWideVlans:Exception:" + e.getMessage(), e);
+                }
             }
-
             return zoneWideVlans;
         } catch (SQLException e) {
-            throw new CloudRuntimeException("Unable to execute " + pstmt.toString(), e);
+            throw new CloudRuntimeException("searchForZoneWideVlans:Exception:" + e.getMessage(), e);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a600d840/engine/schema/src/com/cloud/storage/dao/StoragePoolWorkDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/storage/dao/StoragePoolWorkDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/StoragePoolWorkDaoImpl.java
index c6e1b74..99e96a5 100644
--- a/engine/schema/src/com/cloud/storage/dao/StoragePoolWorkDaoImpl.java
+++ b/engine/schema/src/com/cloud/storage/dao/StoragePoolWorkDaoImpl.java
@@ -116,25 +116,23 @@ public class StoragePoolWorkDaoImpl extends GenericDaoBase<StoragePoolWorkVO, Lo
     @Override
     @DB
     public List<Long> searchForPoolIdsForPendingWorkJobs(long msId) {
-
         StringBuilder sql = new StringBuilder(FindPoolIds);
-
         TransactionLegacy txn = TransactionLegacy.currentTxn();
-        PreparedStatement pstmt = null;
-        try {
-            pstmt = txn.prepareAutoCloseStatement(sql.toString());
-            pstmt.setLong(1, msId);
-
-            ResultSet rs = pstmt.executeQuery();
-            List<Long> poolIds = new ArrayList<Long>();
-
-            while (rs.next()) {
-                poolIds.add(rs.getLong("pool_id"));
+        List<Long> poolIds = new ArrayList<Long>();
+        try (PreparedStatement  pstmt = txn.prepareStatement(sql.toString());){
+            if(pstmt != null) {
+                pstmt.setLong(1, msId);
+                try (ResultSet rs = pstmt.executeQuery();) {
+                    while (rs.next()) {
+                        poolIds.add(rs.getLong("pool_id"));
+                    }
+                } catch (SQLException e) {
+                    throw new CloudRuntimeException("searchForPoolIdsForPendingWorkJobs:Exception:" + e.getMessage(), e);
+                }
             }
             return poolIds;
         } catch (SQLException e) {
-            throw new CloudRuntimeException("Unable to execute " + pstmt.toString(), e);
+            throw new CloudRuntimeException("searchForPoolIdsForPendingWorkJobs:Exception:" + e.getMessage(), e);
         }
-
     }
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a600d840/engine/schema/src/com/cloud/upgrade/DatabaseCreator.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/upgrade/DatabaseCreator.java b/engine/schema/src/com/cloud/upgrade/DatabaseCreator.java
index b04607d..2509d69 100755
--- a/engine/schema/src/com/cloud/upgrade/DatabaseCreator.java
+++ b/engine/schema/src/com/cloud/upgrade/DatabaseCreator.java
@@ -86,8 +86,8 @@ public class DatabaseCreator {
 
     private static void initDB(String dbPropsFile, String rootPassword, String[] databases, boolean dryRun) {
         Properties dbProperties = new Properties();
-        try {
-            dbProperties.load(new FileInputStream(new File(dbPropsFile)));
+        try(FileInputStream f_stream = new FileInputStream(new File(dbPropsFile));) {
+            dbProperties.load(f_stream);
         } catch (IOException e) {
             System.out.println("IOError: unable to load/read db properties file: " + e);
             System.exit(1);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a600d840/engine/schema/src/com/cloud/usage/dao/UsageIPAddressDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/usage/dao/UsageIPAddressDaoImpl.java b/engine/schema/src/com/cloud/usage/dao/UsageIPAddressDaoImpl.java
index 17d5dc8..358e638 100644
--- a/engine/schema/src/com/cloud/usage/dao/UsageIPAddressDaoImpl.java
+++ b/engine/schema/src/com/cloud/usage/dao/UsageIPAddressDaoImpl.java
@@ -18,6 +18,7 @@ package com.cloud.usage.dao;
 
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
+import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
@@ -25,6 +26,7 @@ import java.util.TimeZone;
 
 import javax.ejb.Local;
 
+import com.cloud.exception.CloudException;
 import org.apache.log4j.Logger;
 import org.springframework.stereotype.Component;
 
@@ -57,20 +59,25 @@ public class UsageIPAddressDaoImpl extends GenericDaoBase<UsageIPAddressVO, Long
     @Override
     public void update(UsageIPAddressVO usage) {
         TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
-        PreparedStatement pstmt = null;
         try {
             txn.start();
             if (usage.getReleased() != null) {
-                pstmt = txn.prepareAutoCloseStatement(UPDATE_RELEASED);
-                pstmt.setString(1, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), usage.getReleased()));
-                pstmt.setLong(2, usage.getAccountId());
-                pstmt.setString(3, usage.getAddress());
+                try(PreparedStatement pstmt = txn.prepareStatement(UPDATE_RELEASED);) {
+                    if (pstmt != null) {
+                        pstmt.setString(1, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), usage.getReleased()));
+                        pstmt.setLong(2, usage.getAccountId());
+                        pstmt.setString(3, usage.getAddress());
+                        pstmt.executeUpdate();
+                    }
+                }catch(SQLException e)
+                {
+                   throw new CloudException("update:Exception:"+e.getMessage(), e);
+                }
             }
-            pstmt.executeUpdate();
             txn.commit();
         } catch (Exception e) {
             txn.rollback();
-            s_logger.warn("Error updating usageIPAddressVO", e);
+            s_logger.error("Error updating usageIPAddressVO:"+e.getMessage(), e);
         } finally {
             txn.close();
         }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a600d840/engine/schema/src/com/cloud/usage/dao/UsageLoadBalancerPolicyDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/usage/dao/UsageLoadBalancerPolicyDaoImpl.java b/engine/schema/src/com/cloud/usage/dao/UsageLoadBalancerPolicyDaoImpl.java
index c868ac0..abace04 100644
--- a/engine/schema/src/com/cloud/usage/dao/UsageLoadBalancerPolicyDaoImpl.java
+++ b/engine/schema/src/com/cloud/usage/dao/UsageLoadBalancerPolicyDaoImpl.java
@@ -18,6 +18,7 @@ package com.cloud.usage.dao;
 
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
+import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
@@ -25,6 +26,7 @@ import java.util.TimeZone;
 
 import javax.ejb.Local;
 
+import com.cloud.exception.CloudException;
 import org.apache.log4j.Logger;
 import org.springframework.stereotype.Component;
 
@@ -73,20 +75,24 @@ public class UsageLoadBalancerPolicyDaoImpl extends GenericDaoBase<UsageLoadBala
     @Override
     public void update(UsageLoadBalancerPolicyVO usage) {
         TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
-        PreparedStatement pstmt = null;
         try {
             txn.start();
             if (usage.getDeleted() != null) {
-                pstmt = txn.prepareAutoCloseStatement(UPDATE_DELETED);
-                pstmt.setString(1, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), usage.getDeleted()));
-                pstmt.setLong(2, usage.getAccountId());
-                pstmt.setLong(3, usage.getId());
+                try(PreparedStatement pstmt = txn.prepareStatement(UPDATE_DELETED);) {
+                    if (pstmt != null) {
+                        pstmt.setString(1, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), usage.getDeleted()));
+                        pstmt.setLong(2, usage.getAccountId());
+                        pstmt.setLong(3, usage.getId());
+                        pstmt.executeUpdate();
+                    }
+                }catch (SQLException e) {
+                    throw new CloudException("Error updating UsageLoadBalancerPolicyVO:" + e.getMessage(), e);
+                }
             }
-            pstmt.executeUpdate();
             txn.commit();
         } catch (Exception e) {
             txn.rollback();
-            s_logger.warn("Error updating UsageLoadBalancerPolicyVO", e);
+            s_logger.warn("Error updating UsageLoadBalancerPolicyVO"+e.getMessage(), e);
         } finally {
             txn.close();
         }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a600d840/engine/schema/src/com/cloud/usage/dao/UsageNetworkOfferingDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/usage/dao/UsageNetworkOfferingDaoImpl.java b/engine/schema/src/com/cloud/usage/dao/UsageNetworkOfferingDaoImpl.java
index f27fd60..2661d4e 100644
--- a/engine/schema/src/com/cloud/usage/dao/UsageNetworkOfferingDaoImpl.java
+++ b/engine/schema/src/com/cloud/usage/dao/UsageNetworkOfferingDaoImpl.java
@@ -18,6 +18,7 @@ package com.cloud.usage.dao;
 
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
+import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
@@ -25,6 +26,7 @@ import java.util.TimeZone;
 
 import javax.ejb.Local;
 
+import com.cloud.exception.CloudException;
 import org.apache.log4j.Logger;
 import org.springframework.stereotype.Component;
 
@@ -56,21 +58,25 @@ public class UsageNetworkOfferingDaoImpl extends GenericDaoBase<UsageNetworkOffe
     @Override
     public void update(UsageNetworkOfferingVO usage) {
         TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
-        PreparedStatement pstmt = null;
         try {
             txn.start();
             if (usage.getDeleted() != null) {
-                pstmt = txn.prepareAutoCloseStatement(UPDATE_DELETED);
-                pstmt.setString(1, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), usage.getDeleted()));
-                pstmt.setLong(2, usage.getAccountId());
-                pstmt.setLong(3, usage.getVmInstanceId());
-                pstmt.setLong(4, usage.getNetworkOfferingId());
+                try(PreparedStatement pstmt = txn.prepareStatement(UPDATE_DELETED);) {
+                    if (pstmt != null) {
+                        pstmt.setString(1, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), usage.getDeleted()));
+                        pstmt.setLong(2, usage.getAccountId());
+                        pstmt.setLong(3, usage.getVmInstanceId());
+                        pstmt.setLong(4, usage.getNetworkOfferingId());
+                        pstmt.executeUpdate();
+                    }
+                  }catch (SQLException e) {
+                    throw new CloudException("Error updating UsageNetworkOfferingVO:"+e.getMessage(), e);
+                }
             }
-            pstmt.executeUpdate();
             txn.commit();
         } catch (Exception e) {
             txn.rollback();
-            s_logger.warn("Error updating UsageNetworkOfferingVO", e);
+            s_logger.warn("Error updating UsageNetworkOfferingVO:"+e.getMessage(), e);
         } finally {
             txn.close();
         }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a600d840/engine/schema/src/com/cloud/usage/dao/UsagePortForwardingRuleDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/usage/dao/UsagePortForwardingRuleDaoImpl.java b/engine/schema/src/com/cloud/usage/dao/UsagePortForwardingRuleDaoImpl.java
index 803288a..d2053e8 100644
--- a/engine/schema/src/com/cloud/usage/dao/UsagePortForwardingRuleDaoImpl.java
+++ b/engine/schema/src/com/cloud/usage/dao/UsagePortForwardingRuleDaoImpl.java
@@ -18,6 +18,7 @@ package com.cloud.usage.dao;
 
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
+import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
@@ -25,6 +26,7 @@ import java.util.TimeZone;
 
 import javax.ejb.Local;
 
+import com.cloud.exception.CloudException;
 import org.apache.log4j.Logger;
 import org.springframework.stereotype.Component;
 
@@ -73,20 +75,24 @@ public class UsagePortForwardingRuleDaoImpl extends GenericDaoBase<UsagePortForw
     @Override
     public void update(UsagePortForwardingRuleVO usage) {
         TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
-        PreparedStatement pstmt = null;
         try {
             txn.start();
             if (usage.getDeleted() != null) {
-                pstmt = txn.prepareAutoCloseStatement(UPDATE_DELETED);
-                pstmt.setString(1, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), usage.getDeleted()));
-                pstmt.setLong(2, usage.getAccountId());
-                pstmt.setLong(3, usage.getId());
+                try(PreparedStatement pstmt = txn.prepareStatement(UPDATE_DELETED);) {
+                    if (pstmt != null) {
+                        pstmt.setString(1, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), usage.getDeleted()));
+                        pstmt.setLong(2, usage.getAccountId());
+                        pstmt.setLong(3, usage.getId());
+                        pstmt.executeUpdate();
+                    }
+                }catch (SQLException e) {
+                    throw new CloudException("Error updating UsagePortForwardingRuleVO:"+e.getMessage(), e);
+                }
             }
-            pstmt.executeUpdate();
             txn.commit();
         } catch (Exception e) {
             txn.rollback();
-            s_logger.warn("Error updating UsagePortForwardingRuleVO", e);
+            s_logger.warn("Error updating UsagePortForwardingRuleVO:"+e.getMessage(), e);
         } finally {
             txn.close();
         }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a600d840/engine/schema/src/com/cloud/usage/dao/UsageSecurityGroupDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/usage/dao/UsageSecurityGroupDaoImpl.java b/engine/schema/src/com/cloud/usage/dao/UsageSecurityGroupDaoImpl.java
index 0fc2ce0..2305018 100644
--- a/engine/schema/src/com/cloud/usage/dao/UsageSecurityGroupDaoImpl.java
+++ b/engine/schema/src/com/cloud/usage/dao/UsageSecurityGroupDaoImpl.java
@@ -18,6 +18,7 @@ package com.cloud.usage.dao;
 
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
+import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
@@ -25,6 +26,7 @@ import java.util.TimeZone;
 
 import javax.ejb.Local;
 
+import com.cloud.exception.CloudException;
 import org.apache.log4j.Logger;
 import org.springframework.stereotype.Component;
 
@@ -56,21 +58,25 @@ public class UsageSecurityGroupDaoImpl extends GenericDaoBase<UsageSecurityGroup
     @Override
     public void update(UsageSecurityGroupVO usage) {
         TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
-        PreparedStatement pstmt = null;
         try {
             txn.start();
             if (usage.getDeleted() != null) {
-                pstmt = txn.prepareAutoCloseStatement(UPDATE_DELETED);
-                pstmt.setString(1, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), usage.getDeleted()));
-                pstmt.setLong(2, usage.getAccountId());
-                pstmt.setLong(3, usage.getVmInstanceId());
-                pstmt.setLong(4, usage.getSecurityGroupId());
+                try(PreparedStatement pstmt = txn.prepareStatement(UPDATE_DELETED);) {
+                    if (pstmt != null) {
+                        pstmt.setString(1, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), usage.getDeleted()));
+                        pstmt.setLong(2, usage.getAccountId());
+                        pstmt.setLong(3, usage.getVmInstanceId());
+                        pstmt.setLong(4, usage.getSecurityGroupId());
+                        pstmt.executeUpdate();
+                    }
+                }catch (SQLException e) {
+                    throw new CloudException("Error updating UsageSecurityGroupVO:"+e.getMessage(), e);
+                }
             }
-            pstmt.executeUpdate();
             txn.commit();
         } catch (Exception e) {
             txn.rollback();
-            s_logger.warn("Error updating UsageSecurityGroupVO", e);
+            s_logger.warn("Error updating UsageSecurityGroupVO:"+e.getMessage(), e);
         } finally {
             txn.close();
         }
@@ -101,11 +107,8 @@ public class UsageSecurityGroupDaoImpl extends GenericDaoBase<UsageSecurityGroup
         }
 
         TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
-        PreparedStatement pstmt = null;
-
-        try {
-            int i = 1;
-            pstmt = txn.prepareAutoCloseStatement(sql);
+        int i = 1;
+        try (PreparedStatement pstmt = txn.prepareStatement(sql);){
             if (param1 != null) {
                 pstmt.setLong(i++, param1);
             }
@@ -115,36 +118,36 @@ public class UsageSecurityGroupDaoImpl extends GenericDaoBase<UsageSecurityGroup
             pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), endDate));
             pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), startDate));
             pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), endDate));
-
-            ResultSet rs = pstmt.executeQuery();
-            while (rs.next()) {
-                //zoneId, account_id, domain_id, vm_instance_id, security_group_id, created, deleted
-                Long zoneId = Long.valueOf(rs.getLong(1));
-                Long acctId = Long.valueOf(rs.getLong(2));
-                Long dId = Long.valueOf(rs.getLong(3));
-                long vmId = Long.valueOf(rs.getLong(4));
-                long sgId = Long.valueOf(rs.getLong(5));
-                Date createdDate = null;
-                Date deletedDate = null;
-                String createdTS = rs.getString(6);
-                String deletedTS = rs.getString(7);
-
-                if (createdTS != null) {
-                    createdDate = DateUtil.parseDateString(s_gmtTimeZone, createdTS);
-                }
-                if (deletedTS != null) {
-                    deletedDate = DateUtil.parseDateString(s_gmtTimeZone, deletedTS);
+            try(ResultSet rs = pstmt.executeQuery();) {
+                while (rs.next()) {
+                    //zoneId, account_id, domain_id, vm_instance_id, security_group_id, created, deleted
+                    Long zoneId = Long.valueOf(rs.getLong(1));
+                    Long acctId = Long.valueOf(rs.getLong(2));
+                    Long dId = Long.valueOf(rs.getLong(3));
+                    long vmId = Long.valueOf(rs.getLong(4));
+                    long sgId = Long.valueOf(rs.getLong(5));
+                    Date createdDate = null;
+                    Date deletedDate = null;
+                    String createdTS = rs.getString(6);
+                    String deletedTS = rs.getString(7);
+
+                    if (createdTS != null) {
+                        createdDate = DateUtil.parseDateString(s_gmtTimeZone, createdTS);
+                    }
+                    if (deletedTS != null) {
+                        deletedDate = DateUtil.parseDateString(s_gmtTimeZone, deletedTS);
+                    }
+                    usageRecords.add(new UsageSecurityGroupVO(zoneId, acctId, dId, vmId, sgId, createdDate, deletedDate));
                 }
-
-                usageRecords.add(new UsageSecurityGroupVO(zoneId, acctId, dId, vmId, sgId, createdDate, deletedDate));
+            }catch (SQLException e) {
+                throw new CloudException("Error getting usage records"+e.getMessage(), e);
             }
         } catch (Exception e) {
             txn.rollback();
-            s_logger.warn("Error getting usage records", e);
+            s_logger.warn("Error getting usage records:"+e.getMessage(), e);
         } finally {
             txn.close();
         }
-
         return usageRecords;
     }
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a600d840/engine/schema/src/com/cloud/usage/dao/UsageStorageDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/usage/dao/UsageStorageDaoImpl.java b/engine/schema/src/com/cloud/usage/dao/UsageStorageDaoImpl.java
index 77fc56f..2252275 100644
--- a/engine/schema/src/com/cloud/usage/dao/UsageStorageDaoImpl.java
+++ b/engine/schema/src/com/cloud/usage/dao/UsageStorageDaoImpl.java
@@ -18,6 +18,7 @@ package com.cloud.usage.dao;
 
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
+import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
@@ -25,6 +26,7 @@ import java.util.TimeZone;
 
 import javax.ejb.Local;
 
+import com.cloud.exception.CloudException;
 import org.apache.log4j.Logger;
 import org.springframework.stereotype.Component;
 
@@ -91,19 +93,22 @@ public class UsageStorageDaoImpl extends GenericDaoBase<UsageStorageVO, Long> im
     @Override
     public void removeBy(long accountId, long volId, int storageType) {
         TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
-        PreparedStatement pstmt = null;
         try {
             txn.start();
             String sql = REMOVE_BY_USERID_STORAGEID;
-            pstmt = txn.prepareAutoCloseStatement(sql);
-            pstmt.setLong(1, accountId);
-            pstmt.setLong(2, volId);
-            pstmt.setInt(3, storageType);
-            pstmt.executeUpdate();
+            try( PreparedStatement pstmt = txn.prepareStatement(sql);) {
+                pstmt.setLong(1, accountId);
+                pstmt.setLong(2, volId);
+                pstmt.setInt(3, storageType);
+                pstmt.executeUpdate();
+            }catch(SQLException e)
+            {
+                throw new CloudException("removeBy:Exception:"+e.getMessage(),e);
+            }
             txn.commit();
         } catch (Exception e) {
             txn.rollback();
-            s_logger.warn("Error removing usageStorageVO", e);
+            s_logger.error("Error removing usageStorageVO", e);
         } finally {
             txn.close();
         }
@@ -112,21 +117,26 @@ public class UsageStorageDaoImpl extends GenericDaoBase<UsageStorageVO, Long> im
     @Override
     public void update(UsageStorageVO usage) {
         TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
-        PreparedStatement pstmt = null;
         try {
             txn.start();
             if (usage.getDeleted() != null) {
-                pstmt = txn.prepareAutoCloseStatement(UPDATE_DELETED);
-                pstmt.setString(1, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), usage.getDeleted()));
-                pstmt.setLong(2, usage.getAccountId());
-                pstmt.setLong(3, usage.getId());
-                pstmt.setInt(4, usage.getStorageType());
+                try(PreparedStatement pstmt = txn.prepareStatement(UPDATE_DELETED);) {
+                    if (pstmt != null) {
+                        pstmt.setString(1, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), usage.getDeleted()));
+                        pstmt.setLong(2, usage.getAccountId());
+                        pstmt.setLong(3, usage.getId());
+                        pstmt.setInt(4, usage.getStorageType());
+                        pstmt.executeUpdate();
+                    }
+                }catch (SQLException e)
+                {
+                    throw new CloudException("UsageStorageVO update Error:"+e.getMessage(),e);
+                }
             }
-            pstmt.executeUpdate();
             txn.commit();
         } catch (Exception e) {
             txn.rollback();
-            s_logger.warn("Error updating UsageStorageVO", e);
+            s_logger.error("Error updating UsageStorageVO:"+e.getMessage(), e);
         } finally {
             txn.close();
         }
@@ -157,11 +167,8 @@ public class UsageStorageDaoImpl extends GenericDaoBase<UsageStorageVO, Long> im
         }
 
         TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
-        PreparedStatement pstmt = null;
-
-        try {
-            int i = 1;
-            pstmt = txn.prepareAutoCloseStatement(sql);
+        int i = 1;
+        try (PreparedStatement pstmt = txn.prepareStatement(sql);){
             if (param1 != null) {
                 pstmt.setLong(i++, param1);
             }
@@ -172,38 +179,41 @@ public class UsageStorageDaoImpl extends GenericDaoBase<UsageStorageVO, Long> im
             pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), startDate));
             pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), endDate));
 
-            ResultSet rs = pstmt.executeQuery();
-            while (rs.next()) {
-                //id, zone_id, account_id, domain_id, storage_type, size, created, deleted
-                Long id = Long.valueOf(rs.getLong(1));
-                Long zoneId = Long.valueOf(rs.getLong(2));
-                Long acctId = Long.valueOf(rs.getLong(3));
-                Long dId = Long.valueOf(rs.getLong(4));
-                Integer type = Integer.valueOf(rs.getInt(5));
-                Long sourceId = Long.valueOf(rs.getLong(6));
-                Long size = Long.valueOf(rs.getLong(7));
-                Long virtualSize = Long.valueOf(rs.getLong(10));
-                Date createdDate = null;
-                Date deletedDate = null;
-                String createdTS = rs.getString(8);
-                String deletedTS = rs.getString(9);
-
-                if (createdTS != null) {
-                    createdDate = DateUtil.parseDateString(s_gmtTimeZone, createdTS);
+            try(ResultSet rs = pstmt.executeQuery();) {
+                while (rs.next()) {
+                    //id, zone_id, account_id, domain_id, storage_type, size, created, deleted
+                    Long id = Long.valueOf(rs.getLong(1));
+                    Long zoneId = Long.valueOf(rs.getLong(2));
+                    Long acctId = Long.valueOf(rs.getLong(3));
+                    Long dId = Long.valueOf(rs.getLong(4));
+                    Integer type = Integer.valueOf(rs.getInt(5));
+                    Long sourceId = Long.valueOf(rs.getLong(6));
+                    Long size = Long.valueOf(rs.getLong(7));
+                    Long virtualSize = Long.valueOf(rs.getLong(10));
+                    Date createdDate = null;
+                    Date deletedDate = null;
+                    String createdTS = rs.getString(8);
+                    String deletedTS = rs.getString(9);
+
+                    if (createdTS != null) {
+                        createdDate = DateUtil.parseDateString(s_gmtTimeZone, createdTS);
+                    }
+                    if (deletedTS != null) {
+                        deletedDate = DateUtil.parseDateString(s_gmtTimeZone, deletedTS);
+                    }
+
+                    usageRecords.add(new UsageStorageVO(id, zoneId, acctId, dId, type, sourceId, size, virtualSize, createdDate, deletedDate));
                 }
-                if (deletedTS != null) {
-                    deletedDate = DateUtil.parseDateString(s_gmtTimeZone, deletedTS);
-                }
-
-                usageRecords.add(new UsageStorageVO(id, zoneId, acctId, dId, type, sourceId, size, virtualSize, createdDate, deletedDate));
+            }catch(SQLException e)
+            {
+                throw new CloudException("getUsageRecords:"+e.getMessage(),e);
             }
-        } catch (Exception e) {
+        }catch (Exception e) {
             txn.rollback();
-            s_logger.warn("Error getting usage records", e);
+            s_logger.error("getUsageRecords:Exception:"+e.getMessage(), e);
         } finally {
             txn.close();
         }
-
         return usageRecords;
     }
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a600d840/engine/schema/src/com/cloud/usage/dao/UsageVPNUserDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/usage/dao/UsageVPNUserDaoImpl.java b/engine/schema/src/com/cloud/usage/dao/UsageVPNUserDaoImpl.java
index 64d3ecd..e2251df 100644
--- a/engine/schema/src/com/cloud/usage/dao/UsageVPNUserDaoImpl.java
+++ b/engine/schema/src/com/cloud/usage/dao/UsageVPNUserDaoImpl.java
@@ -18,6 +18,7 @@ package com.cloud.usage.dao;
 
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
+import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
@@ -25,6 +26,7 @@ import java.util.TimeZone;
 
 import javax.ejb.Local;
 
+import com.cloud.exception.CloudException;
 import org.apache.log4j.Logger;
 import org.springframework.stereotype.Component;
 
@@ -52,20 +54,24 @@ public class UsageVPNUserDaoImpl extends GenericDaoBase<UsageVPNUserVO, Long> im
     @Override
     public void update(UsageVPNUserVO usage) {
         TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
-        PreparedStatement pstmt = null;
         try {
             txn.start();
             if (usage.getDeleted() != null) {
-                pstmt = txn.prepareAutoCloseStatement(UPDATE_DELETED);
-                pstmt.setString(1, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), usage.getDeleted()));
-                pstmt.setLong(2, usage.getAccountId());
-                pstmt.setLong(3, usage.getUserId());
+                try(PreparedStatement pstmt = txn.prepareStatement(UPDATE_DELETED);) {
+                    if (pstmt != null) {
+                        pstmt.setString(1, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), usage.getDeleted()));
+                        pstmt.setLong(2, usage.getAccountId());
+                        pstmt.setLong(3, usage.getUserId());
+                        pstmt.executeUpdate();
+                    }
+                }catch (SQLException e) {
+                    throw new CloudException("Error updating UsageVPNUserVO"+e.getMessage(), e);
+                }
             }
-            pstmt.executeUpdate();
             txn.commit();
         } catch (Exception e) {
             txn.rollback();
-            s_logger.warn("Error updating UsageVPNUserVO", e);
+            s_logger.error("Error updating UsageVPNUserVO:"+e.getMessage(), e);
         } finally {
             txn.close();
         }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a600d840/engine/schema/src/com/cloud/usage/dao/UsageVolumeDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/usage/dao/UsageVolumeDaoImpl.java b/engine/schema/src/com/cloud/usage/dao/UsageVolumeDaoImpl.java
index 7bf058c..5beafa6 100644
--- a/engine/schema/src/com/cloud/usage/dao/UsageVolumeDaoImpl.java
+++ b/engine/schema/src/com/cloud/usage/dao/UsageVolumeDaoImpl.java
@@ -18,6 +18,7 @@ package com.cloud.usage.dao;
 
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
+import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
@@ -25,6 +26,7 @@ import java.util.TimeZone;
 
 import javax.ejb.Local;
 
+import com.cloud.exception.CloudException;
 import org.apache.log4j.Logger;
 import org.springframework.stereotype.Component;
 
@@ -55,18 +57,21 @@ public class UsageVolumeDaoImpl extends GenericDaoBase<UsageVolumeVO, Long> impl
     @Override
     public void removeBy(long accountId, long volId) {
         TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
-        PreparedStatement pstmt = null;
         try {
             txn.start();
-            String sql = REMOVE_BY_USERID_VOLID;
-            pstmt = txn.prepareAutoCloseStatement(sql);
-            pstmt.setLong(1, accountId);
-            pstmt.setLong(2, volId);
-            pstmt.executeUpdate();
+            try(PreparedStatement pstmt = txn.prepareStatement(REMOVE_BY_USERID_VOLID);) {
+                if (pstmt != null) {
+                    pstmt.setLong(1, accountId);
+                    pstmt.setLong(2, volId);
+                    pstmt.executeUpdate();
+                }
+            }catch (SQLException e) {
+                throw new CloudException("Error removing usageVolumeVO:"+e.getMessage(), e);
+            }
             txn.commit();
         } catch (Exception e) {
             txn.rollback();
-            s_logger.warn("Error removing usageVolumeVO", e);
+            s_logger.warn("Error removing usageVolumeVO:"+e.getMessage(), e);
         } finally {
             txn.close();
         }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a600d840/engine/schema/src/org/apache/cloudstack/storage/datastore/db/PrimaryDataStoreDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/org/apache/cloudstack/storage/datastore/db/PrimaryDataStoreDaoImpl.java b/engine/schema/src/org/apache/cloudstack/storage/datastore/db/PrimaryDataStoreDaoImpl.java
index 92793f1..cb8ec31 100644
--- a/engine/schema/src/org/apache/cloudstack/storage/datastore/db/PrimaryDataStoreDaoImpl.java
+++ b/engine/schema/src/org/apache/cloudstack/storage/datastore/db/PrimaryDataStoreDaoImpl.java
@@ -263,9 +263,8 @@ public class PrimaryDataStoreDaoImpl extends GenericDaoBase<StoragePoolVO, Long>
         sql.delete(sql.length() - 4, sql.length());
         sql.append(DetailsSqlSuffix);
         TransactionLegacy txn = TransactionLegacy.currentTxn();
-        PreparedStatement pstmt = null;
-        try {
-            pstmt = txn.prepareAutoCloseStatement(sql.toString());
+        try (PreparedStatement pstmt = txn.prepareStatement(sql.toString());){
+            List<StoragePoolVO> pools = new ArrayList<StoragePoolVO>();
             int i = 1;
             pstmt.setLong(i++, dcId);
             pstmt.setLong(i++, podId);
@@ -274,14 +273,16 @@ public class PrimaryDataStoreDaoImpl extends GenericDaoBase<StoragePoolVO, Long>
                 pstmt.setLong(i++, clusterId);
             }
             pstmt.setInt(i++, details.size());
-            ResultSet rs = pstmt.executeQuery();
-            List<StoragePoolVO> pools = new ArrayList<StoragePoolVO>();
-            while (rs.next()) {
-                pools.add(toEntityBean(rs, false));
+            try(ResultSet rs = pstmt.executeQuery();) {
+                while (rs.next()) {
+                    pools.add(toEntityBean(rs, false));
+                }
+            }catch (SQLException e) {
+                throw new CloudRuntimeException("Unable to execute :" + e.getMessage(), e);
             }
             return pools;
         } catch (SQLException e) {
-            throw new CloudRuntimeException("Unable to execute " + pstmt, e);
+            throw new CloudRuntimeException("Unable to execute :" + e.getMessage(), e);
         }
     }
 
@@ -378,21 +379,24 @@ public class PrimaryDataStoreDaoImpl extends GenericDaoBase<StoragePoolVO, Long>
             sql.delete(sql.length() - 4, sql.length());
             sql.append(ZoneWideDetailsSqlSuffix);
             TransactionLegacy txn = TransactionLegacy.currentTxn();
-            PreparedStatement pstmt = null;
-            try {
-                pstmt = txn.prepareAutoCloseStatement(sql.toString());
-                int i = 1;
-                pstmt.setLong(i++, dcId);
-                pstmt.setString(i++, ScopeType.ZONE.toString());
-                pstmt.setInt(i++, details.size());
-                ResultSet rs = pstmt.executeQuery();
+            try (PreparedStatement pstmt = txn.prepareStatement(sql.toString());){
                 List<StoragePoolVO> pools = new ArrayList<StoragePoolVO>();
-                while (rs.next()) {
-                    pools.add(toEntityBean(rs, false));
+                if (pstmt != null) {
+                    int i = 1;
+                    pstmt.setLong(i++, dcId);
+                    pstmt.setString(i++, ScopeType.ZONE.toString());
+                    pstmt.setInt(i++, details.size());
+                    try(ResultSet rs = pstmt.executeQuery();) {
+                        while (rs.next()) {
+                            pools.add(toEntityBean(rs, false));
+                        }
+                    }catch (SQLException e) {
+                        throw new CloudRuntimeException("findZoneWideStoragePoolsByTags:Exception:" + e.getMessage(), e);
+                    }
                 }
                 return pools;
             } catch (SQLException e) {
-                throw new CloudRuntimeException("Unable to execute " + pstmt, e);
+                throw new CloudRuntimeException("findZoneWideStoragePoolsByTags:Exception:" + e.getMessage(), e);
             }
         }
     }
@@ -400,27 +404,25 @@ public class PrimaryDataStoreDaoImpl extends GenericDaoBase<StoragePoolVO, Long>
     @Override
     @DB
     public List<String> searchForStoragePoolDetails(long poolId, String value) {
-
         StringBuilder sql = new StringBuilder(FindPoolTagDetails);
-
         TransactionLegacy txn = TransactionLegacy.currentTxn();
-        PreparedStatement pstmt = null;
-        try {
-            pstmt = txn.prepareAutoCloseStatement(sql.toString());
-            pstmt.setLong(1, poolId);
-            pstmt.setString(2, value);
-
-            ResultSet rs = pstmt.executeQuery();
-            List<String> tags = new ArrayList<String>();
-
-            while (rs.next()) {
-                tags.add(rs.getString("name"));
+        List<String> tags = new ArrayList<String>();
+        try(PreparedStatement pstmt = txn.prepareStatement(sql.toString());) {
+            if (pstmt != null) {
+                pstmt.setLong(1, poolId);
+                pstmt.setString(2, value);
+                try(ResultSet rs = pstmt.executeQuery();) {
+                    while (rs.next()) {
+                        tags.add(rs.getString("name"));
+                    }
+                }catch (SQLException e) {
+                    throw new CloudRuntimeException("searchForStoragePoolDetails:Exception:" + e.getMessage(), e);
+                }
             }
             return tags;
         } catch (SQLException e) {
-            throw new CloudRuntimeException("Unable to execute " + pstmt.toString(), e);
+            throw new CloudRuntimeException("searchForStoragePoolDetails:Exception:" + e.getMessage(), e);
         }
-
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a600d840/engine/storage/cache/src/org/apache/cloudstack/storage/cache/allocator/StorageCacheRandomAllocator.java
----------------------------------------------------------------------
diff --git a/engine/storage/cache/src/org/apache/cloudstack/storage/cache/allocator/StorageCacheRandomAllocator.java b/engine/storage/cache/src/org/apache/cloudstack/storage/cache/allocator/StorageCacheRandomAllocator.java
index b4ef595..ba1ee3c 100644
--- a/engine/storage/cache/src/org/apache/cloudstack/storage/cache/allocator/StorageCacheRandomAllocator.java
+++ b/engine/storage/cache/src/org/apache/cloudstack/storage/cache/allocator/StorageCacheRandomAllocator.java
@@ -52,7 +52,7 @@ public class StorageCacheRandomAllocator implements StorageCacheAllocator {
         }
 
         List<DataStore> cacheStores = dataStoreMgr.getImageCacheStores(scope);
-        if (cacheStores.size() <= 0) {
+        if ((cacheStores == null) || (cacheStores.size() <= 0)) {
             s_logger.debug("Can't find staging storage in zone: " + scope.getScopeId());
             return null;
         }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a600d840/engine/storage/src/org/apache/cloudstack/storage/volume/datastore/PrimaryDataStoreHelper.java
----------------------------------------------------------------------
diff --git a/engine/storage/src/org/apache/cloudstack/storage/volume/datastore/PrimaryDataStoreHelper.java b/engine/storage/src/org/apache/cloudstack/storage/volume/datastore/PrimaryDataStoreHelper.java
index 6b12975..ef0db85 100644
--- a/engine/storage/src/org/apache/cloudstack/storage/volume/datastore/PrimaryDataStoreHelper.java
+++ b/engine/storage/src/org/apache/cloudstack/storage/volume/datastore/PrimaryDataStoreHelper.java
@@ -67,11 +67,14 @@ public class PrimaryDataStoreHelper {
     protected StoragePoolHostDao storagePoolHostDao;
 
     public DataStore createPrimaryDataStore(PrimaryDataStoreParameters params) {
+        if(params == null)
+        {
+            throw new InvalidParameterValueException("createPrimaryDataStore: Input params is null, please check");
+        }
         StoragePoolVO dataStoreVO = dataStoreDao.findPoolByUUID(params.getUuid());
         if (dataStoreVO != null) {
             throw new CloudRuntimeException("duplicate uuid: " + params.getUuid());
         }
-
         dataStoreVO = new StoragePoolVO();
         dataStoreVO.setStorageProviderName(params.getProviderName());
         dataStoreVO.setHostAddress(params.getHost());
@@ -114,7 +117,6 @@ public class PrimaryDataStoreHelper {
 
             dataStoreVO.setPath(updatedPath);
         }
-
         String tags = params.getTags();
         if (tags != null) {
             String[] tokens = tags.split(",");
@@ -127,9 +129,7 @@ public class PrimaryDataStoreHelper {
                 details.put(tag, "true");
             }
         }
-
         dataStoreVO = dataStoreDao.persist(dataStoreVO, details);
-
         return dataStoreMgr.getDataStore(dataStoreVO.getId(), DataStoreRole.Primary);
     }
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a600d840/framework/cluster/src/com/cloud/cluster/dao/ManagementServerHostDaoImpl.java
----------------------------------------------------------------------
diff --git a/framework/cluster/src/com/cloud/cluster/dao/ManagementServerHostDaoImpl.java b/framework/cluster/src/com/cloud/cluster/dao/ManagementServerHostDaoImpl.java
index 3d0c3f5..89d7d27 100644
--- a/framework/cluster/src/com/cloud/cluster/dao/ManagementServerHostDaoImpl.java
+++ b/framework/cluster/src/com/cloud/cluster/dao/ManagementServerHostDaoImpl.java
@@ -53,15 +53,14 @@ public class ManagementServerHostDaoImpl extends GenericDaoBase<ManagementServer
     @Override
     public void invalidateRunSession(long id, long runid) {
         TransactionLegacy txn = TransactionLegacy.currentTxn();
-        PreparedStatement pstmt = null;
-        try {
-            pstmt = txn.prepareAutoCloseStatement("update mshost set runid=0, state='Down' where id=? and runid=?");
-            pstmt.setLong(1, id);
-            pstmt.setLong(2, runid);
-
-            pstmt.executeUpdate();
+        try (PreparedStatement pstmt = txn.prepareStatement("update mshost set runid=0, state='Down' where id=? and runid=?");){
+            if(pstmt != null) {
+                pstmt.setLong(1, id);
+                pstmt.setLong(2, runid);
+                pstmt.executeUpdate();
+            }
         } catch (SQLException e) {
-            throw new CloudRuntimeException("DB exception on " + pstmt.toString(), e);
+            throw new CloudRuntimeException("invalidateRunSession:Exception:"+ e.getMessage(), e);
         }
     }
 
@@ -82,26 +81,30 @@ public class ManagementServerHostDaoImpl extends GenericDaoBase<ManagementServer
     @DB
     public void update(long id, long runid, String name, String version, String serviceIP, int servicePort, Date lastUpdate) {
         TransactionLegacy txn = TransactionLegacy.currentTxn();
-        PreparedStatement pstmt = null;
         try {
             txn.start();
-
-            pstmt =
-                txn.prepareAutoCloseStatement("update mshost set name=?, version=?, service_ip=?, service_port=?, last_update=?, removed=null, alert_count=0, runid=?, state=? where id=?");
-            pstmt.setString(1, name);
-            pstmt.setString(2, version);
-            pstmt.setString(3, serviceIP);
-            pstmt.setInt(4, servicePort);
-            pstmt.setString(5, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), lastUpdate));
-            pstmt.setLong(6, runid);
-            pstmt.setString(7, ManagementServerHost.State.Up.toString());
-            pstmt.setLong(8, id);
-
-            pstmt.executeUpdate();
+            try(PreparedStatement pstmt =
+                txn.prepareStatement("update mshost set name=?, version=?, service_ip=?, service_port=?, last_update=?, removed=null, alert_count=0, runid=?, state=? where id=?");) {
+                pstmt.setString(1, name);
+                pstmt.setString(2, version);
+                pstmt.setString(3, serviceIP);
+                pstmt.setInt(4, servicePort);
+                pstmt.setString(5, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), lastUpdate));
+                pstmt.setLong(6, runid);
+                pstmt.setString(7, ManagementServerHost.State.Up.toString());
+                pstmt.setLong(8, id);
+                pstmt.executeUpdate();
+            }catch(SQLException e)
+            {
+                throw new CloudRuntimeException("update:Exception:"+e.getMessage(),e);
+            }
             txn.commit();
-        } catch (Exception e) {
+        } catch (RuntimeException e) {
+            txn.rollback();
             s_logger.warn("Unexpected exception, ", e);
             throw new RuntimeException(e.getMessage(), e);
+        }finally {
+            txn.close();
         }
     }
 
@@ -109,19 +112,20 @@ public class ManagementServerHostDaoImpl extends GenericDaoBase<ManagementServer
     @DB
     public boolean remove(Long id) {
         TransactionLegacy txn = TransactionLegacy.currentTxn();
-
         try {
             txn.start();
-
             ManagementServerHostVO msHost = findById(id);
             msHost.setState(ManagementServerHost.State.Down);
             super.remove(id);
-
             txn.commit();
             return true;
         } catch (Exception e) {
+            txn.rollback();
             s_logger.warn("Unexpected exception, ", e);
-            throw new RuntimeException(e.getMessage(), e);
+            throw new CloudRuntimeException("remove:Exception:"+e.getMessage(), e);
+        }finally
+        {
+            txn.close();
         }
     }
 
@@ -129,24 +133,29 @@ public class ManagementServerHostDaoImpl extends GenericDaoBase<ManagementServer
     @DB
     public void update(long id, long runid, Date lastUpdate) {
         TransactionLegacy txn = TransactionLegacy.currentTxn();
-        PreparedStatement pstmt = null;
         try {
             txn.start();
-
-            pstmt = txn.prepareAutoCloseStatement("update mshost set last_update=?, removed=null, alert_count=0 where id=? and runid=?");
-            pstmt.setString(1, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), lastUpdate));
-            pstmt.setLong(2, id);
-            pstmt.setLong(3, runid);
-
-            int count = pstmt.executeUpdate();
-            txn.commit();
-
-            if (count < 1) {
-                throw new CloudRuntimeException("Invalid cluster session detected", new ClusterInvalidSessionException("runid " + runid + " is no longer valid"));
+            try( PreparedStatement pstmt = txn.prepareStatement("update mshost set last_update=?, removed=null, alert_count=0 where id=? and runid=?");) {
+                pstmt.setString(1, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), lastUpdate));
+                pstmt.setLong(2, id);
+                pstmt.setLong(3, runid);
+
+                int count = pstmt.executeUpdate();
+                txn.commit();
+
+                if (count < 1) {
+                    throw new CloudRuntimeException("Invalid cluster session detected", new ClusterInvalidSessionException("runid " + runid + " is no longer valid"));
+                }
+            }catch (SQLException e) {
+                throw new CloudRuntimeException("update:Exception:"+e.getMessage(), e);
             }
-        } catch (Exception e) {
-            s_logger.warn("Unexpected exception, ", e);
-            throw new RuntimeException(e.getMessage(), e);
+        } catch (RuntimeException e) {
+            txn.rollback();
+            s_logger.warn("update:Exception:"+e.getMessage(), e);
+            throw new RuntimeException("update:Exception:"+e.getMessage(), e);
+        }
+        finally {
+            txn.close();
         }
     }
 
@@ -170,21 +179,25 @@ public class ManagementServerHostDaoImpl extends GenericDaoBase<ManagementServer
     @DB
     public int increaseAlertCount(long id) {
         TransactionLegacy txn = TransactionLegacy.currentTxn();
-        PreparedStatement pstmt = null;
         int changedRows = 0;
         try {
             txn.start();
-
-            pstmt = txn.prepareAutoCloseStatement("update mshost set alert_count=alert_count+1 where id=? and alert_count=0");
-            pstmt.setLong(1, id);
-
-            changedRows = pstmt.executeUpdate();
+            try(PreparedStatement pstmt = txn.prepareStatement("update mshost set alert_count=alert_count+1 where id=? and alert_count=0");) {
+                pstmt.setLong(1, id);
+                changedRows = pstmt.executeUpdate();
+            }catch (SQLException e)
+            {
+                throw new CloudRuntimeException("increaseAlertCount:Exception:"+e.getMessage(),e);
+            }
             txn.commit();
-        } catch (Exception e) {
-            s_logger.warn("Unexpected exception, ", e);
-            throw new RuntimeException(e.getMessage(), e);
+        } catch (RuntimeException e) {
+            txn.rollback();
+            s_logger.warn("increaseAlertCount:Exception:" + e.getMessage(), e);
+            throw new CloudRuntimeException("increaseAlertCount:Exception:" + e.getMessage(), e);
+        }finally
+        {
+            txn.close();
         }
-
         return changedRows;
     }
 
@@ -211,21 +224,19 @@ public class ManagementServerHostDaoImpl extends GenericDaoBase<ManagementServer
     @Override
     public void update(long id, long runId, State state, Date lastUpdate) {
         TransactionLegacy txn = TransactionLegacy.currentTxn();
-        PreparedStatement pstmt = null;
-        try {
-            pstmt = txn.prepareAutoCloseStatement("update mshost set state=?, last_update=? where id=? and runid=?");
-            pstmt.setString(1, state.toString());
-            pstmt.setString(2, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), lastUpdate));
-            pstmt.setLong(3, id);
-            pstmt.setLong(4, runId);
-
-            int count = pstmt.executeUpdate();
-
-            if (count < 1) {
-                throw new CloudRuntimeException("Invalid cluster session detected", new ClusterInvalidSessionException("runid " + runId + " is no longer valid"));
+        try (PreparedStatement pstmt = txn.prepareStatement("update mshost set state=?, last_update=? where id=? and runid=?");){
+            if (pstmt != null) {
+                pstmt.setString(1, state.toString());
+                pstmt.setString(2, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), lastUpdate));
+                pstmt.setLong(3, id);
+                pstmt.setLong(4, runId);
+                int count = pstmt.executeUpdate();
+                if (count < 1) {
+                    throw new CloudRuntimeException("Invalid cluster session detected", new ClusterInvalidSessionException("runid " + runId + " is no longer valid"));
+                }
             }
         } catch (SQLException e) {
-            throw new CloudRuntimeException("DB exception on " + pstmt.toString(), e);
+            throw new CloudRuntimeException("update:Exception:" + e.getMessage(), e);
         }
     }
 
@@ -241,22 +252,21 @@ public class ManagementServerHostDaoImpl extends GenericDaoBase<ManagementServer
     @Override
     public List<Long> listOrphanMsids() {
         List<Long> orphanList = new ArrayList<Long>();
-
         TransactionLegacy txn = TransactionLegacy.currentTxn();
-        PreparedStatement pstmt = null;
-        try {
-            pstmt =
-                txn.prepareAutoCloseStatement("select t.mgmt_server_id from (select mgmt_server_id, count(*) as count from host group by mgmt_server_id) as t WHERE t.count > 0 AND t.mgmt_server_id NOT IN (select msid from mshost)");
-
-            ResultSet rs = pstmt.executeQuery();
-            while (rs.next()) {
-                orphanList.add(rs.getLong(1));
+        try (PreparedStatement pstmt =
+                     txn.prepareStatement("select t.mgmt_server_id from (select mgmt_server_id, count(*) as count from host group by mgmt_server_id) as t WHERE t.count > 0 AND t.mgmt_server_id NOT IN (select msid from mshost)");)
+        {
+            try(ResultSet rs = pstmt.executeQuery();) {
+                while (rs.next()) {
+                    orphanList.add(rs.getLong(1));
+                }
+            }catch (SQLException e) {
+                throw new CloudRuntimeException("listOrphanMsids:Exception:" + e.getMessage(), e);
             }
+            return orphanList;
         } catch (SQLException e) {
-            throw new CloudRuntimeException("DB exception on " + pstmt.toString(), e);
+            throw new CloudRuntimeException("listOrphanMsids:Exception:" + e.getMessage(), e);
         }
-
-        return orphanList;
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a600d840/framework/db/src/com/cloud/utils/db/DbUtil.java
----------------------------------------------------------------------
diff --git a/framework/db/src/com/cloud/utils/db/DbUtil.java b/framework/db/src/com/cloud/utils/db/DbUtil.java
index 792573c..43ed462 100755
--- a/framework/db/src/com/cloud/utils/db/DbUtil.java
+++ b/framework/db/src/com/cloud/utils/db/DbUtil.java
@@ -257,7 +257,7 @@ public class DbUtil {
             rs = pstmt.executeQuery();
             if (rs != null && rs.first())
                 return rs.getInt(1) > 0;
-            s_logger.error("RELEASE_LOCK() returns unexpected result : " + rs.getInt(1));
+            s_logger.error("releaseGlobalLock:RELEASE_LOCK() returns unexpected result");
         } catch (SQLException e) {
             s_logger.error("RELEASE_LOCK() throws exception ", e);
         } catch (Throwable e) {

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a600d840/framework/db/src/com/cloud/utils/db/GenericDaoBase.java
----------------------------------------------------------------------
diff --git a/framework/db/src/com/cloud/utils/db/GenericDaoBase.java b/framework/db/src/com/cloud/utils/db/GenericDaoBase.java
index 2052aad..4c47404 100755
--- a/framework/db/src/com/cloud/utils/db/GenericDaoBase.java
+++ b/framework/db/src/com/cloud/utils/db/GenericDaoBase.java
@@ -1486,9 +1486,9 @@ public abstract class GenericDaoBase<T, ID extends Serializable> extends Compone
             if (length < str.length()) {
                 try {
                     if (attr.is(Attribute.Flag.Encrypted)) {
-                        pstmt.setBytes(j, DBEncryptionUtil.encrypt(str.substring(0, column.length())).getBytes("UTF-8"));
+                        pstmt.setBytes(j, DBEncryptionUtil.encrypt(str.substring(0, length)).getBytes("UTF-8"));
                     } else {
-                        pstmt.setBytes(j, str.substring(0, column.length()).getBytes("UTF-8"));
+                        pstmt.setBytes(j, str.substring(0, length).getBytes("UTF-8"));
                     }
                 } catch (UnsupportedEncodingException e) {
                     // no-way it can't support UTF-8 encoding

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a600d840/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/BridgeVifDriver.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/BridgeVifDriver.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/BridgeVifDriver.java
index e684b8d..839cbaa 100644
--- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/BridgeVifDriver.java
+++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/BridgeVifDriver.java
@@ -101,6 +101,10 @@ public class BridgeVifDriver extends VifDriverBase {
         } else if (nic.getBroadcastType() == Networks.BroadcastDomainType.Lswitch) {
             throw new InternalErrorException("Nicira NVP Logicalswitches are not supported by the BridgeVifDriver");
         }
+        if ((vNetId == null)||(protocol == null))
+        {
+            throw new InternalErrorException("plug: protocol or vNetId value is null");
+        }
         String trafficLabel = nic.getName();
         if (nic.getType() == Networks.TrafficType.Guest) {
             Integer networkRateKBps = (nic.getNetworkRateMbps() != null && nic.getNetworkRateMbps().intValue() != -1) ? nic.getNetworkRateMbps().intValue() * 128 : 0;

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a600d840/plugins/storage/volume/sample/src/org/apache/cloudstack/storage/datastore/lifecycle/SamplePrimaryDataStoreLifeCycleImpl.java
----------------------------------------------------------------------
diff --git a/plugins/storage/volume/sample/src/org/apache/cloudstack/storage/datastore/lifecycle/SamplePrimaryDataStoreLifeCycleImpl.java b/plugins/storage/volume/sample/src/org/apache/cloudstack/storage/datastore/lifecycle/SamplePrimaryDataStoreLifeCycleImpl.java
index 579cc24..ee38072 100644
--- a/plugins/storage/volume/sample/src/org/apache/cloudstack/storage/datastore/lifecycle/SamplePrimaryDataStoreLifeCycleImpl.java
+++ b/plugins/storage/volume/sample/src/org/apache/cloudstack/storage/datastore/lifecycle/SamplePrimaryDataStoreLifeCycleImpl.java
@@ -61,7 +61,6 @@ public class SamplePrimaryDataStoreLifeCycleImpl implements PrimaryDataStoreLife
 
     @Override
     public DataStore initialize(Map<String, Object> dsInfos) {
-
         DataStore store = primaryStoreHelper.createPrimaryDataStore(null);
         return providerMgr.getPrimaryDataStore(store.getId());
     }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a600d840/server/src/com/cloud/storage/VolumeApiServiceImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/VolumeApiServiceImpl.java b/server/src/com/cloud/storage/VolumeApiServiceImpl.java
index a557c0e..e788cb2 100644
--- a/server/src/com/cloud/storage/VolumeApiServiceImpl.java
+++ b/server/src/com/cloud/storage/VolumeApiServiceImpl.java
@@ -1642,7 +1642,7 @@ public class VolumeApiServiceImpl extends ManagerBase implements VolumeApiServic
                 try {
                 return orchestrateMigrateVolume(vol.getId(), destPool.getId(), liveMigrateVolume);
                 } finally {
-                    if (VmJobEnabled.value())
+                    if ((VmJobEnabled.value())&&(placeHolder != null))
                         _workJobDao.expunge(placeHolder.getId());
                 }
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a600d840/server/src/com/cloud/tags/TaggedResourceManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/tags/TaggedResourceManagerImpl.java b/server/src/com/cloud/tags/TaggedResourceManagerImpl.java
index b77c55e..ea032ad 100644
--- a/server/src/com/cloud/tags/TaggedResourceManagerImpl.java
+++ b/server/src/com/cloud/tags/TaggedResourceManagerImpl.java
@@ -190,7 +190,7 @@ public class TaggedResourceManagerImpl extends ManagerBase implements TaggedReso
             accountId = Account.ACCOUNT_ID_SYSTEM;
         }
 
-        if ( ((accountId != null) && (domainId == -1)) || (domainId == null) )
+        if ((domainId == null) || ((accountId != null) && (domainId.longValue() == -1)))
         {
             domainId = _accountDao.getDomainIdForGivenAccountId(accountId);
         }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a600d840/server/src/com/cloud/vm/snapshot/VMSnapshotManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/vm/snapshot/VMSnapshotManagerImpl.java b/server/src/com/cloud/vm/snapshot/VMSnapshotManagerImpl.java
index e085b8d..e46aded 100644
--- a/server/src/com/cloud/vm/snapshot/VMSnapshotManagerImpl.java
+++ b/server/src/com/cloud/vm/snapshot/VMSnapshotManagerImpl.java
@@ -727,7 +727,7 @@ public class VMSnapshotManagerImpl extends ManagerBase implements VMSnapshotMana
             try {
             return orchestrateDeleteAllVMSnapshots(vmId, type);
             } finally {
-                if (VmJobEnabled.value())
+                if ( (VmJobEnabled.value()) && (placeHolder != null))
                     _workJobDao.expunge(placeHolder.getId());
             }