Search in sources :

Example 6 with TransactionLegacy

use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.

the class AlertDaoImpl method archiveAlert.

@Override
public boolean archiveAlert(List<Long> ids, String type, Date startDate, Date endDate, Long zoneId) {
    SearchCriteria<AlertVO> sc = AlertSearchByIdsAndType.create();
    if (ids != null) {
        sc.setParameters("id", ids.toArray(new Object[ids.size()]));
    }
    if (type != null) {
        sc.setParameters("type", type);
    }
    if (zoneId != null) {
        sc.setParameters("data_center_id", zoneId);
    }
    if (startDate != null && endDate != null) {
        sc.setParameters("createdDateB", startDate, endDate);
    } else if (endDate != null) {
        sc.setParameters("createdDateL", endDate);
    }
    sc.setParameters("archived", false);
    boolean result = true;
    ;
    List<AlertVO> alerts = listBy(sc);
    if (ids != null && alerts.size() < ids.size()) {
        result = false;
        return result;
    }
    if (alerts != null && !alerts.isEmpty()) {
        TransactionLegacy txn = TransactionLegacy.currentTxn();
        txn.start();
        for (AlertVO alert : alerts) {
            alert = lockRow(alert.getId(), true);
            alert.setArchived(true);
            update(alert.getId(), alert);
            txn.commit();
        }
        txn.close();
    }
    return result;
}
Also used : AlertVO(com.cloud.alert.AlertVO) TransactionLegacy(com.cloud.utils.db.TransactionLegacy)

Example 7 with TransactionLegacy

use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.

the class CapacityDaoImpl method findByClusterPodZone.

@Override
public List<SummedCapacity> findByClusterPodZone(Long zoneId, Long podId, Long clusterId) {
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    PreparedStatement pstmt = null;
    List<SummedCapacity> result = 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);
    }
    sql.append(LIST_CAPACITY_GROUP_BY_CAPACITY_PART2);
    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()) {
            SummedCapacity summedCapacity = new SummedCapacity(rs.getLong(1), rs.getLong(2), rs.getLong(3), (short) rs.getLong(5), null, null, rs.getLong(6));
            result.add(summedCapacity);
        }
        return result;
    } catch (SQLException e) {
        throw new CloudRuntimeException("DB Exception on: " + sql, e);
    } catch (Throwable e) {
        throw new CloudRuntimeException("Caught: " + sql, e);
    }
}
Also used : 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)

Example 8 with TransactionLegacy

use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.

the class CapacityDaoImpl method orderPodsByAggregateCapacity.

@Override
public Pair<List<Long>, Map<Long, Double>> orderPodsByAggregateCapacity(long zoneId, short capacityTypeForOrdering) {
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    PreparedStatement pstmt = null;
    List<Long> result = new ArrayList<Long>();
    Map<Long, Double> podCapacityMap = new HashMap<Long, Double>();
    StringBuilder sql = null;
    try {
        if (capacityTypeForOrdering == Capacity.CAPACITY_TYPE_CPU | capacityTypeForOrdering == Capacity.CAPACITY_TYPE_MEMORY) {
            sql = new StringBuilder(ORDER_PODS_BY_AGGREGATE_OVERCOMMIT_CAPACITY);
            pstmt = txn.prepareAutoCloseStatement(sql.toString());
            pstmt.setLong(1, zoneId);
            pstmt.setShort(2, capacityTypeForOrdering);
            if (capacityTypeForOrdering == Capacity.CAPACITY_TYPE_CPU) {
                pstmt.setString(3, "cpuOvercommitRatio");
            } else if (capacityTypeForOrdering == Capacity.CAPACITY_TYPE_MEMORY) {
                pstmt.setString(3, "memoryOvercommitRatio");
            }
        } else {
            sql = new StringBuilder(ORDER_PODS_BY_AGGREGATE_CAPACITY);
            pstmt = txn.prepareAutoCloseStatement(sql.toString());
            pstmt.setLong(1, zoneId);
            pstmt.setShort(2, capacityTypeForOrdering);
        }
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            Long podId = rs.getLong(1);
            result.add(podId);
            podCapacityMap.put(podId, rs.getDouble(2));
        }
        return new Pair<List<Long>, Map<Long, Double>>(result, podCapacityMap);
    } 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 9 with TransactionLegacy

use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.

the class CapacityDaoImpl method listCapacitiesGroupedByLevelAndType.

