我这是关系到逻辑不是技术问题,这里是一个场景,(我用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