use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.
the class DataDomainIndirectDiffBuilder method arcCreated.
@Override
public void arcCreated(Object nodeId, Object targetNodeId, Object arcId) {
ObjEntity entity = resolver.getObjEntity(((ObjectId) nodeId).getEntityName());
ObjRelationship relationship = entity.getRelationship(arcId.toString());
if (relationship.isSourceIndependentFromTargetChange()) {
ObjectId nodeObjectId = (ObjectId) nodeId;
if (!nodeObjectId.isTemporary()) {
indirectModifications.add(nodeObjectId);
}
if (relationship.isFlattened()) {
if (relationship.isReadOnly()) {
throw new CayenneRuntimeException("Cannot set the read-only flattened relationship '%s' in ObjEntity '%s'.", relationship.getName(), relationship.getSourceEntity().getName());
}
// Register this combination (so we can remove it later if an insert occurs before commit)
FlattenedArcKey key = new FlattenedArcKey((ObjectId) nodeId, (ObjectId) targetNodeId, relationship);
// If this combination has already been deleted, simply undelete it.
if (!flattenedDeletes.remove(key)) {
flattenedInserts.add(key);
}
}
}
}
use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.
the class DataNode method performQueries.
/**
* Runs queries using Connection obtained from internal DataSource.
*
* @since 1.1
*/
@Override
public void performQueries(Collection<? extends Query> queries, OperationObserver callback) {
int listSize = queries.size();
if (listSize == 0) {
return;
}
if (callback.isIteratedResult() && listSize > 1) {
throw new CayenneRuntimeException("Iterated queries are not allowed in a batch. Batch size: %d", listSize);
}
// do this meaningless inexpensive operation to trigger AutoAdapter lazy
// initialization before opening a connection. Otherwise we may end up
// with two
// connections open simultaneously, possibly hitting connection pool
// upper limit.
getAdapter().getExtendedTypes();
Connection connection = null;
try {
connection = this.getDataSource().getConnection();
} catch (Exception globalEx) {
getJdbcEventLogger().logQueryError(globalEx);
Transaction transaction = BaseTransaction.getThreadTransaction();
if (transaction != null) {
transaction.setRollbackOnly();
}
callback.nextGlobalException(globalEx);
return;
}
try {
DataNodeQueryAction queryRunner = new DataNodeQueryAction(this, callback);
for (Query nextQuery : queries) {
// catch exceptions for each individual query
try {
queryRunner.runQuery(connection, nextQuery);
} catch (Exception queryEx) {
getJdbcEventLogger().logQueryError(queryEx);
// notify consumer of the exception,
// stop running further queries
callback.nextQueryException(nextQuery, queryEx);
Transaction transaction = BaseTransaction.getThreadTransaction();
if (transaction != null) {
transaction.setRollbackOnly();
}
break;
}
}
} finally {
try {
connection.close();
} catch (SQLException e) {
// ignore closing exceptions...
}
}
}
use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.
the class DataNodeSyncQualifierDescriptor method reset.
void reset(DbEntityClassDescriptor descriptor) {
attributes = new ArrayList<>(3);
valueTransformers = new ArrayList<>(3);
usingOptimisticLocking = descriptor.getEntity().getLockType() == ObjEntity.LOCK_TYPE_OPTIMISTIC;
// master PK columns
if (descriptor.isMaster()) {
for (final DbAttribute attribute : descriptor.getDbEntity().getPrimaryKeys()) {
attributes.add(attribute);
valueTransformers.add(input -> {
ObjectId id = (ObjectId) input.getNodeId();
return id.getIdSnapshot().get(attribute.getName());
});
}
} else {
// TODO: andrus 12/23/2007 - only one step relationship is supported...
if (descriptor.getPathFromMaster().size() != 1) {
throw new CayenneRuntimeException("Only single step dependent relationships are currently supported. Actual path length: %d", descriptor.getPathFromMaster().size());
}
DbRelationship masterDependentDbRel = descriptor.getPathFromMaster().get(0);
if (masterDependentDbRel != null) {
for (final DbJoin dbAttrPair : masterDependentDbRel.getJoins()) {
DbAttribute dbAttribute = dbAttrPair.getTarget();
if (!attributes.contains(dbAttribute)) {
attributes.add(dbAttribute);
valueTransformers.add(input -> {
ObjectId id = (ObjectId) input.getNodeId();
return id.getIdSnapshot().get(dbAttrPair.getSourceName());
});
}
}
}
}
if (usingOptimisticLocking) {
for (final ObjAttribute attribute : descriptor.getEntity().getAttributes()) {
if (attribute.isUsedForLocking()) {
// only care about first step in a flattened attribute
DbAttribute dbAttribute = (DbAttribute) attribute.getDbPathIterator().next();
// only use qualifier if dbEntities match
if (dbAttribute.getEntity().equals(descriptor.getDbEntity()) && !attributes.contains(dbAttribute)) {
attributes.add(dbAttribute);
valueTransformers.add(input -> input.getSnapshotValue(attribute.getName()));
}
}
}
for (final ObjRelationship relationship : descriptor.getEntity().getRelationships()) {
if (relationship.isUsedForLocking()) {
// only care about the first DbRelationship
DbRelationship dbRelationship = relationship.getDbRelationships().get(0);
for (final DbJoin dbAttrPair : dbRelationship.getJoins()) {
DbAttribute dbAttribute = dbAttrPair.getSource();
// relationship transformers override attribute transformers for meaningful FK's...
// why meaningful FKs can go out of sync is another story (CAY-595)
int index = attributes.indexOf(dbAttribute);
if (index >= 0 && !dbAttribute.isForeignKey()) {
continue;
}
Function<ObjectDiff, Object> transformer = input -> {
ObjectId targetId = input.getArcSnapshotValue(relationship.getName());
return targetId != null ? targetId.getIdSnapshot().get(dbAttrPair.getTargetName()) : null;
};
if (index < 0) {
attributes.add(dbAttribute);
valueTransformers.add(transformer);
} else {
valueTransformers.set(index, transformer);
}
}
}
}
}
}
use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.
the class DefaultDataRowStoreFactory method setUpEventBridge.
private void setUpEventBridge(DataRowStore store) {
if (isNoopEventBridge) {
return;
}
try {
store.setEventBridge(eventBridge);
store.startListeners();
} catch (Exception ex) {
throw new CayenneRuntimeException("Error initializing DataRowStore.", ex);
}
}
use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.
the class FlattenedArcKey method lazyJoinSnapshot.
private Map<String, Object> lazyJoinSnapshot() {
List<DbRelationship> relList = relationship.getDbRelationships();
if (relList.size() != 2) {
throw new CayenneRuntimeException("Only single-step flattened relationships are supported in this operation: %s", relationship);
}
DbRelationship firstDbRel = relList.get(0);
DbRelationship secondDbRel = relList.get(1);
List<DbJoin> fromSourceJoins = firstDbRel.getJoins();
List<DbJoin> toTargetJoins = secondDbRel.getJoins();
Map<String, Object> snapshot = new HashMap<>(fromSourceJoins.size() + toTargetJoins.size(), 1);
for (DbJoin join : fromSourceJoins) {
Object value = new PropagatedValueFactory(id1.getSourceId(), join.getSourceName());
snapshot.put(join.getTargetName(), value);
}
for (DbJoin join : toTargetJoins) {
Object value = new PropagatedValueFactory(id2.getSourceId(), join.getTargetName());
snapshot.put(join.getSourceName(), value);
}
return snapshot;
}
Aggregations