How to use Apache Cayenne context in session with concurrent ajax requests? -


i have objectcontext stored in session. have multiple ajax requests same session, modifying data of objectcontext. how go ensuring these requests thread safe?

the following documentation suggests use context nesting. can give me concrete example of how works? or explanation of how nesting context allow thread-safe requests. or link documentation of best practices in these cases. thanks!

https://cayenne.apache.org/docs/3.1/cayenne-guide/persistent-objects-objectcontext.html

nesting useful create isolated object editing areas (child contexts) need committed intermediate in-memory store (parent context), or rolled without affecting changes recorded in parent. think cascading gui dialogs, or parallel ajax requests coming same session.

edit: found following paragraph on documentation helped me.

contexts used fetch objects database , objects never modified application can shared between multiple users (and multiple threads). contexts store modified objects should accessed single user (e.g. web application user might reuse context instance between multiple web requests in same httpsession, carrying uncommitted changes objects request request, until decides commit or rollback them). single user might make sense use mutliple objectcontexts (e.g. request-scoped contexts allow concurrent requests browser change , commit objects independently)

if don't keep uncommitted changes on server between requests, there simpler solution. not use session-scoped objectcontext. instead use per-request or per-method context. per-method works in typical case when changes introduced given request isolated within single method call. you'd create context on method entry, load objects (with query or transferring context via 'localobject'), perform changes, commit. after context discarded. e.g.:

public void myactionmethod() {      objectcontext peer = serverruntime.newcontext();      ... work objects in 'peer'      peer.commitchanges(); } 

now, if do keep uncommitted changes, can still use per-method contexts, nested. example above becomes this:

public void myactionmethod() {      objectcontext nested = serverruntime.newcontext(sessioncontext);      ... work objects in 'nested'      nested.commitchangestoparent(); } 

Popular posts from this blog