在Spring + Hibernate + JTA項目中,我試圖讓異常處理工作。對於下面的代碼:春季休眠異常處理
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
public HandsetManufacturer createHandsetManufacturer(
HandsetManufacturer handsetManufacturer)
throws HandsetManufacturerAlreadyExistsException{
HandsetManufacturer handsetManufacturer2=new HandsetManufacturer();
try {
handsetManufacturerDao.findByUniqueProperty(
HandsetManufacturer.class, NAME_PROPERTY,
handsetManufacturer.getName());
throw new HandsetManufacturerAlreadyExistsException();
} catch (BusinessObjectNotFoundException ignoreMe) {
}
//handsetManufacturer2= handsetManufacturerDao.create(handsetManufacturer);
try{
handsetManufacturer2= handsetManufacturerDao.create(handsetManufacturer);
}catch (JDBCException e) {
System.out.println("::HibernateException::"+e.getSQL());
System.out.println("::HibernateException::"+e.getErrorCode());
System.out.println("::HibernateException::"+e.getSQLState());
System.out.println("::HibernateException::"+e.getSQLException());
System.out.println("::HibernateException::"+e.getMessage());
System.out.println("::HibernateException::"+e.getCause());
throw new TechnicalException(e);
}
return handsetManufacturer2;
}
我試圖抓住潛在的休眠/ JDBC/DB異常(例如,當實體依賴仍然存在刪除將失敗,org.springframework.orm.hibernate3.HibernateJdbcException)並執行一些操作。但是,捕獲代碼永遠不會到達。
但是,如果我從我的方法中刪除「@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)」,它將達到catch塊。 我想這與Spring管理這種方式有關,但我不知道我是如何在JDBCException期間捕獲異常並使用@Transaction註釋
任何幫助表示讚賞!