Spring 引导事务管理不适用于以下配置

Spring boot transaction management does not work for following configuration

我的 spring 引导应用程序中有以下配置。我从存储库层和服务层抛出 RuntimeException 以查看事务处理。当我从存储库层抛出 RuntimeException 时,事务正在回滚,但是,如果我从服务层抛出运行时异常,如下所示,事务不会被回滚,数据将保存到数据库中。谁能帮我看看下面的配置有什么问题吗?

@Configuration
@SpringBootApplication
@EnableAsync
@EnableScheduling
@ComponentScan(basePackages = { "com.abc.xyz.api.config", Constant.BASE_PACKAGE, com.abc.c4s.util.Constant.BASE_PACKAGE })
@EnableAutoConfiguration(exclude = { JndiConnectionFactoryAutoConfiguration.class, DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class,
        DataSourceTransactionManagerAutoConfiguration.class })
@PropertySources({
    @PropertySource(value = "classpath:jdbc.properties")
})
@EnableTransactionManagement
public class ProjectManagerApplication {

    public static ConfigurableApplicationContext context;

    private static final Logger logger = LoggerFactory.getLogger(ProjectManagerApplication.class);

    public static ConfigurableApplicationContext startServer() throws Exception {

        logger.debug("Starting server");

        SpringApplicationBuilder builder = new SpringApplicationBuilder(ProjectManagerApplication.class);
        builder.headless(false);


        context = builder.run(new String[] {});

        logger.debug("Server started - " + (context != null ? context.isRunning() : "context null, starting failed"));

        return context;

    }
}

并且,以下是我的控制器class配置

@RestController
public class RWSettingsController {

    @Autowired
    SettingsService rwSettingsService;

    @RequestMapping(value = "/api/settings", method = RequestMethod.POST)
    public void createKiosk(@RequestBody SettingsDTO settingDTO) throws ABCInternalException, ABCException {
                //try{
                    settingsService.create(settingDTO);
                //}catch (Exception e){
                //    e.printStackTrace();
               // }
    }



}

这是我的服务class。

@Service
@Transactional//(propagation = Propagation.SUPPORTS, readOnly = true)
public class SettingsService {

    @Autowired
    SettingsRepository settingsRepository;

    @Autowired
    ModelMapper modelMapper;

    private static final Logger logger = LoggerFactory.getLogger(SettingsService.class);

//    @Transactional(
//            propagation = Propagation.REQUIRED,
//            readOnly = false)
    public void create(SettingsDTO settingsDTO) throws ButterflyInternalException, ButterflyException {
        try{

            SettingsModel settingsModel = new SettingsModel ();
            settingsModel .setActive(true);
            settingsModel .setParameterDescription(settingsDTO.getParameterDescription());
            settingsModel .setParameterName(settingsDTO.getParameterName());
            settingsModel .setParameterValue(settingsDTO.getParameterValue());

            //throw new HibernateException("");

            rwsSettingsRepository.create(rwsKioskSettingsBPA);
            throw new RuntimeException("");

        }catch(ABCException e){
            logger.error(e.getMessage(),e);
            throw e;
        }catch(Exception e){

            logger.error(e);
            throw e;

        }


    }

}

而我的仓库class如下

@Repository
//@Transactional//(propagation = Propagation.SUPPORTS, readOnly = true)
public class SettingsRepository {

    @Autowired
    @Qualifier("sessionFactory")
    SessionFactory abcSessionFactory;

    @Autowired
    ABCUtilityRepository abcUtilityRepository;

    private static final Logger logger = LoggerFactory.getLogger(SettingsRepository .class);

//    @Transactional(
//            propagation = Propagation.REQUIRED,
//            readOnly = false)
    public void create(RWSKioskSettingsBPA rwsKioskSettings) throws ABCException, ABCInternalException {
        abcUtilityRepository.saveEntity(rwsKioskSettings);
//throw new RuntimeException();


    }

我找到了这种行为背后的原因。 Spring 如果应用程序抛出 运行 时间异常,启动将回滚事务。在我的服务层,我捕获了 运行time 异常并抛出应用程序特定异常,这就是 spring 不回滚事务的原因。因此,在更改代码以抛出与最初生成的相同 运行time 异常之后,应用程序成功回滚了事务。

如果我把它放在服务层,上面的代码将 运行 @Transactional(rollbackFor = Exception.class)