use of com.datastax.oss.driver.api.core.servererrors.QueryExecutionException in project automatiko-engine by automatiko-io.
the class CassandraProcessInstances method create.
@Override
public void create(String id, ProcessInstance instance) {
String resolvedId = resolveId(id, instance);
if (isActive(instance)) {
LOGGER.debug("create() called for instance {}", resolvedId);
byte[] data = codec.encode(marshaller.marhsallProcessInstance(instance));
if (data == null) {
return;
}
Collection<String> tags = new LinkedHashSet<>(instance.tags().values());
tags.add(resolvedId);
if (instance.businessKey() != null) {
tags.add(instance.businessKey());
}
Insert insert = insertInto(config.keyspace().orElse("automatiko"), tableName).value(INSTANCE_ID_FIELD, literal(resolvedId)).value(VERSION_FIELD, literal(((AbstractProcessInstance<?>) instance).getVersionTracker())).value(STATUS_FIELD, literal(((AbstractProcessInstance<?>) instance).status())).value(CONTENT_FIELD, bindMarker()).value(TAGS_FIELD, bindMarker()).ifNotExists();
try {
ResultSet rs = cqlSession.execute(cqlSession.prepare(insert.build()).bind(ByteBuffer.wrap(data), tags));
if (!rs.wasApplied()) {
throw new ProcessInstanceDuplicatedException(id);
}
} catch (QueryExecutionException e) {
throw new ProcessInstanceDuplicatedException(id);
} finally {
cachedInstances.remove(resolvedId);
cachedInstances.remove(id);
disconnect(instance);
}
} else if (isPending(instance)) {
if (cachedInstances.putIfAbsent(resolvedId, instance) != null) {
throw new ProcessInstanceDuplicatedException(id);
}
} else {
cachedInstances.remove(resolvedId);
cachedInstances.remove(id);
}
}
Aggregations