Here is my test:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:repositoryContextTest.xml" }) @Transactional @TransactionConfiguration(defaultRollback = true) public class SeasonITest { @Autowired private SeasonDao seasonDao; @Test public void createSeason() throws Exception { Season season = new Season(); season.setName("2012"); seasonDao.createSeason(season); }
and dataSource in the bean configuration file
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost/tournament_system" /> <property name="username" value="root" /> <property name="password" value="root" /> <property name="defaultAutoCommit" value="false"/> <property name="poolPreparedStatements" value="false"/> <property name="maxOpenPreparedStatements" value="0"/> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" />
When I run this test, a new record is created in my database.
How can I cancel this transaction?
This is the output of the log I see:
2012-06-15 15:00:02,173 [main] INFO - ionalTestExecutionListener - Rolled back transaction after test execution for test context [[ TestContext@76db09 testClass = SeasonITest, locations = array<String>['classpath:repositoryContextTest.xml'], testInstance = org.toursys.repository.dao.SeasonITest@1265109 , testMethod = createSeason@SeasonITest , testException = [null]]]
UPDATE:
all the answers below want to change the logic or mechanism of the database, which I don't want. Therefore, I offer a reputation for the correct answer:
Why, when I have this: @TransactionConfiguration(defaultRollback = true) in the transaction configuration tests do not roll back and how can I fix it?
source share