Search in sources :

Example 61 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class CayenneTransaction method processCommit.

@Override
protected void processCommit() {
    status = BaseTransaction.STATUS_COMMITTING;
    if (connections == null || connections.isEmpty()) {
        return;
    }
    Throwable deferredException = null;
    for (Connection connection : connections.values()) {
        try {
            if (deferredException == null) {
                connection.commit();
            } else {
                // we must do a partial rollback if only to cleanup uncommitted connections.
                connection.rollback();
            }
        } catch (Throwable th) {
            // there is no such thing as "partial" rollback in real
            // transactions, so we can't set any meaningful status.
            // status = ?;
            setRollbackOnly();
            // stores last exception
            // TODO: chain exceptions...
            deferredException = th;
        }
    }
    if (deferredException != null) {
        logger.logRollbackTransaction("transaction rolledback.");
        throw new CayenneRuntimeException(deferredException);
    } else {
        logger.logCommitTransaction("transaction committed.");
    }
}
Also used : Connection(java.sql.Connection) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 62 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class DefaultTransactionManager method performInLocalTransaction.

protected <T> T performInLocalTransaction(TransactionalOperation<T> op, TransactionListener callback) {
    Transaction tx = txFactory.createTransaction();
    BaseTransaction.bindThreadTransaction(tx);
    try {
        T result = performInTransaction(tx, op, callback);
        tx.commit();
        return result;
    } catch (CayenneRuntimeException ex) {
        tx.setRollbackOnly();
        throw ex;
    } catch (Exception ex) {
        tx.setRollbackOnly();
        throw new CayenneRuntimeException(ex);
    } finally {
        BaseTransaction.bindThreadTransaction(null);
        if (tx.isRollbackOnly()) {
            try {
                tx.rollback();
            } catch (Exception e) {
                // although we don't expect an exception here, print the
                // stack, as there have been some Cayenne bugs already
                // (CAY-557) that were masked by this 'catch' clause.
                jdbcEventLogger.logQueryError(e);
            }
        }
    }
}
Also used : CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 63 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class TransactionFilter method onSync.

@Override
public GraphDiff onSync(final ObjectContext originatingContext, final GraphDiff changes, final int syncType, final DataChannelFilterChain filterChain) {
    DataChannelSyncCallbackAction callbackAction = DataChannelSyncCallbackAction.getCallbackAction(originatingContext.getChannel().getEntityResolver().getCallbackRegistry(), originatingContext.getGraphManager(), changes, syncType);
    callbackAction.applyPreCommit();
    GraphDiff result;
    switch(syncType) {
        case DataChannel.ROLLBACK_CASCADE_SYNC:
            result = filterChain.onSync(originatingContext, changes, syncType);
            ;
            break;
        // including transaction handling logic
        case DataChannel.FLUSH_NOCASCADE_SYNC:
        case DataChannel.FLUSH_CASCADE_SYNC:
            result = transactionManager.performInTransaction(new TransactionalOperation<GraphDiff>() {

                @Override
                public GraphDiff perform() {
                    return filterChain.onSync(originatingContext, changes, syncType);
                }
            });
            break;
        default:
            throw new CayenneRuntimeException("Invalid synchronization type: %d", syncType);
    }
    callbackAction.applyPostCommit();
    return result;
}
Also used : GraphDiff(org.apache.cayenne.graph.GraphDiff) DataChannelSyncCallbackAction(org.apache.cayenne.DataChannelSyncCallbackAction) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 64 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class MemoryClob method truncate.

/**
 * Truncates the <code>CLOB</code> value that this <code>Clob</code> designates to
 * have a length of <code>len</code> characters.
 * <p>
 */
public void truncate(final long len) throws SQLException {
    final String ldata = data;
    final long dlen = ldata.length();
    final long chars = len >> 1;
    if (chars == dlen) {
    // nothing has changed, so there's nothing to be done
    } else if (len < 0 || chars > dlen) {
        throw new CayenneRuntimeException("Invalid length: %d", len);
    } else {
        // use new String() to ensure we get rid of slack
        data = ldata.substring(0, (int) chars);
    }
}
Also used : CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 65 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class ObjectDetachOperation method detach.

/**
 * "Detaches" an object from its context by creating an unattached copy. The copy is
 * created using target descriptor of this operation that may be different from the
 * object descriptor passed to this method.
 */
