Error in managing Spring trasaction Declaratively.
Iam trying to use Spring Trasaction manager to declaratively manage my trasactions , but the trasaction is not rolling back as desired on throwing an Exception. The xml file where Iam defining the configurations (test-dao.xml) is as shown below.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schem...ng-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>jdbc.properties</value>
</list>
</property>
</bean>
<bean id="localTestRepo" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
<!-- <property name="maxActive" value="10" />
<property name="initialSize" value="5" />
<property name="maxIdle" value="5" />
<property name="maxWait" value="10000" /> -->
</bean>
<!-- Hibernate Session Factory -->
<bean id="testSessionFactory" class="org.springframework.orm.hibernate.LocalSess ionFactoryBean">
<property name="dataSource" ref="localTestRepo" />
<!-- <property name="lobHandler" ref="lobHandler"/> -->
<property name="mappingResources">
<list>
<value>test/test1.hbm.xml</value>
<value>test/test2.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">net.sf.hibernate.dialect.O racle9Dialect</prop>
</props>
</property>
</bean>
<!-- Transactional Configs-->
<bean id="transactionManager" class="org.springframework.orm.hibernate.Hibernate TransactionManager">
<property name="sessionFactory" ref="testSessionFactory" />
</bean>
<!-- Advice -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
<tx:method name="retrieve" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- Advisor and pointcut -->
<aop:config>
<aop:advisor pointcut="execution(* com.equifax.test.dao.TestSaveInterface.saveRetriev e(..))" advice-ref="txAdvice"/>
</aop:config>
<!-- ************ --><!--
<tx:advice id="txAdvice" transaction-manager="transactionManager1">
<tx:attributes>
<tx:method name="wrap*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="aopTransactionBean" expression="execution(* com.equifax.aspire.persistence.AOPTransactionTestB ean.*(...))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="aopTransactionBean"/>
</aop:config>
--><!-- ************ -->
<bean id="testDao" class="com.equifax.test.dao.BasicTestDao">
<property name="sessionFactory" ref="testSessionFactory" />
</bean>
</beans>
The Class where Iam testing this Transaction is :
package com.equifax.test.dao;
public class TestSave implements TestSaveInterface{
BasicTestDao dao = (BasicTestDao)SpringContextHolder.ctx().getBean("t estDao");
/**
* @param args
*/
public static void main(String[] args)
{
TestSave tsave = new TestSave();
SavableObject obj1 = new TestObj1();
SavableObject obj2 = new TestObj1();
obj1.setName("Robin");
obj1.setDesignation("Senior Analyst");
tsave.saveRetrieve(obj1, obj2);
}
public void saveRetrieve(SavableObject obj1, SavableObject obj2)
{
TestSave tsave = new TestSave();
tsave.save(obj1);
obj2 = (TestObj1) tsave.retrieve(new Long(23),obj2 );
System.out.println("result"+obj2.getDesignation()) ;
}
public void save(SavableObject obj)
{
dao.save(obj);
new Exception();
}
public Object retrieve(Long id,Object obj)
{
return dao.findById(obj.getClass(), id);
}
}
As can be seend from the test-dao.xml , am defining a pointcut on saveRetrive method, and it can be seen that my advice , defines a rollback on save method when an exception is thrown. The SavableObjcet is getting saved even when the exception is thrown. What is that Iam doing worng , or is itnot the way to implement trasaction management in Spring. any help is appreciated.
thanks in advance!!!