• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Transaction not rolled back on a JDBC manual commit / Spring transaction / MySQL InnoDB

 
Stephane Eybert
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have an integration test and am trying to have the database go back to its original state at the end of the test run.

The MySQL table has the InnoDB engine.

The test is based on the Spring AbstractTransactionalJUnit4SpringContextTests class.

There is a transaction manager:



The log shows the transaction being rolled back:

2012-03-09 10:32:20,990 DEBUG [DataSourceTransactionManager] Initiating transaction rollback
2012-03-09 10:32:20,990 DEBUG [DataSourceTransactionManager] Rolling back JDBC transaction on Connection [com.mysql.jdbc.JDBC4Connection@979f67]
2012-03-09 10:32:20,991 DEBUG [DataSourceTransactionManager] Releasing JDBC Connection [com.mysql.jdbc.JDBC4Connection@979f67] after transaction
2012-03-09 10:32:20,992 DEBUG [DataSourceUtils] Returning JDBC Connection to DataSource
2012-03-09 10:32:20,993 INFO [TransactionalTestExecutionListener] Rolled back transaction after test execution for test context [[TestContext@105bd58 testClass = GInnAdresseDaoTest, testInstance = no.nki.sesam.dao.GInnAdresseDaoTest@1d1e730, testMethod = testInsertAndFindWithId@GInnAdresseDaoTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@1a116c9 testClass = GInnAdresseDaoTest, locations = '{classpath:integration.jdbc.xml, classpath:dao.xml, classpath:integration.data-source.xml}', classes = '{}', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader']]]

Maybe it is due to the manual commit in the JDBC dao ?



Any idea ?

Stephane
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephane

In general terms, this looks a bit hacky to me.

- set auto commit to false
- commit a change
- set auto commit to true

Why are you doing this?
 
Stephane Eybert
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good question.

I was thinking I should put the auto commit state back in its original default value.

But do I need to do that ? Probably no in fact.

I removed the code chunk:

try {
connection.setAutoCommit(true);
} catch (SQLException e) {
ExceptionUtils.printRootCauseStackTrace(e);
}

but it did not help.

The roll back still is not happening.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would it perform a rollback? That only occurs if auto-commit is turned off and you a) close the connection without committing, or b) call rollback(). I see neither.
 
Stephane Eybert
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I went for a

protected NamedParameterJdbcTemplate namedParameterJdbcTemplate;

public void setDataSource(DataSource dataSource) {
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}

and the transaction behaves nicely now.

I guess I was mixing two different things, handling the connection manually inside a Spring transaction was not a good idea.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic