Search in sources :

Example 56 with TransactionLegacy

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

the class HighAvailabilityDaoImpl method take.

@Override
public HaWorkVO take(final long serverId) {
    final TransactionLegacy txn = TransactionLegacy.currentTxn();
    try {
        final SearchCriteria<HaWorkVO> sc = TBASearch.create();
        sc.setParameters("time", System.currentTimeMillis() >> 10);
        sc.setParameters("step", Step.Done, Step.Cancelled);
        final Filter filter = new Filter(HaWorkVO.class, null, true, 0l, 1l);
        txn.start();
        final List<HaWorkVO> vos = lockRows(sc, filter, true);
        if (vos.size() == 0) {
            txn.commit();
            return null;
        }
        final HaWorkVO work = vos.get(0);
        work.setServerId(serverId);
        work.setDateTaken(new Date());
        update(work.getId(), work);
        txn.commit();
        return work;
    } catch (final Throwable e) {
        throw new CloudRuntimeException("Unable to execute take", e);
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) Filter(com.cloud.utils.db.Filter) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) HaWorkVO(com.cloud.ha.HaWorkVO) Date(java.util.Date)

Example 57 with TransactionLegacy

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

the class VmRulesetLogDaoImpl method createOrUpdateUsingBatch.

protected int createOrUpdateUsingBatch(Set<Long> workItems) {
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    PreparedStatement stmtInsert = null;
    int[] queryResult = null;
    int count = 0;
    boolean success = true;
    try {
        stmtInsert = txn.prepareAutoCloseStatement(InsertOrUpdateSQl);
        txn.start();
        for (Long vmId : workItems) {
            stmtInsert.setLong(1, vmId);
            stmtInsert.addBatch();
            count++;
            if (count % 16 == 0) {
                queryResult = stmtInsert.executeBatch();
                stmtInsert.clearBatch();
            }
        }
        queryResult = stmtInsert.executeBatch();
        txn.commit();
        if (s_logger.isTraceEnabled())
            s_logger.trace("Updated or inserted " + workItems.size() + " log items");
    } catch (SQLException e) {
        s_logger.warn("Failed to execute batch update statement for ruleset log: ", e);
        txn.rollback();
        success = false;
    }
    if (!success && queryResult != null) {
        Long[] arrayItems = new Long[workItems.size()];
        workItems.toArray(arrayItems);
        for (int i = 0; i < queryResult.length; i++) {
            if (queryResult[i] < 0) {
                s_logger.debug("Batch query update failed for vm " + arrayItems[i]);
            }
        }
    }
    return count;
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement)

Example 58 with TransactionLegacy

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

the class NetworkACLItemCidrsDaoImpl method persist.

/* (non-Javadoc)
     * @see com.cloud.network.dao.NetworkAclItemCidrsDao#persist(long, java.util.List)
     */
@Override
public void persist(long networkACLItemId, List<String> cidrs) {
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    txn.start();
    for (String cidr : cidrs) {
        NetworkACLItemCidrsVO vo = new NetworkACLItemCidrsVO(networkACLItemId, cidr);
        persist(vo);
    }
    txn.commit();
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) NetworkACLItemCidrsVO(com.cloud.network.vpc.NetworkACLItemCidrsVO)

Example 59 with TransactionLegacy

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

the class IPAddressDaoImpl method countIPs.

@Override
@DB
public int countIPs(long dcId, Long accountId, String vlanId, String vlanGateway, String vlanNetmask) {
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    int ipCount = 0;
    try {
        String sql = "SELECT count(*) FROM user_ip_address u INNER JOIN vlan v on (u.vlan_db_id = v.id AND v.data_center_id = ? AND v.vlan_id = ? AND v.vlan_gateway = ? AND v.vlan_netmask = ? AND u.account_id = ?)";
        PreparedStatement pstmt = txn.prepareAutoCloseStatement(sql);
        pstmt.setLong(1, dcId);
        pstmt.setString(2, vlanId);
        pstmt.setString(3, vlanGateway);
        pstmt.setString(4, vlanNetmask);
        pstmt.setLong(5, accountId);
        ResultSet rs = pstmt.executeQuery();
        if (rs.next()) {
            ipCount = rs.getInt(1);
        }
    } catch (Exception e) {
        s_logger.warn("Exception counting IP addresses", e);
    }
    return ipCount;
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DB(com.cloud.utils.db.DB)

Example 60 with TransactionLegacy

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

the class NetworkDaoImpl method persistNetworkServiceProviders.

@Override
@DB
public void persistNetworkServiceProviders(final long networkId, final Map<String, String> serviceProviderMap) {
    final TransactionLegacy txn = TransactionLegacy.currentTxn();
    txn.start();
    for (final String service : serviceProviderMap.keySet()) {
        final NetworkServiceMapVO serviceMap = new NetworkServiceMapVO(networkId, Service.getService(service), Provider.getProvider(serviceProviderMap.get(service)));
        _ntwkSvcMap.persist(serviceMap);
    }
    txn.commit();
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) DB(com.cloud.utils.db.DB)

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