public Object detach(Object object, ClassDescriptor descriptor, final PrefetchTreeNode prefetchTree) {
    if (!(object instanceof Persistent)) {
        throw new CayenneRuntimeException("Expected Persistent, got: %s", object);
    }
    final Persistent source = (Persistent) object;
    ObjectId id = source.getObjectId();
    // sanity check
    if (id == null) {
        throw new CayenneRuntimeException("Server returned an object without an id: %s", source);
    }
    Object seenTarget = seen.get(id);
    if (seenTarget != null) {
        return seenTarget;
    }
    descriptor = descriptor.getSubclassDescriptor(source.getClass());
    // presumably id's entity name should be of the right subclass.
    final ClassDescriptor targetDescriptor = targetResolver.getClassDescriptor(id.getEntityName());
    final Persistent target = (Persistent) targetDescriptor.createObject();
    target.setObjectId(id);
    seen.put(id, target);
    descriptor.visitProperties(new PropertyVisitor() {

        private void fillReverseRelationship(Object destinationTarget, ArcProperty property) {
            ArcProperty clientProperty = (ArcProperty) targetDescriptor.getProperty(property.getName());
            if (clientProperty != null) {
                ArcProperty clientReverse = clientProperty.getComplimentaryReverseArc();
                if (clientReverse instanceof ToOneProperty) {
                    clientReverse.writeProperty(destinationTarget, null, target);
                }
            }
        }

        public boolean visitToOne(ToOneProperty property) {
            if (prefetchTree != null) {
                PrefetchTreeNode child = prefetchTree.getNode(property.getName());
                if (child != null) {
                    Object destinationSource = property.readProperty(source);
                    Object destinationTarget = destinationSource != null ? detach(destinationSource, property.getTargetDescriptor(), child) : null;
                    if (destinationTarget != null) {
                        fillReverseRelationship(destinationTarget, property);
                    }
                    ToOneProperty targetProperty = (ToOneProperty) targetDescriptor.getProperty(property.getName());
                    Object oldTarget = targetProperty.isFault(target) ? null : targetProperty.readProperty(target);
                    targetProperty.writeProperty(target, oldTarget, destinationTarget);
                }
            }
            return true;
        }

        public boolean visitToMany(ToManyProperty property) {
            if (prefetchTree != null) {
                PrefetchTreeNode child = prefetchTree.getNode(property.getName());
                if (child != null) {
                    Object value = property.readProperty(source);
                    Object targetValue;
                    if (property instanceof ToManyMapProperty) {
                        Map<?, ?> map = (Map) value;
                        Map targetMap = new HashMap();
                        for (Entry entry : map.entrySet()) {
                            Object destinationSource = entry.getValue();
                            Object destinationTarget = destinationSource != null ? detach(destinationSource, property.getTargetDescriptor(), child) : null;
                            if (destinationTarget != null) {
                                fillReverseRelationship(destinationTarget, property);
                            }
                            targetMap.put(entry.getKey(), destinationTarget);
                        }
                        targetValue = targetMap;
                    } else {
                        Collection collection = (Collection) value;
                        Collection targetCollection = new ArrayList(collection.size());
                        for (Object destinationSource : collection) {
                            Object destinationTarget = destinationSource != null ? detach(destinationSource, property.getTargetDescriptor(), child) : null;
                            if (destinationTarget != null) {
                                fillReverseRelationship(destinationTarget, property);
                            }
                            targetCollection.add(destinationTarget);
                        }
                        targetValue = targetCollection;
                    }
                    ToManyProperty targetProperty = (ToManyProperty) targetDescriptor.getProperty(property.getName());
                    targetProperty.writeProperty(target, null, targetValue);
                }
            }
            return true;
        }

        public boolean visitAttribute(AttributeProperty property) {
            PropertyDescriptor targetProperty = targetDescriptor.getProperty(property.getName());
            targetProperty.writeProperty(target, null, property.readProperty(source));
            return true;
        }
    });
    return target;
}
Also used : ArcProperty(org.apache.cayenne.reflect.ArcProperty) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) PropertyDescriptor(org.apache.cayenne.reflect.PropertyDescriptor) ObjectId(org.apache.cayenne.ObjectId) HashMap(java.util.HashMap) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) ArrayList(java.util.ArrayList) ToManyMapProperty(org.apache.cayenne.reflect.ToManyMapProperty) Persistent(org.apache.cayenne.Persistent) AttributeProperty(org.apache.cayenne.reflect.AttributeProperty) ToOneProperty(org.apache.cayenne.reflect.ToOneProperty) Entry(java.util.Map.Entry) ToManyProperty(org.apache.cayenne.reflect.ToManyProperty) PrefetchTreeNode(org.apache.cayenne.query.PrefetchTreeNode) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map) PropertyVisitor(org.apache.cayenne.reflect.PropertyVisitor)

Aggregations

CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)168 Test (org.junit.Test)25 DbAttribute (org.apache.cayenne.map.DbAttribute)21 DataMap (org.apache.cayenne.map.DataMap)19 ObjectId (org.apache.cayenne.ObjectId)18 ObjEntity (org.apache.cayenne.map.ObjEntity)18 Persistent (org.apache.cayenne.Persistent)17 Expression (org.apache.cayenne.exp.Expression)17 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)17 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)14 DbEntity (org.apache.cayenne.map.DbEntity)14 IOException (java.io.IOException)13 List (java.util.List)12 ObjRelationship (org.apache.cayenne.map.ObjRelationship)12 DbRelationship (org.apache.cayenne.map.DbRelationship)10 DateTestEntity (org.apache.cayenne.testdo.date_time.DateTestEntity)10 File (java.io.File)9 Connection (java.sql.Connection)9 SQLException (java.sql.SQLException)9