5

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();
    }
}
Krayo
  • 2,492
  • 4
  • 27
  • 45

2 Answers2

7

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?.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Hm, you're right, it was not clear to me. Vaadin handles the locking in the main thread, so **there is** locking when I call setAttribute or getAttribute (visibility guarantee). – Krayo Sep 01 '14 at 09:52
  • 1
    I think you're absolutely correct, Basil. There's nothing more to add to your answer. Concurrent access to VaadinSession or the UI object is preferredly done through the `access()` methods. When accessing these objects from the main thread no locking has to be done. When I wrote the [blog post](https://blog.oio.de/2013/02/22/vaadins-variable-scopes-vaadinsession-and-ui) mentioned above, Vaadin 7.1 hasn't been released yet and explicit session locking had to be done as described in the article. I will update the blog post accordingly. – Roland Krüger Sep 02 '14 at 16:54
  • 1
    @RolandKrüger Thanks for your comment verifying my thoughts. That's a relief that I got it right. Threading is tricky stuff! I'm glad to see Vaadin making it easier. – Basil Bourque Sep 02 '14 at 17:18
  • if you run your jvm with the "-ea" param, you'll get exceptions from vaadin, if you access sth without (needed) a lock. – riddy Oct 02 '14 at 07:04
3

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

Patton
  • 2,002
  • 3
  • 25
  • 36