2008年7月15日 星期二

Try Statement Format

1. set the conn = DatabaseManager.getConnection(); in the begginning and once it one time only.
2. set the conn.rollback(); when the error happen and set the SQL will not keep running.
3. call function by following command and dun open the connection again.
ContractorDAO conDAO = new ContractorDAO();
conDAO.setConnection(conn);


try {

conn = DatabaseManager.getConnection();

log.info("TRY : JobSvc.APP_ID...................." + JobSvc.APP_ID);
log.info("TRY : JobSvc.RCW_VIEW...................." + JobSvc.RCW_VIEW);
log.info("TRY : userId................." + userId);
log.info("TRY : theForm.getCurrentUserIdInt()................." + theForm.getCurrentUserIdInt());
log.info("TRY : Check siteCode !!! ......................." + siteCode );
log.info("check isEmpty() --------------------------> : " + errors.isEmpty());

// check conn is null
if (conn == null || conn.isClosed()) {
conn = null;
log.error("conn == null || conn.isClosed()");
errors.add("SystemError", new ActionMessage("hrms.error.systemError"));
forwardmapping = "result";
return mapping.findForward(forwardmapping);
}

// check conn ok and then check access right
//if (!ValidateAccess.canAccessBySiteCodeServiceIdUserId(conn, siteCode, JobSvc.RCW_CONTRACTOR_REPORT, userId)){

/******* Access right ***********************
if (!ValidateAccess.canAccessBySiteCodeServiceIdUserId(conn, siteCode, JobSvc.RCW_VIEW, userId)){
errors.add("accessrights",new ActionMessage("error.access.norights"));
saveErrors(request,errors);
if (conn == null && !conn.isClosed()) {
conn.close();
conn = null;
}
return mapping.findForward("Error");
}
******* Access right ***********************/

}

// set the contractor list
ContractorDAO conDAO = new ContractorDAO();
conDAO.setConnection(conn);

catch (ExceedLimitException e){
try {if (conn!=null && !conn.isClosed()) {
conn.rollback();}
} catch (SQLException ec){}
log.error("RcwOutputAction Exception" + e);
errors.add("loadExceed" ,new ActionMessage("error.rcw.result.exceed1000"));
} catch (Exception ex) {
try {if (conn!=null && !conn.isClosed()) {
conn.rollback();}
} catch (SQLException ec){}
log.error("RcwOutputAction Exception" + ex);
ex.printStackTrace();
errors.add("load" ,new ActionMessage("error.loadError"));
} finally{
request.setAttribute("totalRecord",totalRecord); // send the totalRecord to the Result JSP page
try {
if (conn != null) {
conn.close();
conn = null;
}
}
catch (Exception e) {}
}

if (errors.isEmpty()) {
action = RcwOutputFileForm.ACTION_LIST;
} else {
action = RcwOutputFileForm.ACTION_SEARCH;
}

}//end if : action_list
log.info("check isEmpty() --------------------------> : " + errors.isEmpty());

if (action != null && action.equals(RcwOutputFileForm.ACTION_SEARCH)){
log.info("ACTION_SEARCH : " + action);
forwardmapping = RcwOutputFileForm.ACTION_SEARCH;
}

if (!errors.isEmpty()){
saveErrors(request,errors);
log.error("error is not empty!");
}

log.info("forwardmapping: " + forwardmapping);
return mapping.findForward(forwardmapping);

沒有留言: