Spring启动事务管理不适用于以下配置
我的spring引导应用程序中有以下配置。我从存储库层和服务层抛出RuntimeException来查看事务处理。当我从存储库层抛出RuntimeException时,事务将回滚,但是,如果我从服务层抛出RuntimeException,如下图所示,事务将不会回滚,数据将保存到数据库中。有人能帮我一下以下配置有什么问题吗Spring启动事务管理不适用于以下配置,spring,spring-boot,transactions,spring-transactions,Spring,Spring Boot,Transactions,Spring Transactions,我的spring引导应用程序中有以下配置。我从存储库层和服务层抛出RuntimeException来查看事务处理。当我从存储库层抛出RuntimeException时,事务将回滚,但是,如果我从服务层抛出RuntimeException,如下图所示,事务将不会回滚,数据将保存到数据库中。有人能帮我一下以下配置有什么问题吗 @Configuration @SpringBootApplication @EnableAsync @EnableScheduling @ComponentScan(base
@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;
}
}
下面是我的控制器类配置
@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();
// }
}
}
这是我的服务课
@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;
}
}
}
我的存储库类如下
@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 boot将回滚事务。在我的服务层中,我捕获运行时异常并抛出特定于应用程序的异常,这就是spring不回滚事务的原因。所以,在更改代码以引发最初生成的运行时异常之后,应用程序成功回滚了事务 如果我把它放在服务层,上面的代码就会运行 @事务性(rollboor=Exception.class)