@Override
public List<SummedCapacity> listCapacitiesGroupedByLevelAndType(Integer capacityType, Long zoneId, Long podId, Long clusterId, int level, Long limit) {
    StringBuilder finalQuery = new StringBuilder();
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    PreparedStatement pstmt = null;
    List<SummedCapacity> results = new ArrayList<SummedCapacity>();
    List<Long> resourceIdList = new ArrayList<Long>();
    switch(level) {
        case // List all the capacities grouped by zone, capacity Type
        1:
            finalQuery.append(LIST_CAPACITY_GROUP_BY_ZONE_TYPE_PART1);
            break;
        case // List all the capacities grouped by pod, capacity Type
        2:
            finalQuery.append(LIST_CAPACITY_GROUP_BY_POD_TYPE_PART1);
            break;
        case // List all the capacities grouped by cluster, capacity Type
        3:
            finalQuery.append(LIST_CAPACITY_GROUP_BY_CLUSTER_TYPE_PART1);
            break;
    }
    if (zoneId != null) {
        finalQuery.append(" AND data_center_id = ?");
        resourceIdList.add(zoneId);
    }
    if (podId != null) {
        finalQuery.append(" AND pod_id = ?");
        resourceIdList.add(podId);
    }
    if (clusterId != null) {
        finalQuery.append(" AND cluster_id = ?");
        resourceIdList.add(clusterId);
    }
    if (capacityType != null) {
        finalQuery.append(" AND capacity_type = ?");
        resourceIdList.add(capacityType.longValue());
    }
    switch(level) {
        case // List all the capacities grouped by zone, capacity Type
        1:
            finalQuery.append(LIST_CAPACITY_GROUP_BY_ZONE_TYPE_PART2);
            break;
        case // List all the capacities grouped by pod, capacity Type
        2:
            finalQuery.append(LIST_CAPACITY_GROUP_BY_POD_TYPE_PART2);
            break;
        case // List all the capacities grouped by cluster, capacity Type
        3:
            finalQuery.append(LIST_CAPACITY_GROUP_BY_CLUSTER_TYPE_PART2);
            break;
    }
    finalQuery.append("?");
    resourceIdList.add((long) limit);
    try {
        pstmt = txn.prepareAutoCloseStatement(finalQuery.toString());
        for (int i = 0; i < resourceIdList.size(); i++) {
            pstmt.setLong(1 + i, resourceIdList.get(i));
        }
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            Long capacityPodId = null;
            Long capacityClusterId = null;
            if (level != 1 && rs.getLong(6) != 0)
                capacityPodId = rs.getLong(6);
            if (level == 3 && rs.getLong(7) != 0)
                capacityClusterId = rs.getLong(7);
            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);
        }
        return results;
    } catch (SQLException e) {
        throw new CloudRuntimeException("DB Exception on: " + finalQuery, e);
    } catch (Throwable e) {
        throw new CloudRuntimeException("Caught: " + finalQuery, e);
    }
}
Also used : 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)

Example 10 with TransactionLegacy

use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.

the class CapacityDaoImpl method findCapacityBy.

/*public static String preparePlaceHolders(int length) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < length;) {
            builder.append("?");
            if (++i < length) {
                builder.append(",");
            }
        }
        return builder.toString();
    }

    public static void setValues(PreparedStatement preparedStatement, Object... values) throws SQLException {
        for (int i = 0; i < values.length; i++) {
            preparedStatement.setObject(i + 1, values[i]);
        }
    }*/
@Override
public List<SummedCapacity> findCapacityBy(Integer capacityType, Long zoneId, Long podId, Long clusterId, String resourceState) {
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    PreparedStatement pstmt = null;
    List<SummedCapacity> result = new ArrayList<SummedCapacity>();
    StringBuilder sql = new StringBuilder(LIST_CAPACITY_BY_RESOURCE_STATE);
    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());
    }
    try {
        pstmt = txn.prepareAutoCloseStatement(sql.toString());
        pstmt.setString(1, resourceState);
        pstmt.setString(2, resourceState);
        pstmt.setString(3, resourceState);
        pstmt.setString(4, resourceState);
        for (int i = 0; i < resourceIdList.size(); i++) {
            pstmt.setLong(5 + i, resourceIdList.get(i));
        }
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            SummedCapacity summedCapacity = new SummedCapacity(rs.getLong(2), rs.getLong(3), rs.getLong(4), (short) rs.getLong(5), null, null, rs.getLong(1));
            result.add(summedCapacity);
        }
        return result;
    } catch (SQLException e) {
        throw new CloudRuntimeException("DB Exception on: " + sql, e);
    } catch (Throwable e) {
        throw new CloudRuntimeException("Caught: " + sql, e);
    }
}
Also used : 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)

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