2012-06-12 4 views
0

Spring + Hibernate + JTA 프로젝트에서 나는 예외 처리 작업을 시도하고있다.Spring Hibernate Exception Handling

@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; 
} 

내가 (종속 실체는 여전히 org.springframework.orm.hibernate3.HibernateJdbcException 실패합니다 제거를 제시 할 때, 예를 들어) 기본 최대 절전/JDBC/DB 예외를 잡으려고 노력하고있어 다음 코드 몇 가지 작업을 수행하십시오. 그러나 catch 코드에 도달하지 않습니다.

하지만 "@Transactional (propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)"을 제거하면 내 방식에서 블록을 잡을 수 있습니다. 이것이 스프링이 관리하는 방식과 관련이 있다고 생각합니다. 그러나 JDBCException 동안 예외를 잡아낼 수 있는지, 그리고 @Transaction annotation을 사용하는 방법을 모르겠다.

도움이 되었습니까?

답변

0

귀하의 DAO도 REQUIRED 전달을 사용하여 트랜잭션을 구성하도록 설정되어 있습니다. 최대 절전 모드에서는 세션이 플러시 될 때까지 많은 작업이 지연됩니다. 플러시가 발생할 타이밍 중 하나는 트랜잭션 커밋 이전입니다. 그것은 당신의 메소드 (일부 서비스의 메소드라고 생각합니다)가 트랜잭션을 처리하고 있다면 Hibernate를 지속 시키거나 DAO에서 액션을 저장하는 것이 service 메소드가 완료 될 때까지 DB에 실제로 가지 않는다는 것을 의미합니다.

한 가지 해결 방법은 명시 적으로 세션 플러시() (DAO의 create()를 넣는 것을 고려할 수 있음)를 수행하여 DB 업데이트를 트리거하고 예상되는 예외가 발생할 수 있도록합니다.