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