2015-06-20 92 views
1

我已经阅读了很多关于spring事务的stackoverflow页面。 我的春季交易配置是春季交易只是在进入服务方式吗?

<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> 

我的服务是这样的。

@Service 
public class TestServiceImpl implements TestService { 
    @Override 
    public void testRollback() { 
      testRollbackSecondLevel(); 
    } 

    @Transactional 
    @Override 
    public void testRollbackSecondLevel() { 
     // any update sql in here 
     carCostService.testUpdate(); 

     throw new RuntimeException(); 
    } 
} 

然后我写一个测试类来测试,在我的测试代码,当我使用

// this test is not roll back, and the Transactional even not created 
@Test 
public void testTransactional() { 
    // use this function, the Transactional don't work 
    interCityService.testRollback(); 
} 

// this test is roll back successfully 
@Test 
public void testTransactionalSecondLevel() { 
    // but if I use the second level function instead of the first function, 
    // the Transactional works fine, and the data can roll back 
    interCityService.testRollbackSecondLevel(); 
} 

我调试的代码,当我使用的第一个测试,交易甚至不会被创建。第二个可以成功创建事务。

我使用sql来判断事务是否存在。

SELECT * FROM INFORMATION_SCHEMA.INNODB_TRX\G 

如果sql返回空集,那么没有事务被创建。

那么问题是什么?提前致谢。

我使用春季版4.1.2.RELEASE。

回答

0

如果你希望你所有的方法服务事务添加事务标注为类,而不是方法级

@Transactional 
@Service 
public class TestServiceImpl implements TestService { 

    @Override 
    public void testRollback() { 
     testRollbackSecondLevel(); 
    } 


@Override 
public void testRollbackSecondLevel() { 
    // any update sql in here 
    carCostService.testUpdate(); 

    throw new RuntimeException(); 
} 
} 

而且,因为他们已经解释说你已经有一笔交易不能在同一个服务中开始。所以,如果你想使第一个方法事务处理,你必须从你的服务之外调用。