我通過Vosao CMS代碼,一個開源的CMS託管在谷歌應用程序引擎(我認爲這是一個真棒想法)瀏覽,我偶然發現了下面的代碼CurrentUser類中:在Google App Engine上進行部署時,是否將數據存儲在靜態字段中是否是線程安全的?
/**
* 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慣用語是什麼?不應該在這裏使用ThreadLocal,就像他們在Spring Security中一樣?或某種有限的bean(由依賴注入容器管理)?