Spring commits on NOT_SUPPORTED propagation level
If you are working with spring and executing a DML query inside a transaction which has a propagation level of NOT_SUPPORTED, you might get surprised that the DML query gets committed despite the fact that the propagation level is NOT_SUPPORTED.
Here’s the sample code
public interface FooDAO { public void updateTable(String query); } public class FooDAOImpl implements FooDAO { //for testing @Transactional(propagation = Propagation.NOT_SUPPORTED) public void updateTable(String query) { //sql update method } } @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(...) @TransactionConfiguration(transactionManager="txManager", defaultRollback=true) public class MyTest { @Resource private FooDAO fooDAO; @Test public void testTransactionCommit() { fooDAO.updateTable("") } }
The table gets updated after the test case ends even when the test class has @TransactionConfiguration with defaultRollback=true.
This happens because JDBC is in autocommit mode by default. This means each individual statement is automatically committed when it completes successfully.
Neither NOT_SUPPORTED or SUPPORTS start a transaction, so whatever default state the Connection is in, will remain.
Posted on September 1, 2012, in jdbc, spring and tagged auto-commit, transactions. Bookmark the permalink. 1 Comment.
Keep on working, great job!|