thread 内 事务失效

进行批量数据更新的时候, 为了接口主线程的直接返回避免http超时, 新构建Thread进行数据的更新.

为了避免更新中出现问题导致最终的数据库不一致, 添加了事务.

由于在一个类内操作, 于是添加了 

@EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy = true) 注解, 

同时在调用的时候, 使用 AopContext.currentProxy() 获取当前的代理类, 测试报错如下:

Cannot find current proxy: Set 'exposeProxy' property on Advised to 'true' to make it

常见错误, 但是明明已经在类上进行了声明.

 

后发现是Thread的问题,导致声明式事务无法使用, 解决方案多是回到了显式调用, 如:

https://blog.csdn.net/weixin_33728268/article/details/92718629

但是这种调用徒增了很多的模板代码,后考虑设置属性 localProxy, 利用@PostConstruct,  使用获取当前类的对象,

@Autowired
ApplicationContext context;
localProxy = context.getBean(当前类.class);

获取的localProxy 默认为代理对象, 后续的调用直接使用localProxy调用即可.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章