日期:2014-05-18 浏览次数:20652 次
@Transactional
public void click(Goods goods) throws Exception {
if(goods != null) {
try {
GoodsCount goodsCount = goodsCountDao.findUniqueBy(GoodsCount.PROPERTY_NAME_FOR_GOODS_ID, goods.getId());
if(goodsCount == null) {
goodsCount = new GoodsCount();
goodsCount.setGoods(goods);
goodsCount.setId(UuidUtils.randomUuid());
}
goodsCount.setClickCount(goodsCount.getClickCount() + 1);
goodsCountDao.save(goodsCount);
} catch(Exception e) {
LoggerUtils.logException(e);
//版本异常或者违反唯一约束
if(e instanceof StaleObjectStateException || e instanceof ConstraintViolationException || e instanceof DataIntegrityViolationException) {
//再次加重重新处理一遍
GoodsCount goodsCount = goodsCountDao.findUniqueBy(GoodsCount.PROPERTY_NAME_FOR_GOODS_ID, goods.getId());
goodsCount.setClickCount(goodsCount.getClickCount() + 1);
goodsCountDao.save(goodsCount);
} else {
throw e;
}
}
}
}
觉得LZ捕获不到的原因是因为调用其它方法里面捕获了异常,但是没有再次抛出来。
看看你的findUniqueBy、save等方法,是否存在try-catch块,都做了什么处理。
不会啊,我在调用这个service的controller方法里再次捕获异常,就能捕获到