Search in sources :

Example 16 with EntityExistsException

use of javax.persistence.EntityExistsException in project cloudstack by apache.

the class OvsTunnelManagerImpl method createTunnelRecord.

@DB
protected OvsTunnelNetworkVO createTunnelRecord(long from, long to, long networkId, int key) {
    OvsTunnelNetworkVO ta = null;
    try {
        ta = new OvsTunnelNetworkVO(from, to, key, networkId);
        OvsTunnelNetworkVO lock = _tunnelNetworkDao.acquireInLockTable(Long.valueOf(1));
        if (lock == null) {
            s_logger.warn("Cannot lock table ovs_tunnel_account");
            return null;
        }
        _tunnelNetworkDao.persist(ta);
        _tunnelNetworkDao.releaseFromLockTable(lock.getId());
    } catch (EntityExistsException e) {
        s_logger.debug("A record for the tunnel from " + from + " to " + to + " already exists");
    }
    return ta;
}
Also used : OvsTunnelNetworkVO(com.cloud.network.ovs.dao.OvsTunnelNetworkVO) EntityExistsException(javax.persistence.EntityExistsException) DB(com.cloud.utils.db.DB)

Example 17 with EntityExistsException

use of javax.persistence.EntityExistsException in project cloudstack by apache.

the class OvsTunnelManagerImpl method createInterfaceRecord.

@DB
protected OvsTunnelInterfaceVO createInterfaceRecord(String ip, String netmask, String mac, long hostId, String label) {
    OvsTunnelInterfaceVO ti = null;
    try {
        ti = new OvsTunnelInterfaceVO(ip, netmask, mac, hostId, label);
        // TODO: Is locking really necessary here?
        OvsTunnelInterfaceVO lock = _tunnelInterfaceDao.acquireInLockTable(Long.valueOf(1));
        if (lock == null) {
            s_logger.warn("Cannot lock table ovs_tunnel_account");
            return null;
        }
        _tunnelInterfaceDao.persist(ti);
        _tunnelInterfaceDao.releaseFromLockTable(lock.getId());
    } catch (EntityExistsException e) {
        s_logger.debug("A record for the interface for network " + label + " on host id " + hostId + " already exists");
    }
    return ti;
}
Also used : OvsTunnelInterfaceVO(com.cloud.network.ovs.dao.OvsTunnelInterfaceVO) EntityExistsException(javax.persistence.EntityExistsException) DB(com.cloud.utils.db.DB)

Example 18 with EntityExistsException

use of javax.persistence.EntityExistsException in project cloudstack by apache.

the class GenericDaoBase method update.

