Spring/SpringBoot手动控制事务

/**
 * func description
 *
 * @author langkye
 * @date 2099/12/31
 */
@Service
public class FuncServiceImpl {
    @Resource
    private org.springframework.jdbc.datasource.DataSourceTransactionManager dataSourceTransactionManager;
    @Resource 
    private org.springframework.transaction.TransactionDefinition transactionDefinition;
    
    public void someFunc() {
        //手动开启事务
        org.springframework.transaction.TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
        try {
            //do something...
            //手动提交事务
            //dataSourceTransactionManager.commit(transactionStatus);
        } catch (Exception ex) {
            //手动回滚事务
            dataSourceTransactionManager.rollback(transactionStatus);
        }
    }
}
posted @ 2022-07-13 09:08  LANGKYE#  阅读(347)  评论(0编辑  收藏  举报