cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Gentry <>
Subject Re: Committing changes for specified individual objects
Date Sat, 21 May 2016 01:58:05 GMT
Hi Brian,

"is there any way to specify which individual objects you want to commit"

Not really.  Cayenne works at the object graph level and an ObjectContext
(or DataContext) is what is used to manage your graph changes and then
commit them in a single database transaction.

That being said, there is nothing stopping you from creating many more
ObjectContexts and it is cheap to do so.  In your example, you would:

1) create a brand new ObjectContext totally separate of the thread
2) create your exception log object in that new ObjectContext,
3) commit the new ObjectContext (which only has your exception log object
in it).

Since you mentioned you are using 3.0.2, this is easily done with
DataContext.createDataContext().  Cayenne 3.1 requires you to get it from
the ServerRuntime.

I'd be wary of creating children of your thread's ObjectContext for this
purpose as committing child contexts will also commit the parent, if I'm
remembering correctly.

If your exception log object needs a relationship to an object in the
thread context, you'll either have to pull it in using DC's localObject()
method or perhaps DataObjectUtils.objectForPK(), assuming that object is
already persisted.  I tend to use the latter approach, typically with the
original object's getObjectId() method.


On Thu, May 19, 2016 at 12:22 PM, Brian Baillargeon <> wrote:

> Hey folks,
> Generally, I retrieve objects using
> BaseContext.getThreadObjectContext().performQuery(query);
> and after creating / updating / deleting objects, I commit changes with
> BaseContext.getThreadObjectContext().commitChanges();
> This commits all changes on the current thread's ObjectContext, but is
> there any way to specify which individual objects you want to commit
> changes for? I'm just looking for a best practice to safeguard my
> objects, but I think this would be a valid use case:
> You have a table that logs exceptions, and you encounter an exception
> while modifying an object backed by another table. So you want to
> persist the exception's log, but not the object you were modifying.
> I've thought about separating my objects onto different ObjectContexts
> (Ie. children of getThreadObjectContext()), but this doesn't seem like a
> good solution, especially if you have relationships (since related
> objects have to be in the same ObjectContext).
> Any suggestions? I'm using Cayenne 3.0.2, but I'm all ears for 3.1
> suggestions too.
> Thanks,
> Brian Baillargeon

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message