Vaadin EntityManager-Per-Request pattern using Server Push
Posted by BeCodeMonkey on August 18, 2015
Hibernate is one reason you mite need to do the following since Hibernate does not like keeping entity manager alive for long.
According the the Book of Vaadin section 19.9 they recommend using a Servlet filter to close the entity manager is used. This is all good until you start using Server Push since this will use Websockets and bypass the Servlet filter. What I found is Vaadin uses Atmosphere framework for the Websocket work. So you will need a Interceptor to do the same thing a Servlet filter does for servlet requests.
My version of LazyHibernateEntityManagerProvider is a bit different from the one in the Vaadin documentation in that I only create a entity manager on the first use since not every call requires a entity manager.
Now to add your Interceptor to Atmosphere just add the following initParams to your servelet
This can also be done with the web.xml optionally.
Just remember you will still need the Servlet Filter to close the Entity Manager also since not all request will go via websockets.