为什么没有回滚事务呢?
来源:4-5 店铺注册之Service层的实现
liangwc3489305
2017-12-08
@Service
public class ShopServiceImpl implements ShopService {
@Autowired
private ShopDao shopDao;
@Override
@Transactional
public ShopExecution addShop(Shop shop, File shopImg) {
if (shop == null) {
return new ShopExecution(ShopStateEnum.NULL_SHOP);
}
try {
shop.setEnableStatus(0);
shop.setCreateTime(new Date());
shop.setLastEditTime(new Date());
int effectedNum = shopDao.insertShop(shop);
if (effectedNum <= 0) {
throw new RuntimeException("店铺创建失败");
} else {
if (shopImg != null) {
try {
// 存储图片
addShopImg(shop, shopImg);
} catch (Exception e) {
throw new ShopOperationException("addShopImg error:" + e.getMessage());
}
// 更新店铺图片地址
effectedNum = shopDao.updateShop(shop);
if (effectedNum <= 0) {
throw new ShopOperationException("更新图片地址失败");
}
}
}
} catch (Exception e) {
throw new ShopOperationException("addShop error:" + e.getMessage());
}
return new ShopExecution(ShopStateEnum.CHECK, shop);
}
private void addShopImg(Shop shop, File shopImg) {
// 获取shop图片目录的相对自路径
String dest = PathUtil.getShopImagePath(shop.getShopId());
String shopImgAddr = ImageUtil.generateThumbnail(shopImg, dest);
shop.setShopImg(shopImgAddr);
}
}public class ShopOperationException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 8385422554610647351L;
public ShopOperationException(String msg) {
super(msg);
}
}
写回答
1回答
-
同学这个问题有点宽泛呢,只要完成事务前能够抛出runtimeexception就会回滚 但是物理图片保存的话是没办法回滚的 只能回滚数据库操作 同学可以用程序处理删库
012017-12-08
相似问题
事务回滚
回答 1
4-5 事务不回滚?
回答 1