Hi all,
I'm using Spring 1.1.5 with Hibernate 2.1.8.
I have a business service class that does two db calls and sends a mail. I have declared a transaction manager with declarative transaction management. I only want to send mail if both db calls are successfull, and I want to rollback the two db calls if sending of the mail fails.
Bean declarations:
Java code:Code:<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"> <property name="transactionManager"> <ref bean="hibernateTransactionManager" /> </property> <property name="transactionAttributes"> <props> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean> <bean id="myServiceManager" parent="baseTransactionProxy"> <property name="target"> <bean id="myTarget" class="com.mycomp.myapp.service.impl.MyManagerImpl"> <property name="dao1"><ref bean="dao1"/></property> <property name="dao2"><ref bean="dao2"/></property> <property name="mailSender"><ref bean="mailSender"/></property> </bean> </property> </bean>
dao1 and dao2 are classes extending HibernateDaoSupport and use HibernateTemplate to save objects.Code:public void saveData(Data d) { dao1.save(d.getSomething()); logger.debug("dao1"); dao2.save(d.getSomethingElse()); logger.debug("dao2"); mailSender.send(createMessage(d)); logger.debug("mail"); }
This works as intended when the db calls are ok and mailsending fails, but mails are sent even if the db calls fail.
I can see in my logs that all three calls in the saveData method is called even if the first one fails.
- How can I make the transaction manager return from the method if the first call fails?
- Is it possible with declarative transaction management, or do I need to look more into programmatic management?
- Any tips to change my code to make this happen?
Thanks in advance
Trond


Reply With Quote