Issue
-
I have an SEO requirement to track some information for my website.
To that end I have inserted a groovy script in a workflow, which gets the user session, but it returns a null session ID. -
Steps to reproduce:
1. Create a workflow from the attached source in workflow_for_testing.txt.
2. Create a new form in Contents&Data and assign it to that worklflow.
3. Put it on a page.
4. Go to that page and submit the form once. Watch the logs.
Result: you see something like this loggedSession is NOT NULL!HttpSessionAdaptor[com.liferay.util.servlet.NullSession#nctk, equinox.http.dynamic-data-mapping-form-web]
5. Submit something in the form once again.
Result: you see now a "normal" session ID logged.Session is NOT NULL!HttpSessionAdaptor[D46098C43B9A32517696BB5B4D7D7733, equinox.http.dynamic-data-mapping-form-web]
All subsequent submits seems to be logging a real session ID. (or in some cases also null)
Environment
- 7.4
Resolution
-
When the groovy script is going to be executed here, a different thread is created and, in this new thread, the session returns
null - this is probably related to the fact that HttpSession is not thread safe, see https://stackoverflow.com/questions/10473254/accessing-httpsession-outside-of-the-originally-receiving-thread
"The HttpSession object itself can be used in multiple threads (but is not thread-safe and therefore must be synchronized)…. extract session attributes you need in @Async method and pass them directly."
-
Maybe if you could create a
DDMFormInstanceRecordmodel listener you could use thePortalSessionThreadLocal.getHttpSession()to extract these attributes:
public class DDMFormInstanceRecordModelListener
extends BaseModelListener<DDMFormInstanceRecord> {
@Override
public void onBeforeCreate(DDMFormInstanceRecord ddmFormInstanceRecord) throws ModelListenerException {
HttpSession httpSession = PortalSessionThreadLocal.getHttpSession();
}
}
Note: ThePortalSessionThreadLocal.getHttpSession()will returnnullin the groovy script as it is in a different thread.