Вопрос

I know that it is necessary when call setAttribute (link), but what about getAttirbute?

Is this correct?

public Object getMyAttribute() {
    return VaadinSession.getCurrent().getAttribute("myAttribute");
}

Or need locking?

public Object getMyAttribute() {
    try {
        VaadinSession.getCurrent().getLockInstance().lock();
        return VaadinSession.getCurrent().getAttribute("myAttribute");
    } finally {
        VaadinSession.getCurrent().getLockInstance().unlock();
    }
}
Это было полезно?

Решение

Adding to the answer by Patton. While I'm not an expert on this topic, I am posting my understanding after a thorough reading of the doc, and reading this post by Roland Krüger.

Upshot: Moot Question

While I do not know the exact answer to your question, I believe the question is moot.

Let Vaadin 7.1 and later handle the locking for you automatically. The doc says the automatic locking route is preferred over manual locking.

Non-Issue On Main Thread

If accessing the VaadinSession from within the usual main Vaadin user interface thread, then no explicit locking is needed. Vaadin automatically locks the VaadinSession as needed when working in the main thread.

All of your app's state is stored in that session object, so Vaadin is accessing and protecting that session routinely.

Other Threads

Locking is only an issue if accessing the VaadinSession from a background thread, from a thread you started.

Even in this case, Vaadin provides a pair of options where locking is handled automatically if you pass a Runnable to either of these "access" methods:

If you code affects only the VaadinSession without touching any UI object (user interface, layouts, widget components, and such), then use the first, VaadinSession.access(). On the other hand, if your code affects any UI objects as well as directly addressing the VaadinSession, use the second, UI.access().

Manual Locking Unneeded

So while you can manage the locking during access to the VaadinSession, you need do so only when in a background thread and for some reason you do not want to call either access method. But I cannot imagine any such reason.


For more discussion and a groovy diagram I made, see this similar question, how to put data in session variable and get the data in different page in vaadin?.

Другие советы

If you trying to access Vaadin Session from another background thread, then you need to access to lock, else you don't actually have to. Vaadin Service will automatically do that for you i.e., when ever you do a some operation on the UI before vaadin framework calls your methods the framework will have a lock on the session.

Next, if you trying to access Session variables from another thread, then you have to do some thing like this to access session Variables.

          UI.getCurrent().access(new Runnable() {

                @Override
                public void run() {

                    Thread thread = new Thread(new Runnable(){
                      //TODO Write your logic to perform some session related action

                     });
                    thread.start();

                }
            });

Hope this helps you

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top