use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.
the class CapacityDaoImpl method updateAllocated.
public void updateAllocated(Long hostId, long allocatedAmount, short capacityType, boolean add) {
TransactionLegacy txn = TransactionLegacy.currentTxn();
PreparedStatement pstmt = null;
try {
txn.start();
String sql = null;
if (add) {
sql = ADD_ALLOCATED_SQL;
} else {
sql = SUBTRACT_ALLOCATED_SQL;
}
pstmt = txn.prepareAutoCloseStatement(sql);
pstmt.setLong(1, allocatedAmount);
pstmt.setLong(2, hostId);
pstmt.setShort(3, capacityType);
// TODO: Make sure exactly 1 row was updated?
pstmt.executeUpdate();
txn.commit();
} catch (Exception e) {
txn.rollback();
s_logger.warn("Exception updating capacity for host: " + hostId, e);
}
}
use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.
the class CapacityDaoImpl method findCapacityBy.
@Override
public List<SummedCapacity> findCapacityBy(Integer capacityType, Long zoneId, Long podId, Long clusterId) {
TransactionLegacy txn = TransactionLegacy.currentTxn();
PreparedStatement pstmt = null;
List<SummedCapacity> results = new ArrayList<SummedCapacity>();
StringBuilder sql = new StringBuilder(LIST_CAPACITY_GROUP_BY_CAPACITY_PART1);
List<Long> resourceIdList = new ArrayList<Long>();
if (zoneId != null) {
sql.append(" AND capacity.data_center_id = ?");
resourceIdList.add(zoneId);
}
if (podId != null) {
sql.append(" AND capacity.pod_id = ?");
resourceIdList.add(podId);
}
if (clusterId != null) {
sql.append(" AND capacity.cluster_id = ?");
resourceIdList.add(clusterId);
}
if (capacityType != null) {
sql.append(" AND capacity.capacity_type = ?");
resourceIdList.add(capacityType.longValue());
}
sql.append(LIST_CAPACITY_GROUP_BY_CAPACITY_DATA_CENTER_POD_CLUSTER);
try {
pstmt = txn.prepareAutoCloseStatement(sql.toString());
for (int i = 0; i < resourceIdList.size(); i++) {
pstmt.setLong(i + 1, resourceIdList.get(i));
}
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Long capacityPodId = null;
Long capacityClusterId = null;
if (rs.getLong(7) != 0)
capacityPodId = rs.getLong(7);
if (rs.getLong(8) != 0)
capacityClusterId = rs.getLong(8);
SummedCapacity summedCapacity = new SummedCapacity(rs.getLong(1), rs.getLong(2), rs.getLong(3), rs.getFloat(4), (short) rs.getLong(5), rs.getLong(6), capacityPodId, capacityClusterId);
results.add(summedCapacity);
}
HashMap<String, SummedCapacity> capacityMap = new HashMap<String, SummedCapacity>();
for (SummedCapacity result : results) {
String key;
if (zoneId != null || podId != null) {
key = String.valueOf(result.getCapacityType());
} else {
// sum the values based on the zoneId.
key = String.valueOf(result.getDataCenterId()) + String.valueOf(result.getCapacityType());
}
SummedCapacity tempCapacity = null;
if (capacityMap.containsKey(key)) {
tempCapacity = capacityMap.get(key);
tempCapacity.setUsedCapacity(tempCapacity.getUsedCapacity() + result.getUsedCapacity());
tempCapacity.setReservedCapacity(tempCapacity.getReservedCapacity() + result.getReservedCapacity());
tempCapacity.setSumTotal(tempCapacity.getTotalCapacity() + result.getTotalCapacity());
} else {
capacityMap.put(key, result);
}
tempCapacity = capacityMap.get(key);
tempCapacity.setPodId(podId);
tempCapacity.setClusterId(clusterId);
}
List<SummedCapacity> summedCapacityList = new ArrayList<SummedCapacity>();
for (String key : capacityMap.keySet()) {
summedCapacityList.add(capacityMap.get(key));
}
return summedCapacityList;
} catch (SQLException e) {
throw new CloudRuntimeException("DB Exception on: " + sql, e);
} catch (Throwable e) {
throw new CloudRuntimeException("Caught: " + sql, e);
}
}
use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.
the class ResourceCountDaoImpl method createResourceCounts.
@Override
@DB
public void createResourceCounts(long ownerId, ResourceLimit.ResourceOwnerType ownerType) {
TransactionLegacy txn = TransactionLegacy.currentTxn();
txn.start();
ResourceType[] resourceTypes = Resource.ResourceType.values();
for (ResourceType resourceType : resourceTypes) {
if (!resourceType.supportsOwner(ownerType)) {
continue;
}
ResourceCountVO resourceCountVO = new ResourceCountVO(resourceType, 0, ownerId, ownerType);
persist(resourceCountVO);
}
txn.commit();
}
use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.
the class ClusterDetailsDaoImpl method persist.
@Override
public void persist(long clusterId, Map<String, String> details) {
TransactionLegacy txn = TransactionLegacy.currentTxn();
txn.start();
SearchCriteria<ClusterDetailsVO> sc = ClusterSearch.create();
sc.setParameters("clusterId", clusterId);
expunge(sc);
for (Map.Entry<String, String> detail : details.entrySet()) {
String value = detail.getValue();
if ("password".equals(detail.getKey())) {
value = DBEncryptionUtil.encrypt(value);
}
ClusterDetailsVO vo = new ClusterDetailsVO(clusterId, detail.getKey(), value);
persist(vo);
}
txn.commit();
}
use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.
the class ClusterDetailsDaoImpl method persist.
@Override
public void persist(long clusterId, String name, String value) {
TransactionLegacy txn = TransactionLegacy.currentTxn();
txn.start();
SearchCriteria<ClusterDetailsVO> sc = DetailSearch.create();
sc.setParameters("clusterId", clusterId);
sc.setParameters("name", name);
expunge(sc);
ClusterDetailsVO vo = new ClusterDetailsVO(clusterId, name, value);
persist(vo);
txn.commit();
}
Aggregations