Search in sources :

Example 96 with TransactionLegacy

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);
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) SQLException(java.sql.SQLException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 97 with TransactionLegacy

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);
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) TransactionLegacy(com.cloud.utils.db.TransactionLegacy) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResultSet(java.sql.ResultSet) Pair(com.cloud.utils.Pair)

Example 98 with TransactionLegacy

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);
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) TransactionLegacy(com.cloud.utils.db.TransactionLegacy) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResultSet(java.sql.ResultSet) Pair(com.cloud.utils.Pair)

Example 99 with TransactionLegacy

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());
        }
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SQLException(java.sql.SQLException)

Example 100 with TransactionLegacy

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());
        }
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SQLException(java.sql.SQLException)

Aggregations

TransactionLegacy (com.cloud.utils.db.TransactionLegacy)368 PreparedStatement (java.sql.PreparedStatement)174 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)149 SQLException (java.sql.SQLException)133 ResultSet (java.sql.ResultSet)102 ArrayList (java.util.ArrayList)98 DB (com.cloud.utils.db.DB)95 ConfigurationException (javax.naming.ConfigurationException)54 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)35 Date (java.util.Date)34 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)31 HashMap (java.util.HashMap)29 URISyntaxException (java.net.URISyntaxException)28 AccountVO (com.cloud.user.AccountVO)21 CloudException (com.cloud.exception.CloudException)20 Account (com.cloud.user.Account)20 Field (java.lang.reflect.Field)19 MockVolumeVO (com.cloud.simulator.MockVolumeVO)18 AgentManager (com.cloud.agent.AgentManager)13 IPAddressDao (com.cloud.network.dao.IPAddressDao)13