springboot 编程式事务
在SpringBoot中使用事务很简单,这一篇不是为了讲事务,主要是讲一下一个工具类,将需要加入事务的Service层代码作为参数传递出去并返回事务的执行结果(成功和回滚)。声明式事务在SpringBoot中声明式事务最常见,就是把需要事务的方法用@Transactional标注一下就行了,这个一般用在Service层。标注后该方法就具备了事务的能力,出错了会自动回滚。在大部分场景下,该方法...
在SpringBoot中使用事务很简单,这一篇不是为了讲事务,主要是讲一下一个工具类,将需要加入事务的Service层代码作为参数传递出去并返回事务的执行结果(成功和回滚)。
声明式事务
在SpringBoot中声明式事务最常见,就是把需要事务的方法用@Transactional标注一下就行了,这个一般用在Service层。标注后该方法就具备了事务的能力,出错了会自动回滚。
在大部分场景下,该方法已经够用了。
编程式事务
在有些场景下,我们需要获取事务的状态,是执行成功了还是失败回滚了,那么使用声明式事务就不够用了,需要编程式事务。
在SpringBoot中,可以使用两种编程式事务。
TransactionManager
TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());
try {
for (int i = 0; i < 10; i++) {
Post post = new Post();
if (i == 5) {
post.setContent("dddddddddddddddddddddddddddddddddddddddddddd");
} else
post.setContent("post" + i);
post.setWeight(i);
postService.save(post);
}
transactionManager.commit(status);
} catch (Exception e) {
transactionManager.rollback(status);
e.printStackTrace();
}
可以把事务结果同步返回给调用端,出异常返回false,成功返回true。
我们就基于这种方法来做一个工具类。这个工具类作用是接收一个Service层需要被事务包围的方法为参数,然后给调用端返回事务结果,供调用端根据结果做相应的处理。
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import java.util.function.Consumer;
/**
* spring 编程式事务工具类
*
* @author zhaoyang10
* @date 2020/10/26
*/
@Slf4j
@Component
public class TransactionUtil {
@Autowired
private PlatformTransactionManager transactionManager;
public <T> boolean transactional(Consumer<T> consumer) {
TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());
try {
consumer.accept(null);
transactionManager.commit(status);
return true;
} catch (Exception e) {
transactionManager.rollback(status);
log.error("编程式事务业务异常回滚", e);
return false;
}
}
}
该类通过接收一个函数型参数,返回该函数的事务执行结果。由于我们仅仅是为了执行Service代码,并不需要改变代码的值,所有consumer.accept(null)即可。
Service举例:
@Service
public class TestService {
/**
* 此处不需要事务,由TransactionUtil控制事务
*/
public void doSome(int i) {
System.out.println("我是Service层" + i);
}
}
Controller中就可以使用
// 获取事务的执行结果
boolean result = transactionUtil.transactional(s -> testService.doSome(1))
该工具是需要获取事务执行结果的封装。
TransactionTemplate
借助(TransactionCallback)执行事务管理,带有返回值:
public Object getObject(String str) {
/*
* 执行带有返回值<Object>的事务管理
*/
transactionTemplate.execute(new TransactionCallback<Object>() {
@Override
public Object doInTransaction(TransactionStatus transactionStatus) {
try {
...
//....... 业务代码
return new Object();
} catch (Exception e) {
//回滚
transactionStatus.setRollbackOnly();
return null;
}
}
});
}
借助(TransactionCallbackWithoutResult)执行事务管理,无返回值:
public void update(String str) {
/*
* 执行无返回值的事务管理
*/
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
try {
// .... 业务代码
} catch (Exception e){
//回滚
transactionStatus.setRollbackOnly();
}
}
});
}
参考文档:
分布式事务技巧之使用编程式事务
更多推荐
所有评论(0)