這個方法是在連線是返回stream物件的。我們看這個方法裡的這段**。
findhealthyconnection方法返回健康的鏈結。我們繼續跟蹤**。最後到了findconnection方法。realconnection resultconnection = findhealthyconnection(connecttimeout, readtimeout,
writetimeout, connectionretryenabled, doextensivehealthchecks);
//省略其他**
上面的internal.instance是啥玩意呢?這個是在okhttpclient的靜態**塊中初始化的。我們看下他對應的get方法。//省略其他**
realconnection pooledconnection = internal.instance.get(connectionpool, address, this);
//省略其他**
這裡呼叫connectionpool的get方法返回乙個realconnection物件。@override public realconnection get(
connectionpool pool, address address, streamallocation streamallocation)
這樣就返回了乙個已經存在的同樣的realconnect物件。realconnection get(address address, streamallocation streamallocation)
}return null;
}
streamallocation.acquire(connection);是realconnection的allocations增加了乙個弱引用。
回到streamallocation的findconnection方法,,如果從連線池中拿到了realconnection物件,就返回。否則,繼續。生成乙個realconnection物件,並加入連線池。
現在我們看看健康檢查是怎麼回事。
這裡就是檢查socket的各項指標是夠正確,比如說socket關閉沒,輸入輸出通道關閉沒。public
boolean
ishealthy(boolean doextensivechecks)
if (framedconnection != null)
if (doextensivechecks)
return
true;
} finally
} catch (sockettimeoutexception ignored) catch (ioexception e)
}return
true;
}
如果不健康的話就呼叫nonewstream,這裡就是釋放這個realconnection所佔的資源,並且從上面操作,直到返回乙個可用的健康的realconnection。加入池裡沒有,就建立乙個新的。並將這個新的加入池中,相關**如下。
realconnection newconnection = new realconnection(selectedroute);
acquire(newconnection);
synchronized (connectionpool)
恩,預設是5個鏈結,每個鏈結的存活時長為5分鐘。鏈結池在加入第乙個鏈結物件的時候,會啟動cleanuprunnable,做清理。我們看下這個。public
connectionpool()
在cleanup函式中,會根據一定的策略移除鏈結。其中還有很多細節,我在這裡就不關注了,感興趣的同學自己去看下他的策略吧。private
final runnable cleanuprunnable = new runnable() catch (interruptedexception ignored) }}
}}
};
Mybatis自帶連線池原始碼解析
poolmaximumactiveconnections 連線池最大活躍連線數,預設為10個。poolmaximumidleconnections 連線池最大空閒連線數,預設為5個。poolmaximumcheckouttime 最大可 時間,當活躍連線數達到上限時,如果此時有連線請求,則會檢查當前...
實驗三 連線查詢
1 查詢每個學生的資訊及其選修課程的資訊,寫出sql語句和貼上執行輸出結果的截圖 2 用自身連線方式查詢表courses中和課程 c 的課時數 hour 相同的課程資訊,寫出sql語句和貼上執行輸出結果的截圖 3 用左外連線方式查詢每個學生的資訊及其選修課程的資訊,寫出sql語句和貼上執行輸出結果的...
Mosh Mysql 學習筆記(三) 連線
課程是國外mosh hamedani 的付費mysql課程。該課程乾貨滿滿,沒有廢話。並且授課方式目的性強,適合各個基礎的學生。課程在b站有搬運,大家動動手就可以找到 希望能幫助到各位同學 本章覆蓋了mysql中的所有連線方式,包括內連線,外連線,自連線,多表連線,跨資料庫連線,聯合,自然連線,交叉...