温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Mybatis多线程下使用Example的案例

发布时间:2021-03-05 15:19:32 来源:亿速云 阅读:139 作者:小新 栏目:编程语言

这篇文章主要介绍了Mybatis多线程下使用Example的案例,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

前言

服务器每收到一个请求,都会从线程池中调度一个空闲线程来处理,spring整合的web时,controller和service一般都是单例的,这样导致无论你的Example标注的是单例还是多例,同一个service下的Example也只有一个,多线程访问时产生的

问题如下

问题详情

工程目录结构如下

MyService 的service()方法接收两个参数并据此查询数据库

@Servicepublic class MyService { @Autowired StudentMapper studentMapper; @Autowired StudentExample studentExample; public void service(Integer begin,Integer end){  StudentExample.Criteria criteria1 = studentExample.createCriteria();  criteria1.andAgeBetween(begin,end);  List<Student> list=studentMapper.selectByExample(studentExample);  studentExample.clear();  System.out.println(list); }}

当同时有两个请求时,两个请求的StudentExample相同

请求1如下

begin=2,end=8

请求2如下

begin=4,end=8

先放行请求1,请求1成功添加条件

再放行请求2,请求2添加失败

这时如果请求2在请求1执行查询操作前就已经执行完studentExample.clear (),请求1的查询条件就失效了

至此两个请求都没有得到正确的结果。

解决方案

可以使用ThreadLocal为每个线程配备单独的Example,为保证每次都能获取到值,这里对ThreadLocal简单扩展一下,如果当前线程没有对应的Example(多例),就从spring容器中获取一个并与这个线程绑定。

ThreadLocalExtension

public class ThreadLocalExtension<T> extends ThreadLocal<T> {   //获取ApplicationContext方法见下  @Autowired  ApplicationContext applicationContext;  public ThreadLocalExtension(){    super();  }  public T get(Class<T> example){    T bean=super.get();    if(bean==null){      super.set((T) applicationContext.getBean(example));    }    return super.get();  }}

spring泛型依赖注入

由于Example会有很多个,所以这里使用了泛型,spring4.0提供了对泛型依赖注入的支持。

首先实际类型对应的ThreadLocalExtension交由spring管理

@Repositorypublic class StudentExampleThreadLocal extends ThreadLocalExtension<StudentExample> {}

然后直接在代码中注入

@AutowiredThreadLocalExtension<StudentExample> studentExampleThreadLocal;

修改后的MyService

@Servicepublic class MyService {  @Autowired  StudentMapper studentMapper;  @Autowired  ThreadLocalExtension<StudentExample> studentExampleThreadLocal;  public void service(Integer begin,Integer end){    StudentExample studentExample = studentExampleThreadLocal.get(StudentExample.class);    StudentExample.Criteria criteria1 = studentExample.createCriteria();    criteria1.andAgeBetween(begin,end);    List<Student> list=studentMapper.selectByExample(studentExample);    studentExample.clear();    System.out.println(list);  }}

获取ApplicationContext

创建一个类实现ApplicationContextAware,并向spring容器中注入applicationContext

@Componentpublic class ApplicationContextHelper implements ApplicationContextAware {  private static ApplicationContext applicationContext;  public ApplicationContextHelper() {  }  @Bean(name="applicationContext")  public ApplicationContext getApplicationContext(){    return applicationContext;  }  @Override  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {    ApplicationContextHelper.applicationContext = applicationContext;  }  public static Object getBean(String beanName) {    return applicationContext != null?applicationContext.getBean(beanName):null;  }}

结果

至此,整个改造完成,看看效果

请求1

请求2

每个请求获取到了不同的StudentExample,也就不存在冲突的问题,并且StudentExample没有大量的创建与销毁,最多只创建了与服务器线程池中线程相同的个数,实现了重复使用

感谢你能够认真阅读完这篇文章,希望小编分享的“Mybatis多线程下使用Example的案例”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI