All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ideaaedi.commonmp.lite.transaction.DSManualTransactionSupport Maven / Gradle / Ivy

package com.ideaaedi.commonmp.lite.transaction;

import com.baomidou.dynamic.datasource.aop.DynamicLocalTransactionInterceptor;
import com.baomidou.dynamic.datasource.tx.DsPropagation;
import com.baomidou.dynamic.datasource.tx.TransactionalExecutor;
import com.baomidou.dynamic.datasource.tx.TransactionalInfo;
import com.baomidou.dynamic.datasource.tx.TransactionalTemplate;
import com.ideaaedi.commonds.function.NoArgConsumer;
import com.ideaaedi.commonds.function.NoArgFunction;
import com.ideaaedi.commonds.transaction.ManualTransactionSupport;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.function.Consumer;
import java.util.function.Function;

/**
 * dynamic-datasource 手动事务支持 (即:等价于{@link com.baomidou.dynamic.datasource.annotation.DSTransactional})
 * 

* 见{@link DynamicLocalTransactionInterceptor},版本:com.baomidou:dynamic-datasource-spring-boot-starter:4.2.0 * * @author JustryDeng * @since @since 2100.8.0 */ @Slf4j public class DSManualTransactionSupport implements ManualTransactionSupport { private final TransactionalTemplate transactionalTemplate; private final TransactionalInfo transactionalInfo; public DSManualTransactionSupport() { transactionalTemplate = new TransactionalTemplate(); this.transactionalInfo = new TransactionalInfo(); this.transactionalInfo.setRollbackFor(new Class[]{Exception.class}); this.transactionalInfo.setNoRollbackFor(new Class[]{}); this.transactionalInfo.setPropagation(DsPropagation.REQUIRED); } public DSManualTransactionSupport(DsPropagation propagation) { transactionalTemplate = new TransactionalTemplate(); this.transactionalInfo = new TransactionalInfo(); this.transactionalInfo.setRollbackFor(new Class[]{Exception.class}); this.transactionalInfo.setNoRollbackFor(new Class[]{}); this.transactionalInfo.setPropagation(propagation); } public DSManualTransactionSupport(Class[] rollbackFor) { transactionalTemplate = new TransactionalTemplate(); this.transactionalInfo = new TransactionalInfo(); this.transactionalInfo.setRollbackFor(rollbackFor); this.transactionalInfo.setNoRollbackFor(new Class[]{}); this.transactionalInfo.setPropagation(DsPropagation.REQUIRED); } public DSManualTransactionSupport(TransactionalInfo transactionalInfo) { transactionalTemplate = new TransactionalTemplate(); this.transactionalInfo = transactionalInfo; } @Override @SneakyThrows public R exec(Function function, P param) { log.debug(" transactionalInfo -> {}", transactionalInfo); //noinspection unchecked return (R) transactionalTemplate.execute(new TransactionalExecutor() { @Override public Object execute() { return function.apply(param); } @Override public TransactionalInfo getTransactionInfo() { return transactionalInfo; } }); } @Override @SneakyThrows public R exec(NoArgFunction function) { log.debug(" transactionalInfo -> {}", transactionalInfo); //noinspection unchecked return (R) transactionalTemplate.execute(new TransactionalExecutor() { @Override public Object execute() { return function.apply(); } @Override public TransactionalInfo getTransactionInfo() { return transactionalInfo; } }); } @Override @SneakyThrows public

void voidExec(Consumer

consumer, P param) { log.debug(" transactionalInfo -> {}", transactionalInfo); transactionalTemplate.execute(new TransactionalExecutor() { @Override public Object execute() { consumer.accept(param); return null; } @Override public TransactionalInfo getTransactionInfo() { return transactionalInfo; } }); } @Override @SneakyThrows public void voidExec(NoArgConsumer consumer) { log.debug(" transactionalInfo -> {}", transactionalInfo); transactionalTemplate.execute(new TransactionalExecutor() { @Override public Object execute() { consumer.accept(); return null; } @Override public TransactionalInfo getTransactionInfo() { return transactionalInfo; } }); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy