在Vaadin 7中调用VaadinSession getAttribute时需要锁定 [英] Need locking when call VaadinSession getAttribute in Vaadin 7

查看:143
本文介绍了在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();
    }
}


推荐答案

添加巴顿的答案。虽然我不是这个主题的专家,但是在仔细阅读文档并阅读这篇文章由RolandKrüger撰写。

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.

虽然我不知道你问题的确切答案,但我相信这个问题没有实际意义。

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

让Vaadin 7.1及更高版本自动处理锁定。 doc说自动锁定路径比手动锁定更受欢迎。

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

如果访问 VaadinSession 从通常的主要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提供了一对选项,如果您传递 Runnable 到这些访问方法之一:

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:


  • access 方法> 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.

有关更多讨论和 groovy diagram 我做了,看到这个类似的问题,如何将数据放入会话变量并在vaadin中的不同页面中获取数据?

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时需要锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