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

This commit is contained in:
Santhosh Edukulla 2014-07-02 14:08:16 +05:30
parent 4607c26949
commit a600d8408e
22 changed files with 336 additions and 276 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}
}

View File

@ -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);

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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));
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);
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 (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));
}
if (deletedTS != null) {
deletedDate = DateUtil.parseDateString(s_gmtTimeZone, deletedTS);
}
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;
}
}

View File

@ -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);
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);
}
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));
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;
}
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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();
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);
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();
int count = pstmt.executeUpdate();
txn.commit();
if (count < 1) {
throw new CloudRuntimeException("Invalid cluster session detected", new ClusterInvalidSessionException("runid " + runid + " is no longer valid"));
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

View File

@ -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) {

View File

@ -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

View File

@ -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;

View File

@ -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());
}

View File

@ -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());
}

View File

@ -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);
}

View File

@ -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());
}