Announcement Announcement Module
No announcement yet.
Opinions: Handling HibernateException thrown by session.flush() in DAO Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Opinions: Handling HibernateException thrown by session.flush() in DAO

    I have inherited some code where exceptions are being thrown by session.flush() within the DAOs. I would like to handle these exceptions in a nice(non invasive?) way and would like to gather some opinions from the readers here.

    Spring/Hibernate has been setup to handle transactions via AOP service methods calls, just like the spring reference doc.

    I have thought of the following solutions:

    1) Cleanup all code and remove session.flush in order for the TransactionManager to handle the exceptions via the jdbcExceptionTranslator property.

    2) try/catch each session.flush and throw a custom exception to be handled higher up the application

    3) figure out how to use the AfterThrowing advice and throw a custom exception to be handled higher up the application ( this option I know very little of)

    Can you recommend for or against or maybe even propose another solution to this problem?

    See below code to put into perspective

    Spring/transaction config:

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource" ref="myDataSource" />
            <property name="mappingResources">
    <bean id="addressService" class="">
            <property name="addressDao" ref="addressDao" />
    <bean id="addressDao" class="">
            <property name="sessionFactory" ref="sessionFactory" />
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory">
                    <ref bean="sessionFactory" />
            <property name="jdbcExceptionTranslator">
                    <bean class=""/>
    <tx:advice id="txAdvice" transaction-manager="txManager">
                <tx:method name="get*" read-only="true" />
                <!-- all methods starting with 'get' are read-only -->
                <tx:method name="*" />
                <!-- other methods use the default transaction settings -->
            <aop:pointcut id="service" expression="execution(**.*(..))" />
            <aop:advisor advice-ref="txAdvice" pointcut-ref="service" order="1" />
    Service layer code:

    public class AddressServiceImpl implements AddressService {
     private AddressDao addressDao = null;
     // addressDao getter/setter 
     public void createAddress(Address add) 
    DAO code:
    public class AddressDaoHibernateImpl implements	AddressDao
        protected SessionFactory sessionFactory;
     // sessionFactory getter/setter 
        public void create(BaseObject s)
    	sessionFactory.getCurrentSession().flush(); // exception thrown for some reason such as PK violation
    Last edited by tharshan; Mar 1st, 2011, 12:49 PM. Reason: sp

  • #2

    Is my question to vague or has this topic been exhausted and no one wants to answer....previous threads end discuss how the service method must complete in order to rollback transaction


    • #3
      Decided to go with combination of 1 & 2 with my DAO's implementing HibernateTemplate

      #3 will not work for my setup.

      Used HibernatedTransactionManager jdbcExceptionHandler with a try/catch for any session.flush that could not be recoded. DAO's implement HibernateTemplate so the caught exception can be re-thrown as a DataAccessExcetion and handled by it's own jdbcExceptionHandler wired via the HibernateTemplate.

      Hope this helps someone.