Announcement Announcement Module
Collapse
No announcement yet.
Opinions: Handling HibernateException thrown by session.flush() in DAO Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:

    Code:
       
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource" ref="myDataSource" />
            <property name="mappingResources">
                <list>
                        <value>
    			my/company/domain/Address.hbm.xml
                        </value>
                </list>
            </property>
    </bean>
    
    <bean id="addressService" class="my.company.serviceImpl.AddressServiceImpl">
            <property name="addressDao" ref="addressDao" />
    </bean>
    
    <bean id="addressDao" class="my.company.daoImpl.AddressDaoHibernateImpl">
            <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory">
                    <ref bean="sessionFactory" />
            </property>
            <property name="jdbcExceptionTranslator">
                    <bean class="my.company.core.BusinessRuleErrorCodesTranslator"/>
            </property>
    </bean>
    
    <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
                <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 -->
            </tx:attributes>
    </tx:advice>
    
    <aop:config>
            <aop:pointcut id="service" expression="execution(* my.company.serviceImpl.*.*(..))" />
            <aop:advisor advice-ref="txAdvice" pointcut-ref="service" order="1" />
    </aop:config>
    Service layer code:

    Code:
    package my.company.serviceImpl;
    
    import my.company.service.AddressService;
    import my.company.dao.AddressDao;
    
    public class AddressServiceImpl implements AddressService {
    
     private AddressDao addressDao = null;
    
     // addressDao getter/setter 
    
     public void createAddress(Address add) 
        {
            addressDao.create(add);
        }
    }
    DAO code:
    Code:
    package my.company.daoImpl;
    public class AddressDaoHibernateImpl implements	AddressDao
    {
        protected SessionFactory sessionFactory;
    
     // sessionFactory getter/setter 
    
        public void create(BaseObject s)
        {
    	sessionFactory.getCurrentSession().save(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
    anyone?

    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

    Comment


    • #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.

      Comment

      Working...
      X