Need to block when calling VaadinSession getAttribute in Vaadin 7

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

Is it correct?

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

Or do you need a lock?

 public Object getMyAttribute() { try { VaadinSession.getCurrent().getLockInstance().lock(); return VaadinSession.getCurrent().getAttribute("myAttribute"); } finally { VaadinSession.getCurrent().getLockInstance().unlock(); } } 
+3
source share
2 answers

Add to Patton's answer . Although I am not an expert on this topic, I post my understanding after carefully reading the document and read this post from Roland Krueger.

Result: controversial issue

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

Let Vaadin 7.1 and later will automatically commit . Doc says an automatic blocking route is preferable to manual blocking.

Non-issue on main thread

If you use VaadinSession from the normal Vaadin main UI thread, then explicit locking is not required. Vaadin automatically blocks VaadinSession as needed when working in the main thread.

All your application state is stored in this session object, so Vaadin regularly accesses and protects this session.

Other topics

Locking is only a problem when accessing VaadinSession from the background thread from the thread you started.

Even so, Vaadin provides a couple of options when the lock is processed automatically if you pass Runnable to any of these access methods:

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

Manual lock without function

Therefore, when you can control the lock during access to VaadinSession, you only need to do this in the background thread and for some reason you do not want to call the access method. But I can not imagine such a reason.


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

+5
source

If you are trying to access a Vaadin session from a different background thread, you need to access the lock, otherwise you do not need it. Vaadin will automatically do this for you, i.e. When you perform some operation in the user interface before the vaadin framework calls your methods, the framework will block the session.

Then, if you are trying to access session variables from another thread, you need to do something 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

+2
source

All Articles