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.

Advertisements

Posted on September 1, 2012, in jdbc, spring and tagged , . Bookmark the permalink. 1 Comment.

  1. Keep on working, great job!|

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: