I have an ObjectContext stored in the session. Now I have multiple ajax requests for the same session, all modifying the data of ObjectContext. How do I go about ensuring these requests will be thread safe?
The following documentation suggests that I use context nesting. Can someone give me a concrete example of how this works? Or even an explanation of how nesting context will allow thread-safe requests. Or even a link to some documentation of best practices in these cases. Thanks!
https://cayenne.apache.org/docs/3.1/cayenne-guide/persistent-objects-objectcontext.html
Nesting is useful to create isolated object editing areas (child contexts) that need to all be committed to an intermediate in-memory store (parent context), or rolled back without affecting changes already recorded in the parent. Think cascading GUI dialogs, or parallel AJAX requests coming to the same session.
Edit: I found the following paragraph on the documentation which helped me.
Contexts that are only used to fetch objects from the database and whose objects are never modified by the application can be shared between multiple users (and multiple threads). Contexts that store modified objects should be accessed only by a single user (e.g. a web application user might reuse a context instance between multiple web requests in the same HttpSession, thus carrying uncommitted changes to objects from request to request, until he decides to commit or rollback them). Even for a single user it might make sense to use mutliple ObjectContexts (e.g. request-scoped contexts to allow concurrent requests from the browser that change and commit objects independently)