Vaadin 7 to 8 migration, VaadinSession is not working

Hello,

we are migrating from Vaadin 7 to 8 and I’m getting NullPointerException When Using VaadinSeesion.getCurrent() from different threads. This works on our Vaadin 7 code.

we are running on Tomcat 8.5, Java 8, VaadinVersion 8.6.1 and Using VaadinPush(value = PushMode.AUTOMATIC, transport = Transport.WEBSOCKET).

Also I see a WARNING message : WARNING: More than one Servlet Mapping defined. WebSocket may not work org.apache.catalina.core.ApplicationServletRegistration@2ef8d07c

Any help or suggestion are welcome!

Getting the current UI or session from a background thread is not guaranteed to work correctly even in Vaadin 7 - if it happens to return the instance you want, it’s by accident. It was changed to return null explicitly in Vaadin 8 to discourage this kind of problematic usage. You should store a pointer to an instance instead.

Hey Olli,

Thank you for you reply.

I see the point of passing variables within UI for accessing VaadinSession. However, there is explanation on Book of Vaain Pocket, “You can obtain the VaadinSession of a UI with getSession() or globally with VaadinSession.getCurrent().”

I assume VaadinSession.getcurrent() is a static method that could using globally to get VaadinSession. Or Could you provide any resource of how to using VaadinSeesion.getcurrent() correctly?

Thank you for you help!

And this is what is found in Data-Centric Applications with Vaadin 8 page 53,
public static boolean authenticate( String username, String password){

if(authentic) {
VaadinSession.getcurrent().setAttribute(USERNAME_ATTRIBUTE, username);
}

}

public static boolean is Authenticated(){
return VaadinSession.getCurrent().getAttribute(USERNAME_ATTRIBUTE) != null;
}

I’m using VaadinSession just like this.

I was slightly off the mark earlier. From the JavaDoc of VaadinSession.getCurrent():

/**
 * Gets the currently used session. The current session is automatically
 * defined when processing requests related to the session (see
 * {@link ThreadLocal}) and in {@link VaadinSession#access(Runnable)} and
 * {@link UI#access(Runnable)}. In other cases, (e.g. from background
 * threads, the current session is not automatically defined.
 * <p>
 * The session is stored using a weak reference to avoid leaking memory in
 * case it is not explicitly cleared.
 *
 * @return the current session instance if available, otherwise
 *         <code>null</code>
 *
 * @see #setCurrent(VaadinSession)
 *
 * @since 7.0
 */
public static VaadinSession getCurrent() {
// ...

So you need to use UI.access() to be able to get the current VaadinSession. But what I said about the current UI remains true - it can’t be retrieved with UI.getCurrent() in a background thread - so in order to get the session, you need to run UI.access from an instance you know is attached to the correct session. One option to retrieve the current UI would be to store it into a member field where it’s visible to the thread. Something like private UI ui; and later, before starting the thread, ui = UI.getCurrent();. Another option would be calling someComponent.getUI().access(...) from a component you have that is attached to the UI.