Changing application locale makes changes to all u

2019-07-07 19:25发布

问题:

In my application I set up the locale when the user get into the system to localize the components. Recently I was giving a demo and noticed that the language was switching from English to Dutch without apparent reason. It turned out that the demo server (hosted in a German server) was being accessed at the same time by another person that set it to Dutch and was affecting my English demo and viceversa.

I gave it a try on my machine with different browsers, using Chrome normal and incognito modes, etc to simulate different sessions and the issue is present. Any ideas on how to handle/fix this? I thought Vaadin already handled user sessions on its own but it seems that the application's variables are shared?

Here's what I do in my application to change the locale:

@Override
    public void setLocale(Locale locale) {
        super.setLocale(locale);
        xerb = ResourceBundle.getBundle("com.bluecubs.xinco.messages.XincoMessages", getLocale());
}

Then the xerb resource bundle is used to internationalize the UI. I tried not doing the super call but the result is the same, as if xerb was being modified by various sessions and shared among them.

Any ideas?

Same question on Vaadin's forum: https://vaadin.com/forum/-/message_boards/view_message/1091312

Edit

Using ThreadLocal pattern I added outputs to when each instance is obtained and I see different instances for each browser. (i.e. com.bluecubs.xinco.core.server.vaadin.Xinco@2114ed for the first one and com.bluecubs.xinco.core.server.vaadin.Xinco@fd68fe for the second browser) so I believe the model is used correctly. Sadly I still see the same issue.

回答1:

Try to apply ThreadLocal pattern to access the Application instance from other controls in the app. This way, it's more explicit whenever you try to obtain some session-specific information. It's also quite widespread in Vaadin applications, so the code may be more readable.

Passing the reference to the fields of your Application indirectly by making it an outer class for anonymous listeners can lead to hard to detect bugs, because it can be difficult to work out the exact moment of creating the controls and what ResourceBundle was referenced at that very point.



回答2:

Vaadin does handle user sessions and keeps variables separate, so this is not normal behavior. The 'xerb' field must be somehow shared between users. The only a few reasons for this that I can think of right now are

  1. xerb is a static field
  2. the ResourceBundle pointed to by xerb is the same (static) one
  3. The actual Application instance is shared between users

You already said #1 is not the cause. #2 might be it, but I don't know if that would be possible. #3 would be possible if you have a custom ApplicationServlet containing a bug that causes it to return the same instance of Application for all users. Or perhaps there is some injection mechanism injecting the same thing in all Applications?

Without more information it's impossible to know what's actually going on.

HTH