Reputation: 3974
In a managed bean you have fields, and the fields have getters and setters.
But I also need to save values back to, in this case, a Notes profile document.
So I have a loadProfileDocument
and a saveProfileDocument
method.
I was thinking of using the bean in the application scope.
How do I make sure the profile document is saved?
Do I have to call the saveProfileDocument
from the setter?
Do I call the saveProfileDocument()
explisitly?
Could I use a destructor (finalize)?
Or what...???...
Upvotes: 2
Views: 407
Reputation: 8086
You may want to consider using the so-called "MIMEBean" approach, which serializes the state of a bean to a MIME item on a document. With this approach, your bean fields can be hierarchical (objects within objects within objects), as long as each object is also serializable. This provides far more flexibility than the usual flat data structure where one field = one primitive value (or vector of values).
For an example of this approach used rather extensively in an application, examine the source of the WatrCoolr project on OpenNTF.
Jesse Gallagher also created a custom data source that allows this technique to be used rather transparently.
Upvotes: 2
Reputation: 3524
Do not use profile document. As @Sven Hasselbach mentioned, every thread of HTTP task have its own cache copy of profile document (profiles are cached by design) and save in one thread is not populated to other threads. Usually restart of HTTP task is required to fix the mess.
To answer your question you have to decide what is the frequency of updates. If you update single property from time to time, save invoked in setter is good choice. For updates of many attributes/fields in batch the same approach would perform badly.
Good tradeoff would be to set dirty flag in setters and call save method in afterRenderResponse to conditionally save bean state to backend document. For request scoped bean there is no need to clear dirty flag, for other scopes don't forget to reset it after save.
Upvotes: 1