public int update(UpdateBuilder ub, final SearchCriteria<?> sc, Integer rows) {
    StringBuilder sql = null;
    PreparedStatement pstmt = null;
    final TransactionLegacy txn = TransactionLegacy.currentTxn();
    try {
        final String searchClause = sc.getWhereClause();
        sql = ub.toSql(_tables);
        if (sql == null) {
            return 0;
        }
        sql.append(searchClause);
        if (rows != null) {
            sql.append(" LIMIT ").append(rows);
        }
        txn.start();
        pstmt = txn.prepareAutoCloseStatement(sql.toString());
        Collection<Ternary<Attribute, Boolean, Object>> changes = ub.getChanges();
        int i = 1;
        for (final Ternary<Attribute, Boolean, Object> value : changes) {
            prepareAttribute(i++, pstmt, value.first(), value.third());
        }
        for (Pair<Attribute, Object> value : sc.getValues()) {
            prepareAttribute(i++, pstmt, value.first(), value.second());
        }
        int result = pstmt.executeUpdate();
        txn.commit();
        ub.clear();
        return result;
    } catch (final SQLException e) {
        if (e.getSQLState().equals("23000") && e.getErrorCode() == 1062) {
            throw new EntityExistsException("Entity already exists ", e);
        }
        throw new CloudRuntimeException("DB Exception on: " + pstmt, e);
    }
}
Also used : Ternary(com.cloud.utils.Ternary) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) EntityExistsException(javax.persistence.EntityExistsException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 19 with EntityExistsException

use of javax.persistence.EntityExistsException in project cloudstack by apache.

the class GenericDaoBase method persist.

@Override
@SuppressWarnings("unchecked")
public T persist(final T entity) {
    if (Enhancer.isEnhanced(entity.getClass())) {
        if (_idField != null) {
            ID id;
            try {
                id = (ID) _idField.get(entity);
            } catch (IllegalAccessException e) {
                throw new CloudRuntimeException("How can it be illegal access...come on", e);
            }
            update(id, entity);
            return entity;
        }
        assert false : "Can't call persit if you don't have primary key";
    }
    ID id = null;
    final TransactionLegacy txn = TransactionLegacy.currentTxn();
    PreparedStatement pstmt = null;
    String sql = null;
    try {
        txn.start();
        for (final Pair<String, Attribute[]> pair : _insertSqls) {
            sql = pair.first();
            final Attribute[] attrs = pair.second();
            pstmt = txn.prepareAutoCloseStatement(sql, Statement.RETURN_GENERATED_KEYS);
            int index = 1;
            index = prepareAttributes(pstmt, entity, attrs, index);
            pstmt.executeUpdate();
            final ResultSet rs = pstmt.getGeneratedKeys();
            if (id == null) {
                if (rs != null && rs.next()) {
                    id = (ID) rs.getObject(1);
                }
                try {
                    if (_idField != null) {
                        if (id != null) {
                            _idField.set(entity, id);
                        } else {
                            id = (ID) _idField.get(entity);
                        }
                    }
                } catch (final IllegalAccessException e) {
                    throw new CloudRuntimeException("Yikes! ", e);
                }
            }
        }
        if (_ecAttributes != null && _ecAttributes.size() > 0) {
            HashMap<Attribute, Object> ecAttributes = new HashMap<Attribute, Object>();
            for (Attribute attr : _ecAttributes) {
                Object ec = attr.field.get(entity);
                if (ec != null) {
                    ecAttributes.put(attr, ec);
                }
            }
            insertElementCollection(entity, _idAttributes.get(_table)[0], id, ecAttributes);
        }
        txn.commit();
    } catch (final SQLException e) {
        if (e.getSQLState().equals("23000") && e.getErrorCode() == 1062) {
            throw new EntityExistsException("Entity already exists: ", e);
        } else {
            throw new CloudRuntimeException("DB Exception on: " + pstmt, e);
        }
    } catch (IllegalArgumentException e) {
        throw new CloudRuntimeException("Problem with getting the ec attribute ", e);
    } catch (IllegalAccessException e) {
        throw new CloudRuntimeException("Problem with getting the ec attribute ", e);
    }
    return _idField != null ? findByIdIncludingRemoved(id) : null;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) EntityExistsException(javax.persistence.EntityExistsException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResultSet(java.sql.ResultSet) UUID(java.util.UUID) AttributeOverride(javax.persistence.AttributeOverride)

Example 20 with EntityExistsException

use of javax.persistence.EntityExistsException in project API by ca-cwds.

the class AllegationPerpetratorHistoryService method create.

/**
   * {@inheritDoc}
   * 
   * @see gov.ca.cwds.rest.services.CrudsService#create(gov.ca.cwds.rest.api.Request)
   */
@Override
public PostedAllegationPerpetratorHistory create(Request request) {
    assert request instanceof gov.ca.cwds.rest.api.domain.cms.AllegationPerpetratorHistory;
    gov.ca.cwds.rest.api.domain.cms.AllegationPerpetratorHistory allegationPerpetratorHistory = (gov.ca.cwds.rest.api.domain.cms.AllegationPerpetratorHistory) request;
    try {
        String lastUpdatedId = staffPersonIdRetriever.getStaffPersonId();
        AllegationPerpetratorHistory managed = new AllegationPerpetratorHistory(CmsKeyIdGenerator.cmsIdGenertor(lastUpdatedId), allegationPerpetratorHistory, lastUpdatedId);
        managed = allegationPerpetratorHistoryDao.create(managed);
        return new PostedAllegationPerpetratorHistory(managed);
    } catch (EntityExistsException e) {
        LOGGER.info("AllegationPerpetratorHistory already exists : {}", allegationPerpetratorHistory);
        throw new ServiceException(e);
    }
}
Also used : PostedAllegationPerpetratorHistory(gov.ca.cwds.rest.api.domain.cms.PostedAllegationPerpetratorHistory) AllegationPerpetratorHistory(gov.ca.cwds.data.persistence.cms.AllegationPerpetratorHistory) PostedAllegationPerpetratorHistory(gov.ca.cwds.rest.api.domain.cms.PostedAllegationPerpetratorHistory) EntityExistsException(javax.persistence.EntityExistsException) ServiceException(gov.ca.cwds.rest.services.ServiceException)

Aggregations

EntityExistsException (javax.persistence.EntityExistsException)24 ServiceException (gov.ca.cwds.rest.services.ServiceException)13 SQLException (java.sql.SQLException)5 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)4 StaffPerson (gov.ca.cwds.data.persistence.cms.StaffPerson)4 PreparedStatement (java.sql.PreparedStatement)4 DB (com.cloud.utils.db.DB)3 PersistenceException (javax.persistence.PersistenceException)3 Ternary (com.cloud.utils.Ternary)2 ResultSet (java.sql.ResultSet)2 HashMap (java.util.HashMap)2 UUID (java.util.UUID)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 AttributeOverride (javax.persistence.AttributeOverride)2 EntityNotFoundException (javax.persistence.EntityNotFoundException)2 NoResultException (javax.persistence.NoResultException)2 NonUniqueResultException (javax.persistence.NonUniqueResultException)2 OptimisticLockException (javax.persistence.OptimisticLockException)2 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 CiscoAsa1000vDevice (com.cloud.network.cisco.CiscoAsa1000vDevice)1