我有一个插入数据库的表单但是在我预先形成插入之前我检查是否存在现有记录。我无法插入记录,而且我收到 StackOverflow 错误。
有人能告诉我导致这个错误的原因以及我可以做些什么来摆脱它?如果我删除检查hasRecords代码预先形成好。它几乎就像代码不想在此时访问数据库。我已经创建了不同类型的函数来预先形成他在这里检查记录但是此时我一直得到相同的错误 StackOverflow
代码
com.crimetrack.service.MonitoringManager.getMonitoringStDate(MonitoringManager.java:60)
public String getMonitoringStDate(Integer crimeRecNo,
Integer socialSecurityNumber) throws Exception {
return this.getMonitoringStDate(crimeRecNo, socialSecurityNumber);
}
DAO for getMonitoringStDate
public String getMonitoringStDate(Integer crimeRecNo, Integer socialSecurityNumber){
//select the minimum start date in event there is several records although this will not be allowed
String sql = "select IFNULL(min(monitoringStDate),'0') as monitoringStDate from tblmonitoring where crimeRecNo = ? and socialSecurityNumber = ?";
String monitoringStDate = (String)getJdbcTemplate().queryForObject(sql, new Object[]{crimeRecNo,socialSecurityNumber}, String.class);
return monitoringStDate;
}
我还注册了以下听众:
监听
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
代码
DAO
public boolean hasMonitoringRecord(Integer crimeRecNo, Integer socialSecurityNumber){
//select a count of monitoring records there should be only one monitoring record
String sql = "select count(*) as monitoringStDate from tblmonitoring where crimeRecNo = ? and socialSecurityNumber = ?";
Integer count = (Integer)getJdbcTemplate().queryForObject(sql, new Object[]{crimeRecNo,socialSecurityNumber}, Integer.class);
if(count > 0 ){
return true;
}else{
return false;
}
}
控制器
if(result.hasErrors()){
myMonitoringTypeList.put("monitoringTypeList",this.monitoringTypeManager.getListOfMonitoringType());
model.addAttribute("icon", "ui-icon ui-icon-circle-close");
model.addAttribute("results", "Error: Unable to Save Record!");
model.addAttribute("monitoringType",myMonitoringTypeList);
model.addAttribute("monitoring",monitoring);
model.addAttribute("records", session.getAttribute("records"));
model.addAttribute("crimeRecNo",session.getAttribute("crimeRecNo"));
//return the user to the page they were on initially not the first page but the one with the error
return new ModelAndView("monitoringList","page",session.getAttribute("page"));
}else{
int crimeRecNo = monitoring.getCrimeRecNo();
int socialSecurityNumber = monitoring.getSocialSecurityNumber();
logger.info("No errors going to process records");
logger.info("CrimeRecNo is " + monitoring.getCrimeRecNo());
logger.info("SocialSecurity Number is " + monitoring.getSocialSecurityNumber());
//check if a record already exists. if one exist do an update else do an insert
//to check if the record exists we can simply get the start date
String dbMonitoringStDate = monitoringManager.getMonitoringStDate(crimeRecNo,socialSecurityNumber);
logger.info("The dbMonitoringStDate is " + dbMonitoringStDate);
if(monitoringManager.hasMonitoringRecord(crimeRecNo, socialSecurityNumber)){
logger.info("Has monitoring record");
//do an update
monitoringManager.updateMonitoringRecord(monitoring);
model.addAttribute("icon", "ui-icon ui-icon-circle-check");
model.addAttribute("results", "Record Was Updated");
}else {
logger.info("Does not have monitoring record");
//do an insert
monitoringManager.saveMonitoringRecord(monitoring);
model.addAttribute("icon", "ui-icon ui-icon-circle-check");
model.addAttribute("results", "Record Was Saved");
}
错误
7035 [http-bio-8084-exec-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@6f138f45
7035 [http-bio-8084-exec-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Could not complete request
org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.StackOverflowError
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:972)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.StackOverflowError
at com.crimetrack.service.MonitoringManager.getMonitoringStDate(MonitoringManager.java:60)
at com.crimetrack.service.MonitoringManager.getMonitoringStDate(MonitoringManager.java:60)
at com.crimetrack.service.MonitoringManager.getMonitoringStDate(MonitoringManager.java:60)
at com.crimetrack.service.MonitoringManager.getMonitoringStDate(MonitoringManager.java:60)
at com.crimetrack.service.MonitoringManager.getMonitoringStDate(MonitoringManager.java:60)
答案 0 :(得分:2)
查看您的方法定义:
public String getMonitoringStDate(Integer crimeRecNo,
Integer socialSecurityNumber) throws Exception {
return this.getMonitoringStDate(crimeRecNo, socialSecurityNumber);
}
这种方法唯一能做的就是调用自己。很明显,你有一个无限的递归循环。