京东电话面:说说你对Spring事务传播属性的理解?
点击关注 ? Java面试那些事儿 文章源自JAVA秀-https://www.javaxiu.com/24319.html
收录于话题文章源自JAVA秀-https://www.javaxiu.com/24319.html
#Java面试那些事儿文章源自JAVA秀-https://www.javaxiu.com/24319.html
117个文章源自JAVA秀-https://www.javaxiu.com/24319.html
大家好,我是D哥点击关注下方公众号,Java面试资料 都在这里
文章源自JAVA秀-https://www.javaxiu.com/24319.html
来源:MoDou Blog (modouxiansheng.top)文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
笔者文笔功力尚浅,如有不妥,请慷慨指出,必定感激不尽文章源自JAVA秀-https://www.javaxiu.com/24319.html
学习东西要知行合一,如果只是知道理论而没实践过,那么掌握的也不会特别扎实,估计过几天就会忘记,接下来我们一起实践来学习Spring事务的传播属性。文章源自JAVA秀-https://www.javaxiu.com/24319.html
# 传播属性
文章源自JAVA秀-https://www.javaxiu.com/24319.html
传播属性定义的是当一个事务方法碰到另一个事务方法时的处理行为,一共有七种行为,定义如下文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
其实只看概念的话已经很直截了当了说明了每个传播性的作用,此时我们再用具体的例子演示一下每个传播性属性下的行为。文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
此次演示我们使用的是H2数据库,这个数据库是作用在内存里面的,所以对于我们演示事务效果来说正好,无需我们在进行其他的配置了,我们新建一个表。将下面语句放在schema.sql文件里面即可,SpringBoot程序在启动的时候就会自动为我们在内存里面建立这样的一个表。文章源自JAVA秀-https://www.javaxiu.com/24319.html
CREATE TABLE FOO (ID INT IDENTITY, BAR VARCHAR(64));
文章源自JAVA秀-https://www.javaxiu.com/24319.html演示之前我们会定义两个类FooService和BarService。我们使用BarService 里面的方法进行调用FooService 中的方法。文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
# 环境准备
文章源自JAVA秀-https://www.javaxiu.com/24319.html
在进行事务演示之前,其实可以分为以下几种情况,根据排列组合,我们可以得出以下八种情况文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
调用者:有无事务文章源自JAVA秀-https://www.javaxiu.com/24319.html
调用者:是否有异常文章源自JAVA秀-https://www.javaxiu.com/24319.html
被调用者:有无事务(这个是通过传播属性进行控制的)所以并不在排列组合中文章源自JAVA秀-https://www.javaxiu.com/24319.html
被调用者:是否有异常文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
# 异常类文章源自JAVA秀-https://www.javaxiu.com/24319.html
其中的RollbackException是我们自己定义的一个异常类文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html@Service
public class BarServiceImpl implements BarService{
@Autowired
private FooService fooService;
// PROPAGATION_REQUIRED演示 无事务
@Override
public void testRequiredNoTransactional() throws RollbackException {
fooService.testRequiredTransactional();
}
}
# 调用者
文章源自JAVA秀-https://www.javaxiu.com/24319.html
在BarService中定义两个方法,一个是带着事务的,一个是不带事务的文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html// 有事务
@Override
@Transactional(rollbackFor = Exception.class)
public void hasTransactional() throws RollbackException {
}
// 无事务
@Override
public void noTransactional() throws RollbackException {
}
接下来我们就根据俄上面定义的八种情况进行事务传播属性的学习。文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
PROPAGATION_REQUIRED
在此传播属性下,被调用方是否新建事务取决去调用者是否带着事务。文章源自JAVA秀-https://www.javaxiu.com/24319.html
想要了解这个传播属性的特性,其实我们演示上面八种情况的两个例子就够了文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
第一种情况我们在被调用者抛出异常的情况下,如果查询不到插入的数据,那么就说明被调用者在调用者没有事务的情况下自己新建了事务。文章源自JAVA秀-https://www.javaxiu.com/24319.html
第二种情况我们在调用者抛出异常的情况下,如果查询不到插入的数据,那么就说明被调用者在调用者有事务的情况下就加入当前事务了。文章源自JAVA秀-https://www.javaxiu.com/24319.html
我们先来看一下被调用者的类的方法例子。文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html@Service
public class FooServiceImpl implements FooService {
@Autowired
private JdbcTemplate jdbcTemplate;
// REQUIRED传播属性-被调用者有异常抛出
@Override
@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
public void testRequiredHasException() throws RollbackException {
jdbcTemplate.execute("INSERT INTO FOO (BAR) VALUES ("+Global.REQUIRED_HAS_EXCEPTION+")");
throw new RollbackException();
}
// REQUIRED传播属性-被调用者无异常抛出
@Override
@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
public void testRequiredNoException() throws RollbackException {
jdbcTemplate.execute("INSERT INTO FOO (BAR) VALUES ("+Global.REQUIRED_NO_EXCEPTION+")");
}
}
接下来我们看一下调用者方法的例子文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html@Service
public class BarServiceImpl implements BarService{
@Autowired
private FooService fooService;
// 有事务
@Override
@Transactional(rollbackFor = Exception.class)
public void hasTransactional() throws RollbackException {
// 调用者有事务,抛异常 被调用者无异常
fooService.testRequiredNoException();
throw new RollbackException();
}
// 无事务
@Override
public void noTransactional() throws RollbackException {
// 调用者无事务,不抛异常 被调用者有异常
fooService.testRequiredHasException();
}
}
此时我们在程序调用时进行查询文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.htmlString noException = Global.REQUIRED_NO_EXCEPTION;
String hasException = Global.REQUIRED_HAS_EXCEPTION;
try {
barService.noTransactional();
}catch (Exception e){
log.info("第一种情况 {}",
jdbcTemplate
.queryForObject("SELECT COUNT(*) FROM FOO WHERE BAR='"+hasException+"'", Long.class));
}
try {
barService.hasTransactional();
}catch (Exception e){
log.info("第二种情况 {}",
jdbcTemplate
.queryForObject("SELECT COUNT(*) FROM FOO WHERE BAR='"+noException+"'", Long.class));
}
查看打印出来的日志文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html2019-10-16 13:02:04.142 INFO 11869 --- [ main] c.e.t.t.TransactionApplication : 第一种情况 0
2019-10-16 13:02:04.143 INFO 11869 --- [ main] c.e.t.t.TransactionApplication : 第二种情况 0
我们看到我们都没有查到相应的数据,说明数据都回滚了。此时我们应该就理解了那句话支持当前事务,如果没有就新建事务。文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
PROPAGATION_SUPPORTS
被调用者是否有事务,完全依赖于调用者,调用者有事务则有事务,调用者没事务则没事务。文章源自JAVA秀-https://www.javaxiu.com/24319.html
接下来我们还是用上面的两个例子进行演示文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
第一种情况:被调用者抛出异常的情况下,如果仍能查询到数据,说明事务没有回滚,说明被调用者没有事务文章源自JAVA秀-https://www.javaxiu.com/24319.html
第二种情况:调用者抛出异常情况下,如果查不到数据,说明两个方法在一个事务中文章源自JAVA秀-https://www.javaxiu.com/24319.html
接下来仍然是例子演示文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
被调用者,只是将@Transactional 注解中的propagation 属性更换为了Propagation.SUPPORTS文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html// SUPPORTS传播属性-被调用者有异常抛出
@Override
@Transactional(rollbackFor = Exception.class,propagation = Propagation.SUPPORTS)
public void testSupportsHasException() throws RollbackException {
jdbcTemplate.execute("INSERT INTO FOO (BAR) VALUES ('"+Global.SUPPORTS_HAS_EXCEPTION+"')");
throw new RollbackException();
}
// SUPPORTS传播属性-被调用者无异常抛出
@Override
@Transactional(rollbackFor = Exception.class,propagation = Propagation.SUPPORTS)
public void testSupportsNoException() throws RollbackException {
jdbcTemplate.execute("INSERT INTO FOO (BAR) VALUES ('"+Global.SUPPORTS_NO_EXCEPTION+"')");
}
调用者和上面的例子调用一样,我们直接查看执行效果文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html2019-10-16 13:50:27.738 INFO 12174 --- [ main] c.e.t.t.TransactionApplication : 第一种情况 1
2019-10-16 13:50:27.741 INFO 12174 --- [ main] c.e.t.t.TransactionApplication : 第二种情况 0
我们看到了在第一种情况下查到了数据,说明在第一种情况下被调用者是没有事务的。此时我们应该就理解了这句话 支持当前事务,如果没有就不以事务的方式运行。文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
PROPAGATION_MANDATORY
文章源自JAVA秀-https://www.javaxiu.com/24319.html
依然是这两个例子进行演示文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
第一种情况:因为调用者没有事务,所以此传播属性下应该是抛异常的文章源自JAVA秀-https://www.javaxiu.com/24319.html
第二种情况:被调用者的事务和调用者事务是同样的文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
接下来是被调用者的代码例子文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html// MANDATORY传播属性-被调用者有异常抛出
@Override
@Transactional(rollbackFor = Exception.class,propagation = Propagation.MANDATORY)
public void testMandatoryHasException() throws RollbackException {
jdbcTemplate.execute("INSERT INTO FOO (BAR) VALUES ('"+Global.SUPPORTS_HAS_EXCEPTION+"')");
throw new RollbackException();
}
// MANDATORY传播属性-被调用者无异常抛出
@Override
@Transactional(rollbackFor = Exception.class,propagation = Propagation.MANDATORY)
public void testMandatoryNoException() throws RollbackException {
jdbcTemplate.execute("INSERT INTO FOO (BAR) VALUES ('"+Global.SUPPORTS_NO_EXCEPTION+"')");
}
调用者和上面的例子调用一样,我们直接查看执行效果文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html2019-10-16 13:58:39.178 ERROR 12317 --- [ main] c.e.t.t.TransactionApplication : org.springframework.transaction.IllegalTransactionStateException: No existing transaction found for transaction marked with propagation 'mandatory'
2019-10-16 13:58:39.276 INFO 12317 --- [ main] c.e.t.t.TransactionApplication : 第一种情况 0
2019-10-16 13:58:39.281 INFO 12317 --- [ main] c.e.t.t.TransactionApplication : 第二种情况 0
我们发现和我们推测一样,说明被调用者是不会自己新建事务的,此时我们应该就理解了这句话支持当前事务,如果当前没事务就抛异常。文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
PROPAGATION_REQUIRES_NEW
此传播属性下,无论调用者是否有事务,被调用者都会新建一个事务文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
第一种情况:调用者无事务,被调用者会新建事务,所以查不到数据文章源自JAVA秀-https://www.javaxiu.com/24319.html
第二种情况:调用者有事务,被调用者会新建一个事务,所以调用者抛异常影响不到被调用者,所以能查到数据文章源自JAVA秀-https://www.javaxiu.com/24319.html
接下来我们演示代码。文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
被调用者文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html// REQUIRES_NEW传播属性-被调用者有异常抛出
@Override
@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
public void testRequiresNewHasException() throws RollbackException {
jdbcTemplate.execute("INSERT INTO FOO (BAR) VALUES ('"+Global.REQUIRES_NEW_HAS_EXCEPTION+"')");
throw new RollbackException();
}
// REQUIRES_NEW传播属性-被调用者无异常抛出
@Override
@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
public void testRequiresNewNoException() throws RollbackException {
jdbcTemplate.execute("INSERT INTO FOO (BAR) VALUES ('"+Global.REQUIRES_NEW_NO_EXCEPTION+"')");
}
调用者的例子和上面的相同,我们直接来看执行情况文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html2019-10-16 16:29:20.296 INFO 15553 --- [ main] c.e.t.t.TransactionApplication : 第一种情况 0
2019-10-16 16:29:20.298 INFO 15553 --- [ main] c.e.t.t.TransactionApplication : 第二种情况 1
我们发现和我们的推论是一样的,说明调用者的事务和被调用者的事务完全无关。此时我们应该就理解这句话了无论当前是否有事务,都会新起一个事务。文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
PROPAGATION_NOT_SUPPORTED
无论调用者是否有事务,被调用者都不以事务的方法运行文章源自JAVA秀-https://www.javaxiu.com/24319.html
同样是这两个例子文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
第一种情况:被调用者都不会有事务,那么在抛异常之后就能查到相应的数据文章源自JAVA秀-https://www.javaxiu.com/24319.html
第二种情况:在调用者有事务的情况下,被调用者也会在无事务环境下运行,所以我们依然能查到数据文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
接下来验证我们的猜测文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html// NOT_SUPPORTED传播属性-被调用者有异常抛出
@Override
@Transactional(rollbackFor = Exception.class,propagation = Propagation.NOT_SUPPORTED)
public void testNotSupportHasException() throws RollbackException {
jdbcTemplate.execute("INSERT INTO FOO (BAR) VALUES ('"+Global.NOT_SUPPORTS_HAS_EXCEPTION+"')");
throw new RollbackException();
}
// NOT_SUPPORTED传播属性-被调用者无异常抛出
@Override
@Transactional(rollbackFor = Exception.class,propagation = Propagation.NOT_SUPPORTED)
public void testNotSupportNoException() throws RollbackException {
jdbcTemplate.execute("INSERT INTO FOO (BAR) VALUES ('"+Global.NOT_SUPPORTS_NO_EXCEPTION+"')");
}
然后查看执行结果文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html2019-10-16 16:38:35.065 INFO 15739 --- [ main] c.e.t.t.TransactionApplication : 第一种情况 1
2019-10-16 16:38:35.067 INFO 15739 --- [ main] c.e.t.t.TransactionApplication : 第二种情况 1
我们可以看到在最后两种情况都查到了数据,根据演示效果应该可以理解这句话了,不支持事务,如果当前存在事务,就将此事务挂起不以事务方式运行。文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
PROPAGATION_NEVER
调用者有事务,被调用者就会抛出异常文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
这个就不演示,相信大家看到这里应该都会明白在第一种情况下我们是能够查到数据的。在第二种情况下由于调用者带着事务,所以会抛异常。文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
PROPAGATION_NESTED
此传播属性下,被调用者的事务是调用者的事务的子集。文章源自JAVA秀-https://www.javaxiu.com/24319.html
我们重点说一下NESTED的传播属性的特性文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
关于什么是嵌套事务的关系,我们用下面三个例子能够进行演示。文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
第一种情况:如果查不到数据,则说明在调用者无事务情况下,被调用者会新起一个事务文章源自JAVA秀-https://www.javaxiu.com/24319.html
第二种情况:如果查不到数据,说明外层事务能够影响内层事务文章源自JAVA秀-https://www.javaxiu.com/24319.html
第三种情况:如果查到数据,说明内层事务不影响外层事务文章源自JAVA秀-https://www.javaxiu.com/24319.html
接下来我们编写具体的代码文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html// NESTED传播属性-回滚事务
@Override
@Transactional(rollbackFor = Exception.class,propagation = Propagation.NESTED)
public void testNestedHasException() throws RollbackException {
jdbcTemplate.execute("INSERT INTO FOO (BAR) VALUES ('"+Global.NESTED_HAS_EXCEPTION+"')");
// TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
throw new RollbackException();
}
// NESTED传播属性-不回滚事务
@Override
@Transactional(rollbackFor = Exception.class,propagation = Propagation.NESTED)
public void testNestedNoException() throws RollbackException {
jdbcTemplate.execute("INSERT INTO FOO (BAR) VALUES ('"+Global.NESTED_NO_EXCEPTION+"')");
}
然后接下来的调用者也会有点区别文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html@Override
@Transactional()
public void hasTransactionalNoException() throws RollbackException {
// NESTED传播属性 - 调用者有事务,不抛异常 被调用者有异常
jdbcTemplate.execute("INSERT INTO FOO (BAR) VALUES ('"+Global.NESTED_HAS_EXCEPTION_TWO+"')");
fooService.testNestedHasException();
}
然后执行效果文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html2019-10-16 18:01:06.387 INFO 17172 --- [ main] c.e.t.t.TransactionApplication : 第一种情况 0
2019-10-16 18:01:06.389 INFO 17172 --- [ main] c.e.t.t.TransactionApplication : 第二种情况 0
2019-10-16 18:01:06.390 INFO 17172 --- [ main] c.e.t.t.TransactionApplication : 第三种情况 1
可以看出来嵌套事务的本质就是外层会影响内层,内层不影响外层。而REQUIRES_NEW则是互不影响。文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
# 总结
文章源自JAVA秀-https://www.javaxiu.com/24319.html
到现在我们已经全部分析完了七种传播属性,从写这篇文章开始到结束其中也碰到过一些坑,有些是不自己实践一遍是根本不知道的,所以我还是建议读者看完这篇文章以后自己进行实践,演示各种情况,只有这样才能够烂熟于心。文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
热门推荐:文章源自JAVA秀-https://www.javaxiu.com/24319.html
这是我见过SpringBoot版的最好的ERP系统,自带进销存+财务+生产功能,拿来即用(附项目地址)文章源自JAVA秀-https://www.javaxiu.com/24319.html
这是我见过SpringBoot版的最好的CMS系统,改改就能卖钱,拿来即用(附项目地址)文章源自JAVA秀-https://www.javaxiu.com/24319.html
哇撒!这几个SpringBoot前后端分离项目(附源码),star过千,快去收藏夹吃灰吧文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
简历&面试题&视频资料获取
文章源自JAVA秀-https://www.javaxiu.com/24319.html
扫描下方二维码,回复关键字【 java】文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html
文章源自JAVA秀-https://www.javaxiu.com/24319.html

评论