Q:

Spring Hibernate Exception Handling

In a Spring + Hibernate + JTA project I'm trying to get exception handling working. For the following code:
@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;
}

I'm trying to catch underlying hibernate/jdbc/db exceptions (for example when dependent entities are still present the remove will fail with a org.springframework.orm.hibernate3.HibernateJdbcException) and perform some actions. However the catch code is never reached.
But if i remove "@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)" from my method it will reach to catch block.
I guess this has to do with the way Spring manages this, but I don't know just how I can catch exceptions during JDBCException and also use @Transaction annotation
Any help is appreciated!

A:

I guess your DAO is also configured to have transaction with REQUIRED propagation. In Hibernate, there are lots of action delayed until the session is flushed. One of the timing that flush will happen is before transaction commit. That means if your method (I guess it is a method in some service) is having transaction around it, Hibernate persist or save actions in your DAO create() are not actually going to DB until finish of your service method. 
One workaround is explicitly perform a Session flush() (you may consider putting in DAO's create() ), so that it will trigger DB update and hence cause exception you expect to be thrown.

