开发者

Transaction not rolling back in Spring Test for delete operation

Somehow my test is not rolling back the delete transaction when doing a Spring Test. The data is deleted permanently. I am using Spring-Hibernate combo.

here is my test class:

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners( {TransactionalTestExecutionListener.class, 
 DependencyInjectionTestExecutionListener.class
})
@ContextConfiguration(locations={"/testApplicationContext.xml"})
@TransactionConfiguration(defaultRollback=true)
public class TestDummy  { 

private ApplicationContext context;

@Transactional
private AccountManager getAccountManager() {
    this.context = new ClassPathXmlApplicationContext("testApplicationContext.xml");
    return (AccountManager) context.getBean("accountManager");  
}



@Test
@Transactional
@Rollback(true)
public void testDeleteAccount(){

        Account acc = getAccountManager().getAccountDaoHibernate().get("87EDA29EBB65371CE04500144F54AB6D");
        System.out.println("Account name is "+acc.getAccountName());
        getAccountManager().deleteAccountHard(acc);
        Account acc1 = getAccountManager().getAccountDaoHibernate().get("87EDA29EBB65371CE04500144F54AB6D");
        if(acc1 != null){
        System.out.println("Now name is "+ acc1.getAccountName());
        }else{
            System.out.println("Account again is null");
        }

    }
}

I can see the message on console "Account again is null " which it should be. As its with in the test. But After test is finished. In database the record with id "87EDA29EBB65371CE04500144F54AB6D" is deleted permanently!. It should roll back after test is done. I am really confused why transactions are not rolling back.

Here is my testApplicationContext.xml entries:

        <bean id="accountManager"   class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            <property name="target"><ref local="accountManagerTarget"/></property>
            <property name="transactionManager"><ref local="transactionManager"/></property>
                    <property name="transactionAttributes">
                    <props>
                            <!-- Avoid PROPAGATION_REQUIRED !! It could kill your performances by generating a new transaction on each request !! -->

                            <prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop>
                            <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
                            <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
                            <prop key="add*">PROPAG开发者_如何转开发ATION_REQUIRED</prop>
                            <prop key="del*">PROPAGATION_REQUIRED</prop>

                    </props>
            </property>
            <property name="preInterceptors">
                <list>
                    <ref bean="hibernateInterceptor"/>
                </list>
            </property>         

    </bean>


<bean id="accountManagerTarget"
            class="com.db.spgit.abstrack.manager.AccountManager">
    <property name="accountDaoHibernate" ref="accountDaoHibernate" />
</bean>


<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configurationClass">
        <value>org.hibernate.cfg.AnnotationConfiguration</value>
    </property>
    <property name="configLocation">
        <value>classpath:hibernate-test.cfg.xml</value>
    </property>
</bean>

<bean id="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor">  
    <property name="sessionFactory">    
        <ref bean="sessionFactory"/>  
    </property>
</bean> 


<bean id="hibernateTemplate"
    class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>


Your test looks absolutely weird. @ContextConfiguration already loads application context, you don't need to do it manually.

The following code should work as expected:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/testApplicationContext.xml"})
@TransactionConfiguration(defaultRollback=true)
public class TestDummy  { 
    @Autowired
    private AccountManager accountManager;

    @Test
    @Transactional
    public void testDeleteAccount(){        
        Account acc = accountManager.getAccountDaoHibernate().get("87EDA29EBB65371CE04500144F54AB6D");
        System.out.println("Account name is "+acc.getAccountName());
        accountManager.deleteAccountHard(acc);
        Account acc1 = accountManager.getAccountDaoHibernate().get("87EDA29EBB65371CE04500144F54AB6D");
        if(acc1 != null){
            System.out.println("Now name is "+ acc1.getAccountName());
        }else{
            System.out.println("Account again is null");
        }           
    }
}

See also:

  • 9.3.5 Spring TestContext Framework
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