Pregunta

Using the spring testing framework, my transactions do not roll back and I have absolutely no idea why. I've spent a good few days trying to find answers on SO but to no avail so I decided to post.

Testfile

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/applicationContextTest.xml")
@Transactional
public class joinTest {

@Autowired
private DAO AccountDAO;//My DAO is annotated with @Repository


@Before
public void beforeMethod()
{
    //log4j append code
}

@Test
public void saveMethod()
 {
    Account acct = new Account();
    acct.setUsername("USER");
    SmokeEvent evt = new SmokeEvent();
    evt.setDateSmoked(new DateTime());
    evt.setAccount(acct);
    AccountDAO.addSmokeEvent(evt);
    }
}

applicationContext.xml

<context:component-scan base-package="com.abstinence.Logic"/>
<context:annotation-config/>


<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
    <property name="url" value="jdbc:hsqldb:hsql://82.165.173.127/testdb"/>
    <property name="username" value="SA"/>
    <property name="password" value=""/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan" value="com.abstinence.Logic"/>
    <property name="hibernateProperties">
        <props>
            <prop key ="dialect">org.hibernate.dialect.HSQLDialect</prop>
            <prop key="javax.persistence.validation.mode">none</prop>
        </props>
    </property>
</bean>

<tx:advice id="txAdvice">
    <tx:attributes>
        <tx:method name="*"/>
    </tx:attributes>
</tx:advice>

<aop:config>
    <aop:pointcut id="hibernateDAOOperation" expression="execution(* com.abstinence.Logic.AccountDAO.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="hibernateDAOOperation"/>
</aop:config>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

I have checked the logs from log4j. The unusual thing is there is no mention of a rollback anywhere. There is a mention of the logs creating a session and inserting the data into the database but nothing about a rollback.

Someone please help me

¿Fue útil?

Solución

Adding the following annotation to your test class @TransactionConfiguration(defaultRollback = true) should fix your issue.

Otros consejos

You mention that the AccountDAO is annotated with the @Repository annotation. Have you also annotated the DAO with @Transactional? Without it, no transaction will be created when the addSmokeEvent() method is executed in the test. Consequently, the event will be added to your DAO, but the transaction manager cannot rollback the operation.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top