本文介绍了在Vaadin 7中调用VaadinSession getAttribute时需要锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道调用setAttribute(),但getAttirbute呢?

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

这是正确的吗?

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

还是需要锁定?

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


推荐答案

添加。虽然我不是这个主题的专家,但是在仔细阅读文档并阅读从通常的主要Vaadin用户界面线程中,不需要显式锁定。在主线程中工作时,Vaadin会根据需要自动锁定VaadinSession。

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.

您的所有应用状态都存储在该会话对象中,因此Vaadin会定期访问和保护该会话。

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

如果从后台线程从您启动的线程访问VaadinSession,则锁定只是一个问题。

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

即使在这种情况下,Vaadin提供了一对选项,如果您传递到这些访问方法之一:

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:


  • 方法> VaadinSession 对象

  • 方法: //vaadin.com/api/com/vaadin/ui/UI.htmlrel =nofollow noreferrer> UI 对象

  • access method on VaadinSession object
  • access method on UI object

如果您的代码仅影响VaadinSession而不触及任何 UI 对象(用户界面,布局) s,小部件组件等),然后使用第一个 VaadinSession.access()。另一方面,如果您的代码影响任何UI对象以及直接寻址VaadinSession,请使用第二个 UI.access()

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().

因此,当您可以在访问VaadinSession期间管理锁定时,您需要这样做只有在后台线程中由于某种原因你不想调用访问方法。但我无法想象任何这样的原因。

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

这篇关于在Vaadin 7中调用VaadinSession getAttribute时需要锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 00:41