Question

I have the following code:

List<Assignment> assignments = objectContext.performQuery(assignmentQuery);
objectContext.commitChanges();
objectContext.deleteObjects(assignments);
objectContext.commitChanges();

I do the first commitChanges() to commit all the queries. Then I clear Cayenne's log. On the second commitChanges(), this shows up on the log:

INFO : QueryLogger.logBeginTransaction: 2013-07-10 07:37:11,214: --- transaction started. INFO : QueryLogger.logQuery: 2013-07-10 07:37:11,218: INSERT INTO scheduler_assignment ... INFO : QueryLogger.logQuery: 2013-07-10 07:37:11,241: DELETE FROM scheduler_assignment ... INFO : QueryLogger.logCommitTransaction: 2013-07-10 07:37:11,286: +++ transaction committed.

I don't understand why it is doing the INSERT statement when I'm trying to delete. Can anyone explain? Thanks!

Était-ce utile?

La solution

The only logical explanation is that your ObjectContext is "dirty" - it contains other uncommitted objects in addition to what is shown here. This can happen for a variety of reasons, 2 most common being:

(1) ObjectContext scope is too wide and changes to the context are originating from some other place in the app.

(2) changes originating from callbacks/listeners during commit.

Some hints on scoping of ObjectContexts:

  • Do not share ObjectContexts in a concurrent environment if the contexts are expected to handle writes (as opposed to just reads; read-only contexts can be shared).
  • Ideal scope for an ObjectContext that handles writes is a single method or a single request. This guarantees that nobody else would access it concurrently.
  • Often a context would have a longer scope though. E.g. it may be saved in a session in a webapp, and may carry uncommitted changes between requests. In this situation still consider reducing its scope. E.g. create multiple such contexts in a session, each attached to a given page. So that uncommitted changes in one place do not surprise you when you commit something else in another place.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top