Spring 4.1 MVC + Hibernate 4.3 + Webflow 2.4 + OpenSessionInViewFilter = java.lang.IllegalStateException, Already value for key bound to thread

[Note: I originally posted this problem/solution to Stackoverflow.com]

Problem:

I am upgrading an application from these component versions to their latest counterparts:

I am currently very stuck on a problem related the OpenSessionInViewFilter and Spring Webflows. None of the code pertaining to my webflow is even executed, the problem occurs upon initialization of the webflow and Hibernate SessionHolder. I have not changed the webflow configuration during this Spring/Hibernate upgrade and everything has been working fine in production for almost 6 years. I am getting the following exception for which there is little help available on the web. The stack trace is a mile long, so I am including what I think are the important parts.

Pertinent parts of web.xml:

Pertinent parts of Application Context configuration:

Webflow configuration file:

It seems that the OpenSessionInViewFilter is creating the Hibernate Session first, then the HibernateFlowExecutionListener is attempting to create one rather than using the one created by the OpenSessionInViewFilter thus causing the "Already value [org.springframework.orm.hibernate4.SessionHolder@xxxxxxxx] for key [org.hibernate.internal.SessionFactoryImpl@xxxxxxxx] bound to thread [xxxxxxxxxxxxxxx]"error.

Any ideas on what I can tweak or further delve into for troubleshooting? Any solutions or workarounds? Anyone else seen this? Thank you for the help!

Solution

My solution to this was to split out my Web Flows from being handled by the OpenSessionInViewFilter. Before I made the change, all URLs in the app that ended in *.htm were routed through the OpenSessionInViewFilter. This was my web.xml before the changes:

Now my web.xml has this configuration:

Notice that I added a new *.flow url-pattern to my filter-mapping for the openSessionInViewFilter and I now have a custom class that overrides the Spring OpenSessionInViewFilter (more on that below). I then had to change the URL suffix of the places in my views (and controllers in a few cases) where I invoked webflows from using ‘.htm’ to using ‘.flow’.
So, for example, if I had previously used webflow url of ‘/customer/customer-add.htm’, I changed it to ‘/customer/customer-add.flow’.

The next piece is that I added a class derived from Spring’s OpenSessionInViewFilter whose purpose was to pass all *.htm URLs to the OpenSessionInViewFilter while simply forwarding all *.flow URLs to the next filter in the chain (thereby allowing the HibernateFlowExecutionListener to eventually handle the URL and manage the session properly):

Finally, please note that you should leave the Spring configuration you already have for the HibernateFlowExecutionListener alone — no changes required there:

 

This entry was posted in Java and tagged , , , , . Bookmark the permalink.

Leave a Reply