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:
Service layer code: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>
DAO 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); } }
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 } }


Reply With Quote
