spring boot mysql 事務
阿新 • • 發佈:2019-01-17
read frame llb source 手工 don clas per oot
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) { ... }
spring boot mysql 事務