Results 1 to 4 of 4

Thread: [Spring 3.0][JUnit 4.8] How to stop and start new transactions in JUnit tests

  1. #1
    Join Date
    Nov 2010
    Posts
    8

    Default [Spring 3.0][JUnit 4.8] How to stop and start new transactions in JUnit tests

    Hello,

    Using JUnit 3.x and org.springframework.test.AbstractTransactionalSpri ngContextTests in Spring 2.5, I was able to start and stop transactions when I wanted. As it is explained in Spring 2.5 reference documentation for transaction management in Unit test (http://static.springsource.org/sprin...it38-legacy-tx), I just called methods endTransaction(..) and startNewTransaction(..).

    Now, I would like to do the same thing with Spring 3.0 and JUnit 4.8.1. I read the reference documentation for unit testing (http://static.springsource.org/sprin...l/testing.html) but I did not find anything to do the same thing. As far as I have understood, the transaction management in Unit tests is now done via @Transactional (which i already use for my service layer in Spring 2.5).
    I extend my JUnit test class with AbstractTransactionalJUnit4SpringContextTests. This class is already annoted with @Transactional at class level. So all the test method are run in a transaction which rollback by default. However, how to stop and start new transaction ? I tried the code below but it does not insert rows as I expected :

    Code:
    @ContextConfiguration(locations = { "..." })
    public class TransactionTest extends AbstractTransactionalJUnit4SpringContextTests {
    
    	@Test
            @Rollback(false)
    	public void test() {
    		String req = "select num from table1";
    		List<Integer> nums = jdbcTemplate.queryForList(req, Integer.class);
    		for (Integer i : nums) {
    			this.transInsert(i);
    		}
    	}
    
    	// I would like a new transaction and a commit
    	@Transactional(propagation = Propagation.REQUIRES_NEW)
    	@Rollback(false)
    	public void transInsert(int i) {
    		String req = "insert into table2 values (?)";
    		jdbcTemplate.update(req, i);
    	}
    
    }
    All the rows are inserted at the same time at the end of the JUnit. It seems that the transaction which applies is the global one of the test() method and not the new one I would like in transInsert(..) method.

    So, how can I stop and start new transactions like I did in Spring 2.5 with endTransaction(..) and startNewTransaction(..) ?

    Thanks in advance for your help.

  2. #2
    Join Date
    Nov 2010
    Posts
    8

    Default

    Nobody is inspired ?

  3. #3
    Join Date
    Nov 2010
    Posts
    8

    Default

    Is anybody know the answer ?

  4. #4
    Join Date
    Nov 2009
    Location
    Poznań, Poland
    Posts
    23

    Default

    at least your rows are inserted, mine won't even insert. Persist and merge won't throw any exceptions, transaction is commited but no inserts are made. Anyone knows why?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •