后台调用数据库如果发生查询等操作异常,将在DAO层抛出指定的异常信息,然后在service层捕获到这个异常,将友好的信息返回给客户端.
eg:
public Connection getConnection() throws DaoException {
log.info("数据层:获取数据库的JDBC连接");
try {
return getSession().connection();
} catch (Exception e) {
throw new DaoException(DaoException.ERRORCODE_CONNECTION);
}
}
如下的service层 处理DAO层抛出的异常信息 ,
public RPCMessage getDateList(String uniquedId) throws ServiceException {
log.info("服务层操作:初始化全生命周期时间轴的时间范围");
String sql = "";
List list = new ArrayList();
try {
List<Object> parmaters = new ArrayList<Object>();
parmaters.add(uniquedId);
//sql = "select DISTINCT SUBSTR(to_char(DATE_TIME), 0, 7) strDate from CDA_SUMMARY where HRID = ? " ;
sql = "select DISTINCT to_char(DATE_TIME,'yyyymm') year , COUNT(to_char(VI.DATE_TIME,'yyyymm')) num from VI_CDA_SUMMARY vi where HRID = ? GROUP BY to_char(VI.DATE_TIME,'yyyymm')";
List<Map<String, Object>> roleInfoList = this.cdaDao.executeQuerySQL(sql, parmaters.toArray());
if(roleInfoList.size()>0){
list = roleInfoList;
}else{
//返回当前日期的年月
Calendar cal = Calendar.getInstance();
int month = cal.get(Calendar.MONTH) + 1;
int year = cal.get(Calendar.YEAR);
String currentDate = String.valueOf(year)+String.valueOf(month);
list.add(currentDate);
}
log.debug("服务层操作:初始化全生命周期时间轴的时间范围操作完成!");
} catch (Exception e) {
//CustomerContextHolder.setCustomerType("phrm1");
return getRPCMessage(e);
}
return getRPCMessage(new Object[] { list });
}
调用的自定义异常会返回定义的友好信息. (构造函数是好东西啊)