Java – when you call VaadinSession getAttribute in Vaadin 7, you need to lock it up.

I know it's necessary to call setAttribute (link), but what about getattirbut?

Is it correct?

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

Or does it need to be locked?

public Object getMyAttribute() {
    try {
        VaadinSession.getCurrent().getLockInstance().lock();
        return VaadinSession.getCurrent().getAttribute("myAttribute");
    } finally {
        VaadinSession.getCurrent().getLockInstance().unlock();
    }
}

Solution

Add to the answer by Patton Although I am not an expert on this subject, after carefully reading the document, I published my understanding and read Roland Kr ü ger's this post

Upshot: simulation problem

Although I don't know the exact answer to your question, I don't think it makes sense

➡ Let vaadin 7.1 and later automatically handle locking The document says that automatic locking path is better than manual locking

There is no problem on the main thread

If vaadinsession is accessed from the usual primary vaadin user interface thread, no explicit locking is required When working in the main thread, vaadin automatically locks vaadinsession as needed

All your application state is stored in the session object, so vaadin will access and protect the session regularly

Other topics

Locking is only a problem if vaadinsession is accessed from the background thread from the thread you started

Even in this case, vaadin provides a pair of options. If you pass runnable to any of these "access" methods, locking will be handled automatically:

>Access method vaadinsession Object > access method UI object

If the code only affects vaadinsession without touching any UI objects (user interface, layout, widget components, etc.), the first vaadinsession is used access(). On the other hand, if your code affects any UI object and directly addresses vaadinsession, use the second UI access().

Manual locking is not required

Therefore, although you can manage locking during access to vaadinsession, you only need to do so in the background thread and for some reason you don't want to call any access method But I can't imagine any such reason

For more discussion and my groovy diagram, please refer to this similar question, how to put data in session variable and get the data in different page in vaadin

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>