上一节,主要分析了 被标记为事务的方法互相调用,事务失效的缘由,思考比较多,这一节主要说说解决方案,思考会少一些。数据库
解决方案的核心: 经过代理对象去调用方法app
1.把方法放到不一样的类:分布式
若是想学习Java工程化、高性能及分布式、深刻浅出。微服务、Spring,MyBatis,Netty源码分析的朋友能够加个人Java高级交流:854630135,群里有阿里大牛直播讲解技术,以及Java大型互联网技术的视频免费分享给你们。ide
咱们须要新建一个接口:微服务
public interface OtherService { void insertCodeMonkey(); }
再定义一个类去实现这个接口:源码分析
@Service public class OtherServiceImpl implements OtherService { @Autowired AccountMapper mapper; @Override @Transactional(propagation=Propagation.REQUIRES_NEW) public void insertCodeMonkey() { Account account = new Account(); account.setAccount("CodeMonkey"); account.setPassword("CodeMonkey"); mapper.insert(account); int a = 1 / 0; } }
修改本来的实现类:post
@Service public class AccountSerivceImpl implements AccountService { @Autowired AccountMapper mapper; @Autowired OtherService otherService; @Transactional @Override public void insertCodeBear() { try { otherService.insertCodeMonkey(); } catch (Exception e) { e.printStackTrace(); } Account account = new Account(); account.setAccount("CodeBear"); account.setPassword("CodeBear"); mapper.insert(account); } }
运行,查看数据库:性能
只有一条数据,insertCodeBear方法执行成功了,insertCodeMonkey执行失败,而且回滚了。学习
让咱们再看看控制台的日志:测试
若是想学习Java工程化、高性能及分布式、深刻浅出。微服务、Spring,MyBatis,Netty源码分析的朋友能够加个人Java高级交流:854630135,群里有阿里大牛直播讲解技术,以及Java大型互联网技术的视频免费分享给你们。
能够看到是开了两个事务去执行的。
这种解决方案最简单,不须要了解其余东西,可是这种方案须要修改代码结构,原本两个方法都是属于同一个类的,如今须要强行把它们拆开。
2. AopContext:
咱们的目标是要在实现类中获取本类的代理对象,Spring提供了Aop上下文,即:AopContext,经过AopContext,能够很方便的获取到代理对象:
@Service public class AccountSerivceImpl implements AccountService { @Autowired AccountMapper mapper; @Transactional @Override public void insertCodeBear() { try { ((AccountService)AopContext.currentProxy()).insertCodeMonkey(); } catch (Exception ex) { ex.printStackTrace(); } Account account = new Account(); account.setAccount("CodeBear"); account.setPassword("CodeBear"); mapper.insert(account); } @Transactional(propagation = Propagation.REQUIRES_NEW) @Override public void insertCodeMonkey() { Account account = new Account(); account.setAccount("CodeMonkey"); account.setPassword("CodeMonkey"); mapper.insert(account); int a = 1 / 0; } }
当写好代码,很愉快的去测试,发现居然报错了:
翻译下:不能找到当前的代理,须要设置exposeProxy属性为 true使其能够。
expose字面意思就是 暴露。也就是说 咱们须要容许暴露代理。
咱们须要在Spring Boot启动类上+一个注解:
@EnableAspectJAutoProxy(exposeProxy = true) @SpringBootApplication @MapperScan(basePackages = "com.codebear.Dao") public class SpringbootApplication { public static void main(String[] args) throws Exception { SpringApplication.run(SpringbootApplication.class, args); } }
再次运行:
确实是开启了两个事务去执行的。
再看看数据库,也没有问题。
3. ApplicationContext:
@Service public class AccountSerivceImpl implements AccountService { @Autowired AccountMapper mapper; @Autowired ApplicationContext context; AccountService service; @PostConstruct private void setSelf() { service = context.getBean(AccountService.class); } @Transactional @Override public void insertCodeBear() { try { service.insertCodeMonkey(); } catch (Exception e) { e.printStackTrace(); } Account account = new Account(); account.setAccount("CodeBear"); account.setPassword("CodeBear"); mapper.insert(account); } @Transactional(propagation = Propagation.REQUIRES_NEW) @Override public void insertCodeMonkey() { Account account = new Account(); account.setAccount("CodeMonkey"); account.setPassword("CodeMonkey"); mapper.insert(account); int a = 1 / 0; } }
验证的图片就省略了。
此方法不适用于prototype
在这里,我用了一个@PostConstruct注解,在初始化的时候,会调用被@PostConstruct标记的方法(注意,仅仅是初始化的时候,才会被调用。之后都不会被调用了,你们能够打个断点试一下),这里这么作的目的就是为了提高一下效率,不用每次都getBean。因此若是这个类是prototype的,就不适用这个方法了。若是是prototype的话,就在insertCodeBear方法中使用getBean方法吧。
上两种方法比较方便,没有新建其余的接口或者是类,可是没有很好的封装得到Aop代理对象的过程,也不是很符合 迪比特法则,也就是最少知识原则。
4. 重写BeanPostProcessor接口:
关于这个接口是作什么的,这里就不详细阐述了,简单的来讲这是Spring提供的接口,咱们能够经过重写它,在初始化Bean以前或者以后,自定义一些额外的逻辑。
首先,咱们须要定义一个接口:
public interface WeavingSelfProxy { void setSelfProxy(Object bean); }
要得到代理对象的类,须要去实现它:
@Service public class AccountSerivceImpl implements AccountService, WeavingSelfProxy { @Autowired AccountMapper mapper; AccountService service; @Override public void setSelfProxy(Object bean) { System.out.println("进入到setSelfProxy方法"); service = (AccountService) bean; } @Transactional @Override public void insertCodeBear() { try { service.insertCodeMonkey(); } catch (Exception e) { e.printStackTrace(); } Account account = new Account(); account.setAccount("CodeBear"); account.setPassword("CodeBear"); mapper.insert(account); } @Transactional(propagation = Propagation.REQUIRES_NEW) @Override public void insertCodeMonkey() { Account account = new Account(); account.setAccount("CodeMonkey"); account.setPassword("CodeMonkey"); mapper.insert(account); int a = 1 / 0; } }
重写BeanPostProcessor接口:
若是想学习Java工程化、高性能及分布式、深刻浅出。微服务、Spring,MyBatis,Netty源码分析的朋友能够加个人Java高级交流:854630135,群里有阿里大牛直播讲解技术,以及Java大型互联网技术的视频免费分享给你们。
@Component public class SetSelfProxyProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if(bean instanceof WeavingSelfProxy){ System.out.println("实现了WeavingSelfProxy接口"); ((WeavingSelfProxy) bean).setSelfProxy(bean); } return bean; } }
这样就能够了,验证的图片也省略了。
以上就是四种解决方案,能够说 各有千秋,没有哪一个好,哪一个坏,只有适不适合。
若是想学习Java工程化、高性能及分布式、深刻浅出。微服务、Spring,MyBatis,Netty源码分析的朋友能够加个人Java高级交流:854630135,群里有阿里大牛直播讲解技术,以及Java大型互联网技术的视频免费分享给你们。