Java Spring事务测试。嵌套事务不是';他没有后退

Java Spring事务测试。嵌套事务不是';他没有后退,java,spring,oracle,transactions,Java,Spring,Oracle,Transactions,我正在尝试使用DbUnit+Spring(4.1.1)tests+MyBatis+Oracle实现集成测试 我正在做以下工作: 测试课前:(a)公开交易; (b) 插入只读公共父数据集(适用于所有测试方法) 将测试类标记为事务类(propogation=NESTED)。因此,每个测试方法都应该在自己的事务中执行,然后回滚它。嵌套-因为我想访问父事务中“before test class”方法插入的数据(因此propogation=REQUIRED_NEW不是选项) 测试类回滚父事务后 问题是嵌套

我正在尝试使用DbUnit+Spring(4.1.1)tests+MyBatis+Oracle实现集成测试

我正在做以下工作:

  • 测试课前:(a)公开交易; (b) 插入只读公共父数据集(适用于所有测试方法)
  • 将测试类标记为事务类(propogation=NESTED)。因此,每个测试方法都应该在自己的事务中执行,然后回滚它。嵌套-因为我想访问父事务中“before test class”方法插入的数据(因此propogation=REQUIRED_NEW不是选项)
  • 测试类回滚父事务后
  • 问题是嵌套事务不会回滚。如果方法A插入数据,方法B就会看到该数据

    可能是JDBC(11.2.0.1.0)驱动程序问题(因为spring使用保存点方法)还是Oracle(11g)

    我附带了简化(一体式)测试来演示此问题:

        @RunWith(SpringJUnit4ClassRunner.class)
        @ContextConfiguration({ "/core-dao-config.xml", "/test-context.xml" })
        @TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
                TransactionTest.class, TransactionalTestExecutionListener.class})
        @Transactional(propagation = Propagation.NESTED)
    
        public class TransactionTest extends AbstractTestExecutionListener
        {
    
        public TransactionTest(){}
    
        DataSource dataSource;
    
        DataSourceTransactionManager txManager;
    
        protected void injectDependencies(final TestContext testContext) throws Exception {
    
            dataSource = testContext.getApplicationContext().getBean(DataSource.class);
            txManager = testContext.getApplicationContext().getBean(DataSourceTransactionManager.class);
    
        }
    
        @Override
        public void beforeTestClass(TestContext testContext) throws Exception {
            injectDependencies(testContext);
            final TransactionStatus transaction = txManager.getTransaction(new DefaultTransactionAttribute());
            testContext.setAttribute("parenttx", transaction);
        }    
    
        @Autowired
        SomeDao someDao;
    
        @Test
        public void testTx() {
            final DbObject domainObject = new DbObject();
            domainObject.setKey(-303L);
            someDao.add(domainObject);
    
            assertNotNull(someDao.findByKey(-303L));
        }
    
        @Test
        public void testInserted() {
            assertNull(someDao.findByKey(-303L));
        }
    
        @Override
        public void afterTestClass(TestContext testContext) throws Exception {
            final TransactionStatus parenttx =(TransactionStatus)testContext.getAttribute("parenttx");
            Validate.notNull(parenttx);
            txManager.rollback(parenttx);
        }
    }
    
    因此,当testTx方法插入testInserted记录时,就会看到它并失败。但日志显示数据被回滚:

    00:39:09.892 [main] INFO  o.s.t.c.t.TransactionContext - Began transaction (1) for test context [DefaultTestContext@74fbc266 testClass = TransactionTest, testInstance = TransactionTest@3c97f28a, testMethod = testTx@TransactionTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@1bb4611a testClass = TransactionTest, locations = '{classpath:/core-dao-config.xml, classpath:/test-context.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]]; transaction manager [org.springframework.jdbc.datasource.DataSourceTransactionManager@5f53002b]; rollback [true]
    00:39:09.896 [main] DEBUG SomeDao - Running add using Connection [oracle.jdbc.driver.LogicalConnection@34311f74]
    00:39:10.456 [main] DEBUG SomeDao - Running findOne
    00:39:10.458 [main] DEBUG SomeDao - Running find: gkey=(-303) using Connection [oracle.jdbc.driver.LogicalConnection@34311f74]
    00:39:10.752 [main] DEBUG SomeDao - <==      Total: 1
    00:39:10.968 [main] INFO  o.s.t.c.t.TransactionContext - Rolled back transaction for test context [DefaultTestContext@74fbc266 testClass = TransactionTest, testInstance = TransactionTest@3c97f28a, testMethod = testTx@TransactionTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@1bb4611a testClass = TransactionTest, locations = '{classpath:/core-dao-config.xml, classpath:/test-context.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]].
    00:39:11.184 [main] INFO  o.s.t.c.t.TransactionContext - Began transaction (1) for test context [DefaultTestContext@74fbc266 testClass = TransactionTest, testInstance = TransactionTest@3bd8bd66, testMethod = testInserted@TransactionTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@1bb4611a testClass = TransactionTest, locations = '{classpath:/core-dao-config.xml, classpath:/test-context.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]]; transaction manager [org.springframework.jdbc.datasource.DataSourceTransactionManager@5f53002b]; rollback [true]
    00:39:11.187 [main] DEBUG SomeDao - Running findOne
    00:39:11.187 [main] DEBUG SomeDao - Running find: gkey=(-303)
    00:39:11.402 [main] INFO  o.s.t.c.t.TransactionContext - Rolled back transaction for test context [DefaultTestContext@74fbc266 testClass = TransactionTest, testInstance = TransactionTest@3bd8bd66, testMethod = testInserted@TransactionTest, testException = java.lang.AssertionError: expected null, but was:<DbObject{row=BaseRow{rownumStr=row#(1), status=UNCHANGED, rowscn=D, rowid=AAAkSFAAcAAAQ2uAAX, creator=TEST, created=Sun Nov 30 14:38:19 EET 2014}, gkey=-303}>, mergedContextConfiguration = [MergedContextConfiguration@1bb4611a testClass = TransactionTest, locations = '{classpath:/core-dao-config.xml, classpath:/test-context.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]].
    
    00:39:09.892[main]信息o.s.t.c.t.TransactionContext-开始测试上下文的事务(1)[DefaultTestContext@74fbc266testClass=TransactionTest,testInstance=TransactionTest@3c97f28a,testMethod=testTx@TransactionTest,testException=[null],mergedContextConfiguration=[MergedContextConfiguration@1bb4611atestClass=TransactionTest,locations='{classpath:/core-dao-config.xml,classpath:/test context.xml}',classes='{}',contextInitializerClasses='[]',activeProfiles='{}',propertySourceLocations='{}',propertySourceProperties='{},contextLoader='org.springframework.test.context.support.DelegatingSmartContextLoader',parent=[null]];事务管理器[org.springframework.jdbc.datasource。DataSourceTransactionManager@5f53002b];回滚[真]
    00:39:09.896[main]调试SomeDao-使用连接[oracle.jdbc.driver]运行add。LogicalConnection@34311f74]
    00:39:10.456[main]调试SomeDao-运行findOne
    00:39:10.458[main]调试SomeDao-使用连接[oracle.jdbc.driver]运行find:gkey=(-303)。LogicalConnection@34311f74]
    00:39:10.752[main]调试SomeDao-