Search in sources :

Example 1 with TransactionLegacy

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

Example 2 with TransactionLegacy

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

Example 3 with TransactionLegacy

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();
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) ResourceCountVO(com.cloud.configuration.ResourceCountVO) ResourceType(com.cloud.configuration.Resource.ResourceType) DB(com.cloud.utils.db.DB)

Example 4 with TransactionLegacy

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();
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with TransactionLegacy

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();
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy)

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