我已經閱讀了很多關於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。
我還不明白,如何改變我的代碼工作?將 更改爲 。它不起作用。 –
yunfan