File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Transaction not rolled back on a JDBC manual commit / Spring transaction / MySQL InnoDB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Transaction not rolled back on a JDBC manual commit / Spring transaction / MySQL InnoDB " Watch "Transaction not rolled back on a JDBC manual commit / Spring transaction / MySQL InnoDB " New topic
Author

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

Stephane Eybert
Ranch Hand

Joined: Mar 15, 2010
Posts: 34
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

Joined: Nov 09, 2011
Posts: 1031
    
    5

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

Joined: Mar 15, 2010
Posts: 34
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

Joined: Oct 27, 2005
Posts: 19791
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Stephane Eybert
Ranch Hand

Joined: Mar 15, 2010
Posts: 34
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Transaction not rolled back on a JDBC manual commit / Spring transaction / MySQL InnoDB