use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.
the class VMInstanceDaoImpl method listHostIdsByVmCount.
@Override
public List<Long> listHostIdsByVmCount(long dcId, Long podId, Long clusterId, long accountId) {
TransactionLegacy txn = TransactionLegacy.currentTxn();
PreparedStatement pstmt = null;
List<Long> result = new ArrayList<Long>();
try {
String sql = ORDER_HOSTS_NUMBER_OF_VMS_FOR_ACCOUNT;
if (podId != null) {
sql = sql + " AND host.pod_id = ? ";
}
if (clusterId != null) {
sql = sql + " AND host.cluster_id = ? ";
}
sql = sql + ORDER_HOSTS_NUMBER_OF_VMS_FOR_ACCOUNT_PART2;
pstmt = txn.prepareAutoCloseStatement(sql);
pstmt.setLong(1, accountId);
pstmt.setLong(2, dcId);
if (podId != null) {
pstmt.setLong(3, podId);
}
if (clusterId != null) {
pstmt.setLong(4, clusterId);
}
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
result.add(rs.getLong(1));
}
return result;
} catch (SQLException e) {
throw new CloudRuntimeException("DB Exception on: " + ORDER_PODS_NUMBER_OF_VMS_FOR_ACCOUNT, e);
} catch (Throwable e) {
throw new CloudRuntimeException("Caught: " + ORDER_PODS_NUMBER_OF_VMS_FOR_ACCOUNT, e);
}
}
use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.
the class VMInstanceDaoImpl method listClusterIdsInZoneByVmCount.
@Override
public Pair<List<Long>, Map<Long, Double>> listClusterIdsInZoneByVmCount(long zoneId, long accountId) {
TransactionLegacy txn = TransactionLegacy.currentTxn();
PreparedStatement pstmt = null;
List<Long> result = new ArrayList<Long>();
Map<Long, Double> clusterVmCountMap = new HashMap<Long, Double>();
StringBuilder sql = new StringBuilder(ORDER_CLUSTERS_NUMBER_OF_VMS_FOR_ACCOUNT_PART1);
sql.append("host.data_center_id = ?");
sql.append(ORDER_CLUSTERS_NUMBER_OF_VMS_FOR_ACCOUNT_PART2);
try {
pstmt = txn.prepareAutoCloseStatement(sql.toString());
pstmt.setLong(1, accountId);
pstmt.setLong(2, zoneId);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Long clusterId = rs.getLong(1);
result.add(clusterId);
clusterVmCountMap.put(clusterId, rs.getDouble(2));
}
return new Pair<List<Long>, Map<Long, Double>>(result, clusterVmCountMap);
} 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 VMInstanceDaoImpl method listClusterIdsInPodByVmCount.
@Override
public Pair<List<Long>, Map<Long, Double>> listClusterIdsInPodByVmCount(long podId, long accountId) {
TransactionLegacy txn = TransactionLegacy.currentTxn();
PreparedStatement pstmt = null;
List<Long> result = new ArrayList<Long>();
Map<Long, Double> clusterVmCountMap = new HashMap<Long, Double>();
StringBuilder sql = new StringBuilder(ORDER_CLUSTERS_NUMBER_OF_VMS_FOR_ACCOUNT_PART1);
sql.append("host.pod_id = ?");
sql.append(ORDER_CLUSTERS_NUMBER_OF_VMS_FOR_ACCOUNT_PART2);
try {
pstmt = txn.prepareAutoCloseStatement(sql.toString());
pstmt.setLong(1, accountId);
pstmt.setLong(2, podId);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Long clusterId = rs.getLong(1);
result.add(clusterId);
clusterVmCountMap.put(clusterId, rs.getDouble(2));
}
return new Pair<List<Long>, Map<Long, Double>>(result, clusterVmCountMap);
} 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 UserVmDaoImpl method listVmDetails.
@Override
public Hashtable<Long, UserVmData> listVmDetails(Hashtable<Long, UserVmData> userVmDataHash) {
TransactionLegacy txn = TransactionLegacy.currentTxn();
try {
int curr_index = 0;
List<UserVmData> userVmDataList = new ArrayList(userVmDataHash.values());
if (userVmDataList.size() > VM_DETAILS_BATCH_SIZE) {
try (PreparedStatement pstmt = txn.prepareStatement(VM_DETAILS + getQueryBatchAppender(VM_DETAILS_BATCH_SIZE))) {
while ((curr_index + VM_DETAILS_BATCH_SIZE) <= userVmDataList.size()) {
// set the vars value
for (int k = 1, j = curr_index; j < curr_index + VM_DETAILS_BATCH_SIZE; j++, k++) {
pstmt.setLong(k, userVmDataList.get(j).getId());
}
try (ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) {
long vm_id = rs.getLong("vm_instance.id");
//check if the entry is already there
UserVmData uvm = userVmDataHash.get(vm_id);
if (uvm == null) {
uvm = new UserVmData();
uvm.setId(vm_id);
}
// initialize the data with this row
setUserVmData(uvm, rs);
}
} catch (Exception e) {
s_logger.error("listVmDetails:Exception:" + e.getMessage());
throw new CloudRuntimeException("listVmDetails: Exception:" + e.getMessage(), e);
}
curr_index += VM_DETAILS_BATCH_SIZE;
}
} catch (Exception e) {
s_logger.error("listVmDetails:Exception:" + e.getMessage());
throw new CloudRuntimeException("listVmDetails: Exception:" + e.getMessage(), e);
}
}
if (curr_index < userVmDataList.size()) {
int batch_size = (userVmDataList.size() - curr_index);
try (PreparedStatement vm_details_pstmt = txn.prepareStatement(VM_DETAILS + getQueryBatchAppender(batch_size))) {
// set the vars value
for (int k = 1, j = curr_index; j < curr_index + batch_size; j++, k++) {
vm_details_pstmt.setLong(k, userVmDataList.get(j).getId());
}
try (ResultSet rs = vm_details_pstmt.executeQuery()) {
while (rs.next()) {
long vm_id = rs.getLong("vm_instance.id");
//check if the entry is already there
UserVmData uvm = userVmDataHash.get(vm_id);
if (uvm == null) {
uvm = new UserVmData();
uvm.setId(vm_id);
}
// initialize the data with this row
setUserVmData(uvm, rs);
}
} catch (Exception e) {
s_logger.error("listVmDetails: Exception:" + e.getMessage());
throw new CloudRuntimeException("listVmDetails: Exception:" + e.getMessage(), e);
}
} catch (Exception e) {
s_logger.error("listVmDetails:Exception:" + e.getMessage());
throw new CloudRuntimeException("listVmDetails: Exception:" + e.getMessage(), e);
}
}
txn.commit();
return userVmDataHash;
} catch (Exception e) {
s_logger.error("listVmDetails:Exception:" + e.getMessage());
throw new CloudRuntimeException("listVmDetails:Exception : ", e);
} finally {
try {
if (txn != null) {
txn.close();
}
} catch (Exception e) {
s_logger.error("listVmDetails:Exception:" + e.getMessage());
}
}
}
use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.
the class UserVmDaoImpl method listPodIdsHavingVmsforAccount.
@Override
public List<Long> listPodIdsHavingVmsforAccount(long zoneId, long accountId) {
TransactionLegacy txn = TransactionLegacy.currentTxn();
List<Long> result = new ArrayList<Long>();
String sql = LIST_PODS_HAVING_VMS_FOR_ACCOUNT;
try (PreparedStatement pstmt = txn.prepareStatement(sql)) {
pstmt.setLong(1, zoneId);
pstmt.setLong(2, accountId);
try (ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) {
result.add(rs.getLong(1));
}
} catch (Exception e) {
s_logger.error("listPodIdsHavingVmsforAccount:Exception: " + e.getMessage());
throw new CloudRuntimeException("listPodIdsHavingVmsforAccount:Exception: " + e.getMessage(), e);
}
txn.commit();
return result;
} catch (Exception e) {
s_logger.error("listPodIdsHavingVmsforAccount:Exception : " + e.getMessage());
throw new CloudRuntimeException("listPodIdsHavingVmsforAccount:Exception: " + e.getMessage(), e);
} finally {
try {
if (txn != null) {
txn.close();
}
} catch (Exception e) {
s_logger.error("listVmDetails:Exception:" + e.getMessage());
}
}
}
Aggregations