Mybatis多線程下如何使用Example詳解
前言
服務(wù)器每收到一個請求,都會從線程池中調(diào)度一個空閑線程來處理,spring整合的web時,controller和service一般都是單例的,這樣導致無論你的Example標注的是單例還是多例,同一個service下的Example也只有一個,多線程訪問時產(chǎn)生的
問題如下
問題詳情
工程目錄結(jié)構(gòu)如下
MyService 的service()方法接收兩個參數(shù)并據(jù)此查詢數(shù)據(jù)庫
@Service public 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執(zhí)行查詢操作前就已經(jīng)執(zhí)行完studentExample.clear (),請求1的查詢條件就失效了
至此兩個請求都沒有得到正確的結(jié)果。
解決方案
可以使用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管理
@Repository public class StudentExampleThreadLocal extends ThreadLocalExtension<StudentExample> { }
然后直接在代碼中注入
@Autowired ThreadLocalExtension<StudentExample> studentExampleThreadLocal;
修改后的MyService
@Service public 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
創(chuàng)建一個類實現(xiàn)ApplicationContextAware,并向spring容器中注入applicationContext
@Component public 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; } }
結(jié)果
至此,整個改造完成,看看效果
請求1
請求2
每個請求獲取到了不同的StudentExample,也就不存在沖突的問題,并且StudentExample沒有大量的創(chuàng)建與銷毀,最多只創(chuàng)建了與服務(wù)器線程池中線程相同的個數(shù),實現(xiàn)了重復使用
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。
相關(guān)文章
Springcloud Stream消息驅(qū)動工具使用介紹
SpringCloud Stream由一個中間件中立的核組成,應用通過SpringCloud Stream插入的input(相當于消費者consumer,它是從隊列中接收消息的)和output(相當于生產(chǎn)者producer,它是發(fā)送消息到隊列中的)通道與外界交流2022-09-09深入分析Spring BeanDefinition的構(gòu)造元信息
Bean Definition是一個包含Bean元數(shù)據(jù)的對象,它描述了如何創(chuàng)建Bean實例、Bean屬性的值以及Bean之間的依賴關(guān)系,本文將帶大家深入分析Spring BeanDefinition的構(gòu)造元信息,需要的朋友可以參考下2024-01-01詳解Spring boot使用Redis集群替換mybatis二級緩存
本篇文章主要介紹了詳解Spring boot使用Redis集群替換mybatis二級緩存,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05Java數(shù)據(jù)結(jié)構(gòu)及算法實例:插入排序 Insertion Sort
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)及算法實例:插入排序 Insertion Sort,本文直接給出實例代碼,代碼中包含詳細注釋,需要的朋友可以參考下2015-06-06