Vaadin 8 Hibernate session/transaction per request

We have a Vaadin 8 widget application running on Liferay DXP 7.2.

What are the correct extension points to open/close a new Hibernate session and transaction for each Vaadin request (page reload, event handlers)?
We’re not using Spring due to reasons so this has to be done manually.

VaadinPortlet.handleRequest() seems like the best candidate but there seems to be no way to hook into that.

Our widgets are subclasses of UI, and through some black magic this class is automatically registered as both com.vaadin.ui.UI and javax.portlet.Portlet services in Liferay’s OSGi container. I cannot find any way to tell the UI class to use a different portlet class.

What are the correct extension points to open/close a new Hibernate session and transaction for each Vaadin request

I am just wondering why on earth you need to do that in the first place. It sounds a bit wrong to me. In the apps where I have used Hibernate there has been no need for such thing.

However if you do really need to do something for each request, I suggest to add request handler. You need custom portlet in similar fashion I have custom servlet here, instead of overriding serlvetInitialized, the method you need to override is portletInitialized.

https://github.com/TatuLund/cdi-demo/blob/master/src/main/java/org/vaadin/cdidemo/MyVaadinUI.java

@Override
protected void portletInitialized() throws PortletException {
	getService().addSessionInitListener(event -> {
		event.getSession().addRequestHandler(...);
	});

}

Because we don’t have any framework available (due to some legacy and time reasons) that would handle it for us. Every request from different users ends up on the same few HTTP threads and even with ThreadLocal it blows up eventually since a thread is shared by multiple VaadinSessions.

However, I cannot find anywhere on the Internet an example on how to register a custom VaadinPortlet… we have a couple of these and there is no obvious way to do it:

@Widgetset(CustomWidgetSet.NAME)
@Theme(CustomVaadinTheme.NAME)
@Component(service = UI.class, property = {
		"com.liferay.portlet.display-category=Custom",
		"javax.portlet.name=CustomLandingPageApplication",
		"javax.portlet.display-name=Custom Landing page Application",
		"javax.portlet.mime-type=text/html",
		"javax.portlet.portlet-mode=text/html;view,edit",
		"javax.portlet.security-role-ref=user",
		"javax.portlet.preferences=<preference><name>settings</name></preference>",
		"com.liferay.portlet.instanceable=false",
		"com.liferay.portlet.ajaxable=false",
		"com.liferay.portlet.preferences-unique-per-layout=true",
		"com.liferay.portlet.preferences-owned-by-group=true",
		"com.vaadin.osgi.liferay.portlet-ui=true"},
		scope = ServiceScope.PROTOTYPE)
public class CustomLandingPageApp extends UI {

It is long time I have been working on this, but I would try something like

import javax.servlet.annotation.WebServlet;
import com.vaadin.annotations.VaadinServletConfiguration;

@WebServlet(urlPatterns = "/*", name = "MyPortalServlet", asyncSupported = true)
public class MyPortalServlet extends MyPortlet {
}

And

import javax.portlet.PortletException;
import com.vaadin.server.VaadinPortlet;

public class MyPortlet extends VaadinPortlet {

    @Override
    protected void portletInitialized() throws PortletException {
	    getService().addSessionInitListener(event -> {
		    event.getSession().addRequestHandler(...);
	    });

    }
}