• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22787
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic