Spring中使用自定義ThreadLocal存儲(chǔ)導(dǎo)致的坑及解決
Spring自定義ThreadLocal存儲(chǔ)導(dǎo)致的坑
Spring 中有時(shí)候我們需要存儲(chǔ)一些和 Request 相關(guān)聯(lián)的變量,例如用戶(hù)的登陸有關(guān)信息等,它的生命周期和 Request 相同。
一個(gè)容易想到的實(shí)現(xiàn)辦法是使用ThreadLocal
public class SecurityContextHolder { private static final ThreadLocal<SecurityContext> securityContext = new ThreadLocal<SecurityContext>(); public static void set(SecurityContext context) { securityContext.set(context); } public static SecurityContext get() { return securityContext.get(); } public static void clear() { securityContext.remove(); } }
使用一個(gè)自定義的HandlerInterceptor將有關(guān)信息注入進(jìn)去
@Slf4j @Component public class RequestInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { try { SecurityContextHolder.set(retrieveRequestContext(request)); } catch (Exception ex) { log.warn("讀取請(qǐng)求信息失敗", ex); } return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception { SecurityContextHolder.clear(); }
通過(guò)這樣,我們就可以在 Controller 中直接使用這個(gè) context,很方便的獲取到有關(guān)用戶(hù)的信息
@Slf4j @RestController class Controller { public Result get() { long userId = SecurityContextHolder.get().getUserId(); // ... } }
這個(gè)方法也是很多博客中使用的。然而這個(gè)方法卻存在著一個(gè)很隱蔽的坑: HandlerInterceptor 的 postHandle 并不總是會(huì)調(diào)用。
當(dāng)Controller中出現(xiàn)Exception
@Slf4j @RestController class Controller { public Result get() { long userId = SecurityContextHolder.get().getUserId(); // ... throw new RuntimeException(); } }
或者在HandlerInterceptor的preHandle中出現(xiàn)Exception
@Slf4j @Component public class RequestInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { try { SecurityContextHolder.set(retrieveRequestContext(request)); } catch (Exception ex) { log.warn("讀取請(qǐng)求信息失敗", ex); } // ... throw new RuntimeException(); //... return true; } }
這些情況下, postHandle 并不會(huì)調(diào)用。這就導(dǎo)致了 ThreadLocal 變量不能被清理。
在平常的 Java 環(huán)境中,ThreadLocal 變量隨著 Thread 本身的銷(xiāo)毀,是可以被銷(xiāo)毀掉的。但 Spring 由于采用了線程池的設(shè)計(jì),響應(yīng)請(qǐng)求的線程可能會(huì)一直常駐,這就導(dǎo)致了變量一直不能被 GC 回收。更糟糕的是,這個(gè)沒(méi)有被正確回收的變量,由于線程池對(duì)線程的復(fù)用,可能會(huì)串到別的 Request 當(dāng)中,進(jìn)而直接導(dǎo)致代碼邏輯的錯(cuò)誤。
為了解決這個(gè)問(wèn)題,我們可以使用 Spring 自帶的 RequestContextHolder ,它背后的原理也是 ThreadLocal,不過(guò)它總會(huì)被更底層的 Servlet 的 Filter 清理掉,因此不存在泄露的問(wèn)題。
下面是一個(gè)使用RequestContextHolder重寫(xiě)的例子
public class SecurityContextHolder { private static final String SECURITY_CONTEXT_ATTRIBUTES = "SECURITY_CONTEXT"; public static void setContext(SecurityContext context) { RequestContextHolder.currentRequestAttributes().setAttribute( SECURITY_CONTEXT_ATTRIBUTES, context, RequestAttributes.SCOPE_REQUEST); } public static SecurityContext get() { return (SecurityContext)RequestContextHolder.currentRequestAttributes() .getAttribute(SECURITY_CONTEXT_ATTRIBUTES, RequestAttributes.SCOPE_REQUEST); } }
除了使用 RequestContextHolder 還可以使用 Request Scope 的 Bean,或者使用 ThreadLocalTargetSource ,原理上是類(lèi)似的。
需要時(shí)刻注意 ThreadLocal 相當(dāng)于線程內(nèi)部的 static 變量,是一個(gè)非常容易產(chǎn)生泄露的點(diǎn),因此使用 ThreadLocal 應(yīng)該額外小心。
Threadlocal可能會(huì)產(chǎn)生內(nèi)存泄露的問(wèn)題及原理
剛遇到一個(gè)關(guān)于threadlocal的內(nèi)存泄漏問(wèn)題,剛好總結(jié)一下
比較常用的這里先不提,直接提比較重要的部分
為什么會(huì)產(chǎn)生內(nèi)存泄露?
public void set(T value) { Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) map.set(this, value); else createMap(t, value); }
set方法里面,先調(diào)用到當(dāng)前線程thread,每個(gè)線程里都會(huì)有一個(gè)threadlocals成員變量,指向?qū)?yīng)的ThreadLocalMap ,然后以new出來(lái)的引用作為key,和給定的value一塊保存起來(lái)。
當(dāng)外部引用解除以后,對(duì)應(yīng)的ThreadLocal對(duì)象由于被內(nèi)部ThreadLocalMap 引用,不會(huì)GC,可能會(huì)導(dǎo)致內(nèi)存泄露。
JVM解決的辦法
static class Entry extends WeakReference<ThreadLocal<?>> { /** The value associated with this ThreadLocal. */ Object value; Entry(ThreadLocal<?> k, Object v) { super(k); value = v; } }
繼承了一個(gè)軟引用,在系統(tǒng)進(jìn)行g(shù)c的時(shí)候就可以回收
但是回收以后,key變成null,value也無(wú)法被訪問(wèn)到,還是可能存在內(nèi)存泄露。 因此一旦不用了,必須對(duì)里面的keyvalue對(duì)remove掉,否則就會(huì)有內(nèi)存泄露;而且在threadlocal源碼里面,在每次get或者set的時(shí)候會(huì)清楚里面key為value的記錄
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
新手場(chǎng)景Java線程相關(guān)問(wèn)題及解決方案
這篇文章主要介紹了新手場(chǎng)景Java線程相關(guān)問(wèn)題及解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07SpringBoot請(qǐng)求發(fā)送與信息響應(yīng)匹配實(shí)現(xiàn)方法介紹
這篇文章主要介紹了SpringBoot請(qǐng)求發(fā)送與信息響應(yīng)匹配實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-10-10springboot實(shí)現(xiàn)執(zhí)行sql語(yǔ)句打印到控制臺(tái)
這篇文章主要介紹了springboot實(shí)現(xiàn)執(zhí)行sql語(yǔ)句打印到控制臺(tái)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06mybatis多對(duì)多關(guān)聯(lián)實(shí)戰(zhàn)教程(推薦)
下面小編就為大家?guī)?lái)一篇mybatis多對(duì)多關(guān)聯(lián)實(shí)戰(zhàn)教程(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10在啟動(dòng)后臺(tái) jar包時(shí),使用指定的 application.yml操作
這篇文章主要介紹了在啟動(dòng)后臺(tái) jar包時(shí),使用指定的 application.yml操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10