在 Google App Engine 上部署时,将数据存储在静态字段内是否线程安全?

发布于 2024-09-29 15:07:35 字数 1091 浏览 4 评论 0原文

我正在浏览 Vosao CMS 的代码,这是一个托管在 Google App Engine 上的开源 CMS(我认为它是一个很棒的主意),我在 CurrentUser 类:

/**
 * Current user session value cache class. Due to GAE single-threaded nature
 * we can cache currently logged in user in static property. Which we set in 
 * authentication filter.
 */
public class CurrentUser {

        private static UserEntity user;

        public static UserEntity getInstance2() {
                return user;
        }

        public static void setInstance2(UserEntity aUser) {
                user = aUser;
        }
}

我从未使用过 GAE,但这对我来说听起来很奇怪。

  • GAE 真的是“单线程”吗?使用 GAE 时将请求范围的数据存储在静态字段内是否安全?

  • 这是否意味着,对于每个 JVM 实例,一次只会执行一个 HTTP 请求,而所有其他请求都在等待?

  • 这是常见的 GAE 习语吗?如果不是,那么在请求期间存储此类 UserEntity 的最佳 GAE 习惯是什么?难道不应该像 Spring Security 中那样使用 ThreadLocal 吗?或者某种作用域 bean(由依赖注入容器管理)?

I was browsing through the code of Vosao CMS, an open source CMS hosted on Google App Engine (which I think is an awesome idea), and I stumbled upon the following code inside the CurrentUser class:

/**
 * Current user session value cache class. Due to GAE single-threaded nature
 * we can cache currently logged in user in static property. Which we set in 
 * authentication filter.
 */
public class CurrentUser {

        private static UserEntity user;

        public static UserEntity getInstance2() {
                return user;
        }

        public static void setInstance2(UserEntity aUser) {
                user = aUser;
        }
}

I've never used GAE, but this sounds really weird to me.

  • Is GAE really "single threaded"? Is it safe to store request-scoped data inside a static field when using GAE?

  • Does this mean that, for each JVM instance, only one HTTP request will be executed at a time, while all the other requests are waiting?

  • Is this a common GAE idiom? If not, what would be the best GAE idiom to store such an UserEntity for the duration of a request? Shouldn't one use a ThreadLocal here, like they do in Spring Security? Or some kind of scoped bean (managed by the Dependency Injection container)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半暖夏伤 2024-10-06 15:07:35

GAE 真的是“单线程”吗?使用 GAE 时将请求范围的数据存储在静态字段内是否安全?

它曾经是这样的(直到 1.4.3)并且默认情况下仍然如此。

现在,您可以指定您的应用是线程安全的,然后您将收到对同一个 JVM/servlet 的并发请求。

这是否意味着,对于每个 JVM 实例,一次只会执行一个 HTTP 请求,而所有其他请求都在等待?

对于其他请求,您可能会获得另一个 JVM。但这是你无法控制的。他们也可以只是等待。

Is GAE really "single threaded"? Is it safe to store request-scoped data inside a static field when using GAE?

It used to be that way (until 1.4.3) and it still is by default.

Now, you can specify that your app is threadsafe, and then you will receive concurrent requests to the same JVM/servlet.

Does this mean that, for each JVM instance, only one HTTP request will be executed at a time, while all the other requests are waiting?

For other request, you would probably get another JVM. But that is outside of your control. They can also just wait.

青春如此纠结 2024-10-06 15:07:35

目前,App Engine 上的 Java 和 Python 运行时都是单线程的;你是对的,这意味着每个 JVM 只会执行一个 HTTP 请求,但会同时启动多个 JVM 来处理多个传入请求。

然而,这可能在未来随时改变 - Java Servlet 规范允许多线程。因此,您绝对应该使用 ThreadLocal。

Currently, the Java and Python runtimes on App Engine are both single threaded; you're correct that this means only one HTTP request will be executed per JVM, but multiple JVMs will be started simultaneously to handle multiple incoming requests.

This could change at any time in the future, however - the Java Servlet spec permits multi-threading. As a result, you should definitely use a ThreadLocal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文