我有基于類的配置,所以用于回滾事務。我使用了 jdbcTemplate。我的 bean 聲明如下: @Bean public DriverManagerDataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver"); dataSource.setUrl("jdbc:oracle:thin:@192.168.1.5:1521:DCGCDB"); dataSource.setUsername("PCA_OWNER"); dataSource.setPassword("PCA_OWNER"); return dataSource; }<!--for transaction bean-->@Beanpublic PlatformTransactionManager txManager() { return new DataSourceTransactionManager(dataSource());}因此,在我的服務類中,我聲明了 @Transactional 但它不起作用:@Servicepublic class ProcessAnexOneServiceImpl implements ProcessAnexOneService { @Autowired private SelectionCustomOfficeService selectionCustomOfficeService; @Autowired private LetterDocService letterDocService; @Autowired private LetterService letterService; @Override @Transactional public void insertProcessAnexOne(ProcessAnexOne processAnexOne, String entryBy) { BigDecimal zeroValue = new BigDecimal(0); Letter letter = new Letter(processAnexOne.getLetter().getLetterId(), processAnexOne.getLetter().getInout(), processAnexOne.getLetter().getInoutNo()); letter.setEntryBy(entryBy); //1st insert Transaction happens here BigDecimal letterNo = letterService.insertLetter(letter); //1st insert Transaction ends here System.out.println("letterNo from db is" + letterNo); //2nd insert Transaction happens here for (BigDecimal docId: processAnexOne.getDocId()) { LetterDoc letterDoc = new LetterDoc(letterNo, singledocId, null, null); letterDocService.insertLetterDoc(letterDoc, entryBy); }我有三個事務將在這個服務類中的三個不同的表上命中。所以我的問題是,當?shù)谝粋€事務完成并且第二個事務出現(xiàn)錯誤時,那么在第一個事務中沒有任何回滾發(fā)生。在那種情況下,我仍然在我的表中看到第一筆交易的數(shù)據(jù),但第二筆交易出現(xiàn)錯誤。我已經(jīng)為回滾聲明了@Transaction 注釋,并且也嘗試過 (rollbackOn=Exception.class) 但如果出現(xiàn)錯誤,它不會回滾第一個事務。
2 回答
Smart貓小萌
TA貢獻1911條經(jīng)驗 獲得超7個贊
根據(jù)Spring Doc,對于未處理的異常,會自動完成回滾。在這種情況下,您正在處理它,以便事務管理器不會看到有錯誤。
另一方面,任何類型的未經(jīng)檢查的異常(擴展 RuntimeException 的異常)都會發(fā)生回滾,而無需在 @Transactional 注釋中聲明它們。但是,需要在 @Transactional 注釋中聲明已檢查的注釋(您需要在 catch 塊中處理的注釋)。
最后,我建議您在最高級別的方法中添加回滾,即啟動事務的方法,以確保事務行為。
Qyouu
TA貢獻1786條經(jīng)驗 獲得超11個贊
問題出在 LetterDaoImpl.java 上,您在 saveLetter 方法中發(fā)現(xiàn)了任何異常。如果你捕捉到異常并且你沒有把它扔回去,你就不會得到回滾。
此外,您應該檢查注釋@Transactional,因為我很確定語法是rollbackFor() 而不是rollbackOn()。
出于調試事務的目的,我通常在調試級別為 org.springframework.jdbc.datasource.DataSourceTransactionManager 啟用日志。
添加回答
舉報
0/150
提交
取消
