Transactor函数式编程启动事务

Transactor函数式编程启动事务

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.function.Supplier;

/**
 * @author: GeorgeWzz
 * @created: 2020/3/20.
 * @updater:
 * @description:事务处理工具
 */
@Component("myTransactor")
public class Transactor {

    private final static Logger LOGGER = LoggerFactory.getLogger(Transactor.class);

    private static Transactor instance;

    public static Transactor getInstance() {
        if (instance == null) {
            try {
                instance = (Transactor) SpringContextHolder.getBean("myTransactor");
            } catch (Exception e) {
                LOGGER.error("获取Transactor对象异常:", e);
            }
        }
        return instance;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = java.lang.Exception.class)
    public <R> R getThrowsException(SupplierThrowException<R> f) throws Exception {
        return f.get(); // 有返回值的代码块
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = java.lang.Exception.class)
    public <R> R get(Supplier<R> f) {
        return f.get(); // 有返回值的代码块
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = java.lang.Exception.class)
    public <R> R getRequiredNewThrowsException(SupplierThrowException<R> f) throws Exception {
        return f.get(); // 有返回值的代码块
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = java.lang.Exception.class)
    public <R> R getRequiredNew(Supplier<R> f) {
        return f.get(); // 有返回值的代码块
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = java.lang.Exception.class)
    public void run(Runnable f) {
        f.run(); // 无返回值的代码块
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = java.lang.Exception.class)
    public void runThrowsException(RunnableThrowException f) throws Exception {
        f.run(); // 无返回值的代码块
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = java.lang.Exception.class)
    public void runRequiredNew(Runnable f) {
        f.run(); // 无返回值的代码块
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = java.lang.Exception.class)
    public void runRequiredNewThrowsException(RunnableThrowException f) throws Exception {
        f.run(); // 无返回值的代码块
    }


}
/**
 * @author: GeorgeWzz
 * @created: 2019/6/6.
 * @updater:
 * @description:
 */
@FunctionalInterface
public interface RunnableThrowException {
    /**
     * 显示声明抛异常,用于回滚事务
     *
     * @return
     * @throws Exception
     */
    void run() throws Exception;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值