Search code examples
javasessionwebvaadinvaadin7

Vaadin 7 UI session gets closed prematurely


In my current project, that is based on Vaadin 7.6.2, I get an error that I simply don't find an explanaition for in my code. In this application, I open up a bunch of EntityManagers for a UI session and then close them when the session is detatched (when UI.detatch() is called). Now my problem is that my log clearly states that the session is detatched and then used again. AFAIK this should not happen, right?

Here is an excerp from the log file:

***  || 29.02.2016 09:59:32:504 | SESSION BEC3759B529AC42AA7FFE62314633979 | USER a******.i**** | de.darkblue.bagheera.db.EntityManagerBeanFactory.closeAll()
     || Closed 3 entity managers for session a******.i****

***  || 29.02.2016 09:59:32:504 | SESSION BEC3759B529AC42AA7FFE62314633979 | USER a******.i**** | de.darkblue.bagheera.BagheeraUI.detach()
     || Session de.darkblue.bagheera.BagheeraUI@1c5c5ab2 was detatched.

[...]
     
!!!! || 29.02.2016 10:00:21:159 | SESSION BEC3759B529AC42AA7FFE62314633979 | USER a******.i**** | com.vaadin.server.DefaultErrorHandler.doDefault()
     ||  caused by:
     || java.lang.IllegalStateException: Attempting to execute an operation on a closed EntityManager.
     ||         at org.eclipse.persistence.internal.jpa.EntityManagerImpl.verifyOpenWithSetRollbackOnly(EntityManagerImpl.java:1934)
     ||         at org.eclipse.persistence.internal.jpa.EntityManagerImpl.getCriteriaBuilder(EntityManagerImpl.java:2569)
     ||         at de.darkblue.bagheera.ui.tab.EditActivityReportTab.isOverlappingEntry(EditActivityReportTab.java:383)
     ||         [...]

You can see that the session BEC3759B529AC42AA7FFE62314633979 was closed at 09:59:32:504 but then reused nearly one minute later. I was under the impression that when UI.detatch() was called (as in log line 2), the UI instance was not used anymore, becuase the session expired (due to three mission heartbeats)? I get the feeling that I have the wrong idea about the lifecycle of a Vaadin UI session. It would be nice if someone could help me shed some light on this topic.


Solution

  • I'm not sure what session you are printing and referring to there.

    I think you know this but you have the HttpSession holds a VaadinSession object which hold multiple UI objects. If any of that sounds interesting, I think you'll find this helpful - how to put data in session variable and get the data in different page in vaadin?.

    But, my feeling is that you'll want to rethink your EntityManager lifecycle - see https://vaadin.com/docs/-/part/framework/jpacontainer/jpacontainer-hibernate.html

    Although, others do advocate an EM-per-session approach such as seen here - Save changes to database vaadin.

    This all said, the same VaadinSession can absolutely be used for multiple UIs, in fact you'll see a map called uIs in VaadinSession. Now, you do seem to get a new VaadinSession per browser window, but it isn't hard to instantiate multiple UI objects from the same window, and they will share the same VaadinSession.

    Finally it is worth noting that most normal setups will also have single HttpSession across all windows in a browser instance.