Java – portlets, httpsession and thread safety
Our portlet remains in state in httpsession, which is shared by all request processing threads in the same session
The portlet specification (jsr-168) states:
I wonder how I should achieve this goal? Of course, I can use synchronization to achieve mutual exclusion during processaction and render, but I don't know how to enforce the atomicity of the whole request processing In particular, I am concerned about the following:
>Thread 1 executes processaction, loads the data into the session for later rendering > thread 2 executes processaction, discards the data from the session > thread 1 executes rendering, reads the data to be rendered from the session, and throws a NullPointerException because the prepared data is no longer stored in
How do you usually stop this? In particular, when using JBoss portlet bridge to adjust JSF to portlet environment?
Solution
I would say that if two portlets run on the same data, especially if one reads it and the other deletes it, there is likely to be a serious defect in the design
Then, you may want to store data by portlet / thread, that is, if portlet1 reads some data, it should be locked until the reading is complete and put it into the session with a unique key
If it is legal to delete the data that should be rendered, you should take this into account and check again during rendering