MySQL 默认 事务自动提交. 即: 每条 insert/update/delete 语句, 不需要程序手工提交事务, 而是 MySQL 自行提交了.
如果我们想实现程序事务提交, 需要事先关闭 MySQL 的自动提交事务.
但是, 如果采用 spring 管理事务, 不需要实现关闭 MySQL 自动提交事务的, 因为, spring 会帮你关闭 MySQL 的自动提交事务.
- spring:
- <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource" />
- </bean>
- <!-- enable transaction annotation support -->
- <tx:annotation-driven transaction-manager="txManager" />
- springboot:
1), 启动类上加 @EnableTransactionManagement
- @EnableTransactionManagement
- @SpringBootApplication
- @Slf4j
- public class Application {
- public static void main(String[] args) {
- SpringApplication App = new SpringApplication(Application.class);
- App.setBannerMode(Banner.Mode.OFF);
- App.run(args);
- log.info("PortalApplication is success!");
- }
- }
service 的方法上加:@Transactional
- @Transactional(readOnly = false, rollbackFor = Exception.class)
- public AddProjectInfoDto.Resp addPrj(AddProjectInfoDto.Req req) {
- ...
- }
来源: http://www.bubuko.com/infodetail-2923550.html