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