宋子宪博客

TransactionUtils Spring事物回滚工具类

TransactionUtils

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;


@Component
public class TransactionUtils {
    @Autowired
    private DataSourceTransactionManager dataSourceTransactionManager;

    /**
     * begin事务
     *
     * @return
     */
    public TransactionStatus begin() {
        TransactionStatus transaction = dataSourceTransactionManager.
                getTransaction(new DefaultTransactionAttribute());
        return transaction;
    }

    /**
     * 提交事务
     */
    public void commit(TransactionStatus transaction) {
        dataSourceTransactionManager.commit(transaction);
    }

    /**
     * 回滚事务
     *
     * @param transaction
     */
    public void rollback(TransactionStatus transaction) {
        dataSourceTransactionManager.rollback(transaction);
    }
    
}

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »