Search This Blog

Thursday, October 28, 2010

Transaction rollback on Spring JDBC tests

Programmer Question

I'm trying to get JDBC transaction rollback when using Spring-test without success. When I run the following the SQL update is always committed.



package my.dao.impl;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.transaction.TransactionConfiguration;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class})
@ContextConfiguration(locations={"classpath:ApplicationContext-test-DAOs.xml"})
@TransactionConfiguration(defaultRollback = true)
public class ConfirmationMatchingDAOImplTest {

@Autowired
private DataSource dataSource;

@Test
public void testIsCounterPartyMatched_Found1Row() throws Exception {
final Connection connection = dataSource.getConnection();
final Statement statement = connection.createStatement();
statement.executeUpdate("insert into TEST_INSERT values (1, 'hello')");
statement.close();
connection.close();
}
}















What am I doing wrong?



Additionally, am I using too many annotations? Can I make it a bit cleaner?



Find the answer here

1 comment:

  1. What kind of dataSource are U using? If you are using commons dbcp , you can set the property "defaultAutoCommit" to false.
    You can also try calling connection.setAutoCommit(false);

    ReplyDelete

Related Posts with Thumbnails