我這是關係到邏輯不是技術問題,這裏是一個場景,(我用Spring + Hibernate的)如何數據字段從數據庫Spring MVC中存儲
我需要閱讀一些數據從數據庫返回到每個獲取請求的頁面,但我想這裏有一些破解,如果使用某個腳本某人非常頻繁地重新加載頁面,這會導致多次調用服務器,因爲我認爲讀取數據並將它們放入全局變量或類變量,通過這樣做我最終編寫了很奇怪的代碼許多全局變量和愚蠢的方式來給它們初始值,就像變量用戶狀態這是一個字節型變量我已經給-2作爲初始值,所以我的內部邏輯可以理解爲數據庫中的這個變量沒有設置值,下面是我的代碼
@Controller
/* @Secured("hasRole('ROLE_USERS')") */
@RequestMapping("member")
public class ApplyRoles {
@Autowired
private UserInformationForAccessApplication checkUserStatus;
// we will initialize variables to avoid auto-initialize by constructor
private byte userStatus = Constant.IntializationOfGlobalVariable.GLOBALINIT,
requesttype = Constant.IntializationOfGlobalVariable.GLOBALINIT,
access = Constant.IntializationOfGlobalVariable.GLOBALINIT;
Map<String, Object> accessnrole;
Map<String, String> country;
Map<String, String> roleArray;
@Autowired
StudentEnrollmentApplication enrollmentApplication;
@Autowired
SystemProperties systemProperties;
@Autowired
EmployeeEnrollmentApplicationResume employeeEnrollmentApplicationResume;
@Autowired
AccessEnrollmentProcessing accessEnrollmentProcessing;
private String role = Constant.IntializationOfGlobalVariable.ROLENOTSET,
fname, lname;
@RequestMapping(value = "/user", method = RequestMethod.GET)
public String checkingUserStatus(Model model, HttpSession session,
Authentication authentication) {
String sessionemail = "[email protected]";// (String) session
// .getAttribute(Constant.SessionAttributes.LOGGEDINUSER);
// first check global value, if found set than don't fetch from database
if (userStatus == Constant.IntializationOfGlobalVariable.GLOBALINIT) {
// get user status from MySQL Database
userStatus = checkUserStatus.checkStatus(sessionemail).get(0);
if (!(userStatus == Constant.UserRoleApplicationStatus.NOTAPPLIED)) {
access = checkUserStatus.checkStatus(sessionemail).get(1);
model.addAttribute(Constant.SystemName.ACCESS, access);
}
}
if (!(userStatus >= Constant.UserRoleApplicationStatus.NOTAPPLIED || userStatus <= Constant.UserRoleApplicationStatus.REJECTED)) {
model.addAttribute("error", "User status is not avaible");
return "redirect:error/pagenotfound";
} else if (userStatus == Constant.UserRoleApplicationStatus.NOTAPPLIED) {
if (requesttype == Constant.IntializationOfGlobalVariable.GLOBALINIT) {
// get request type from MongoDB database
requesttype = checkUserStatus.getRequestType(sessionemail);
}
if (!(requesttype == Constant.RequestType.NORMALEBIT || requesttype == Constant.RequestType.INVITEBIT)) {
model.addAttribute("error",
"Facing Technichal Issue, Please try again");
return "redirect:error/pagenotfound";
}
if (requesttype == Constant.RequestType.INVITEBIT) {
if (!(Byte.parseByte((String) accessnrole
.get(Constant.SystemName.ACCESS)) == Constant.Access.USERBIT)) {
accessnrole = checkUserStatus
.getAccessAndRole(sessionemail);
}
if (accessnrole.get(Constant.SystemName.ACCESS).equals(
Constant.Database.ERRORMESSAGE)
|| accessnrole.get(Constant.SystemName.ROLE).equals(
Constant.Database.ERRORMESSAGE)) {
model.addAttribute("error",
"Facing Technichal Issue, Please try again");
return "redirect:error/pagenotfound";
}
model.addAttribute(Constant.SystemName.ACCESSNROLE, accessnrole);
model.addAttribute(Constant.SystemName.REQUESTTYPE, requesttype);
}
}
model.addAttribute(Constant.SystemName.USERSTATUS, userStatus);
return "member/user";
}
}
避免全局變量我想起訴的餅乾,因爲我不想讓調用數據庫在同一會話的每個頁面重新加載,一旦其加載會話做的比我不用調用數據庫做。
任何可以幫助重新設計的代碼上面的部分是非常讚賞
感謝
這完全是錯誤的級別,試圖抵禦DDOS攻擊。如果你的java應用程序服務器暴露在公共互聯網上,它應該在apache後面被代理,並且apache應該防禦它。 – Affe 2013-03-06 20:42:14
@Affe我同意你在Apache(Web服務器)上應該處理這種廢話,但這也與性能有關,比方說,用戶每2分鐘重新加載一次頁面,我認爲Apache應該不會有這種行爲的任何問題。我正在通過ehcahe採取什麼措施,使用ehcahe緩存結果,一旦寫入就通過緩存提供服務。是緩存機制像Ecache是會話相關 – Yashprit 2013-03-06 20:54:53