主頁 >  其他 > Mysql Connector/J 原始碼分析(LoadBalance)

Mysql Connector/J 原始碼分析(LoadBalance)

2020-10-04 16:38:57 其他

文章目錄

  • 前言
  • 名詞定義
  • 一、Loadbalance的邏輯結構
  • 二、例外處理機制
    • 1.構造階段
    • 2.使用階段
    • 小結
  • 三、autoReconnect選項
    • 作用分析
  • 四、何時重建連接
  • 五、實用性分析
    • 小結
  • 六、總結

前言

本文討論Connector/J 的loadbalance模塊,我們先觀察整個模塊的大概邏輯結構和每一個大組件的作用,然后在代碼層面分析對于例外的控制,這里會有兩個“區分”:1)區分構造連接程序和使用連接程序;2)區分通訊例外和資料例外,最終分析此模式的實用性,

本次分析的版本為5.1.46,若通過maven下載,可添加以下依賴:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.46</version>
</dependency>

我們獲取連接的例子如下:

 Connection conn = null;
    URL =“jdbc:mysql:loadbalance://ip1:port1,ip2:port2,ip3:port3/dbname”;
    try{
        // 注冊 JDBC 驅動
        Class.forName("com.mysql.jdbc.Driver");

        // 打開鏈接
        conn = DriverManager.getConnection(DB_URL,USER,PASS);
    ....

名詞定義

Mysql:Mysql資料庫管理軟體

Mysql服務器:安裝了Mysql資料庫管理軟體的服務器

呼叫方:呼叫DriverManager#getConnection命令獲取連接的一方

一、Loadbalance的邏輯結構

Loadbalance模塊的UML類圖如下:
在這里插入圖片描述主要組件功能如下:

  • MysqlIO:負責與Mysql服務器建立tcp鏈接,
  • ConnectionImpl、JDBC4Connection:通過MysqlIO控制與Mysql服務器間的連接,并設定和記錄各種連接時間
  • MultiHostMySQLConnection、LoadBalancedMySQLConnection、JDBC4LoadBalancedMySQLConnection:通過代理物件LoadBalancedConnectionProxy獲取JDBC4Connection物件,并呼叫JDBC4Connection物件對應的介面方法(從JDBC4MySQLConnection到java.sql.Connection宣告的方法),或者呼叫LoadBalancedConnectionProxy對應的方法,
  • BalanceStrategy介面以及其實作類,以各自的演算法回傳合適的連接物件JDBC4Connection,
  • MultiHostConnectionProxy:作為各種動態類的父類,實作了各種動態類的公共方法,最常見的就是回傳當前連接物件給到MultiHostMySQLConnection及其子類,它還是InvocationHandler介面的直接實作類,它多載了invoke方法,并宣告了由其子類實作的虛方法invokeMore,invoke方法的實作使用了模板方法這種設計模式,通過invoke方法和類的invokeMore方法,一起實作了代理模式,即在被代理方法執行之前和之后都添加了一些行為,
  • LoadBalancedConnectionProxy作為MultiHostMySQLConnection的子類多載了invokeMore方法,在被代理方法執行后,會通過策略物件計算和更新當前連接物件,以供下次使用,

組件呼叫順序:

  • 構造連接時各組件呼叫順序:
<style>#mermaid-svg-FLrWbPPP5zhvZTGK .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-FLrWbPPP5zhvZTGK .label text{fill:#333}#mermaid-svg-FLrWbPPP5zhvZTGK .node rect,#mermaid-svg-FLrWbPPP5zhvZTGK .node circle,#mermaid-svg-FLrWbPPP5zhvZTGK .node ellipse,#mermaid-svg-FLrWbPPP5zhvZTGK .node polygon,#mermaid-svg-FLrWbPPP5zhvZTGK .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-FLrWbPPP5zhvZTGK .node .label{text-align:center;fill:#333}#mermaid-svg-FLrWbPPP5zhvZTGK .node.clickable{cursor:pointer}#mermaid-svg-FLrWbPPP5zhvZTGK .arrowheadPath{fill:#333}#mermaid-svg-FLrWbPPP5zhvZTGK .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-FLrWbPPP5zhvZTGK .flowchart-link{stroke:#333;fill:none}#mermaid-svg-FLrWbPPP5zhvZTGK .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-FLrWbPPP5zhvZTGK .edgeLabel rect{opacity:0.9}#mermaid-svg-FLrWbPPP5zhvZTGK .edgeLabel span{color:#333}#mermaid-svg-FLrWbPPP5zhvZTGK .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-FLrWbPPP5zhvZTGK .cluster text{fill:#333}#mermaid-svg-FLrWbPPP5zhvZTGK div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-FLrWbPPP5zhvZTGK .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-FLrWbPPP5zhvZTGK text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-FLrWbPPP5zhvZTGK .actor-line{stroke:grey}#mermaid-svg-FLrWbPPP5zhvZTGK .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-FLrWbPPP5zhvZTGK .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-FLrWbPPP5zhvZTGK #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-FLrWbPPP5zhvZTGK .sequenceNumber{fill:#fff}#mermaid-svg-FLrWbPPP5zhvZTGK #sequencenumber{fill:#333}#mermaid-svg-FLrWbPPP5zhvZTGK #crosshead path{fill:#333;stroke:#333}#mermaid-svg-FLrWbPPP5zhvZTGK .messageText{fill:#333;stroke:#333}#mermaid-svg-FLrWbPPP5zhvZTGK .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-FLrWbPPP5zhvZTGK .labelText,#mermaid-svg-FLrWbPPP5zhvZTGK .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-FLrWbPPP5zhvZTGK .loopText,#mermaid-svg-FLrWbPPP5zhvZTGK .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-FLrWbPPP5zhvZTGK .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-FLrWbPPP5zhvZTGK .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-FLrWbPPP5zhvZTGK .noteText,#mermaid-svg-FLrWbPPP5zhvZTGK .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-FLrWbPPP5zhvZTGK .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-FLrWbPPP5zhvZTGK .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-FLrWbPPP5zhvZTGK .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-FLrWbPPP5zhvZTGK .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-FLrWbPPP5zhvZTGK .section{stroke:none;opacity:0.2}#mermaid-svg-FLrWbPPP5zhvZTGK .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-FLrWbPPP5zhvZTGK .section2{fill:#fff400}#mermaid-svg-FLrWbPPP5zhvZTGK .section1,#mermaid-svg-FLrWbPPP5zhvZTGK .section3{fill:#fff;opacity:0.2}#mermaid-svg-FLrWbPPP5zhvZTGK .sectionTitle0{fill:#333}#mermaid-svg-FLrWbPPP5zhvZTGK .sectionTitle1{fill:#333}#mermaid-svg-FLrWbPPP5zhvZTGK .sectionTitle2{fill:#333}#mermaid-svg-FLrWbPPP5zhvZTGK .sectionTitle3{fill:#333}#mermaid-svg-FLrWbPPP5zhvZTGK .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-FLrWbPPP5zhvZTGK .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-FLrWbPPP5zhvZTGK .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-FLrWbPPP5zhvZTGK .grid path{stroke-width:0}#mermaid-svg-FLrWbPPP5zhvZTGK .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-FLrWbPPP5zhvZTGK .task{stroke-width:2}#mermaid-svg-FLrWbPPP5zhvZTGK .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-FLrWbPPP5zhvZTGK .taskText:not([font-size]){font-size:11px}#mermaid-svg-FLrWbPPP5zhvZTGK .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-FLrWbPPP5zhvZTGK .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-FLrWbPPP5zhvZTGK .task.clickable{cursor:pointer}#mermaid-svg-FLrWbPPP5zhvZTGK .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-FLrWbPPP5zhvZTGK .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-FLrWbPPP5zhvZTGK .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-FLrWbPPP5zhvZTGK .taskText0,#mermaid-svg-FLrWbPPP5zhvZTGK .taskText1,#mermaid-svg-FLrWbPPP5zhvZTGK .taskText2,#mermaid-svg-FLrWbPPP5zhvZTGK .taskText3{fill:#fff}#mermaid-svg-FLrWbPPP5zhvZTGK .task0,#mermaid-svg-FLrWbPPP5zhvZTGK .task1,#mermaid-svg-FLrWbPPP5zhvZTGK .task2,#mermaid-svg-FLrWbPPP5zhvZTGK .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-FLrWbPPP5zhvZTGK .taskTextOutside0,#mermaid-svg-FLrWbPPP5zhvZTGK .taskTextOutside2{fill:#000}#mermaid-svg-FLrWbPPP5zhvZTGK .taskTextOutside1,#mermaid-svg-FLrWbPPP5zhvZTGK .taskTextOutside3{fill:#000}#mermaid-svg-FLrWbPPP5zhvZTGK .active0,#mermaid-svg-FLrWbPPP5zhvZTGK .active1,#mermaid-svg-FLrWbPPP5zhvZTGK .active2,#mermaid-svg-FLrWbPPP5zhvZTGK .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-FLrWbPPP5zhvZTGK .activeText0,#mermaid-svg-FLrWbPPP5zhvZTGK .activeText1,#mermaid-svg-FLrWbPPP5zhvZTGK .activeText2,#mermaid-svg-FLrWbPPP5zhvZTGK .activeText3{fill:#000 !important}#mermaid-svg-FLrWbPPP5zhvZTGK .done0,#mermaid-svg-FLrWbPPP5zhvZTGK .done1,#mermaid-svg-FLrWbPPP5zhvZTGK .done2,#mermaid-svg-FLrWbPPP5zhvZTGK .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-FLrWbPPP5zhvZTGK .doneText0,#mermaid-svg-FLrWbPPP5zhvZTGK .doneText1,#mermaid-svg-FLrWbPPP5zhvZTGK .doneText2,#mermaid-svg-FLrWbPPP5zhvZTGK .doneText3{fill:#000 !important}#mermaid-svg-FLrWbPPP5zhvZTGK .crit0,#mermaid-svg-FLrWbPPP5zhvZTGK .crit1,#mermaid-svg-FLrWbPPP5zhvZTGK .crit2,#mermaid-svg-FLrWbPPP5zhvZTGK .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-FLrWbPPP5zhvZTGK .activeCrit0,#mermaid-svg-FLrWbPPP5zhvZTGK .activeCrit1,#mermaid-svg-FLrWbPPP5zhvZTGK .activeCrit2,#mermaid-svg-FLrWbPPP5zhvZTGK .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-FLrWbPPP5zhvZTGK .doneCrit0,#mermaid-svg-FLrWbPPP5zhvZTGK .doneCrit1,#mermaid-svg-FLrWbPPP5zhvZTGK .doneCrit2,#mermaid-svg-FLrWbPPP5zhvZTGK .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-FLrWbPPP5zhvZTGK .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-FLrWbPPP5zhvZTGK .milestoneText{font-style:italic}#mermaid-svg-FLrWbPPP5zhvZTGK .doneCritText0,#mermaid-svg-FLrWbPPP5zhvZTGK .doneCritText1,#mermaid-svg-FLrWbPPP5zhvZTGK .doneCritText2,#mermaid-svg-FLrWbPPP5zhvZTGK .doneCritText3{fill:#000 !important}#mermaid-svg-FLrWbPPP5zhvZTGK .activeCritText0,#mermaid-svg-FLrWbPPP5zhvZTGK .activeCritText1,#mermaid-svg-FLrWbPPP5zhvZTGK .activeCritText2,#mermaid-svg-FLrWbPPP5zhvZTGK .activeCritText3{fill:#000 !important}#mermaid-svg-FLrWbPPP5zhvZTGK .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-FLrWbPPP5zhvZTGK g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-FLrWbPPP5zhvZTGK g.classGroup text .title{font-weight:bolder}#mermaid-svg-FLrWbPPP5zhvZTGK g.clickable{cursor:pointer}#mermaid-svg-FLrWbPPP5zhvZTGK g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-FLrWbPPP5zhvZTGK g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-FLrWbPPP5zhvZTGK .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-FLrWbPPP5zhvZTGK .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-FLrWbPPP5zhvZTGK .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-FLrWbPPP5zhvZTGK .dashed-line{stroke-dasharray:3}#mermaid-svg-FLrWbPPP5zhvZTGK #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-FLrWbPPP5zhvZTGK #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-FLrWbPPP5zhvZTGK #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-FLrWbPPP5zhvZTGK #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-FLrWbPPP5zhvZTGK #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-FLrWbPPP5zhvZTGK #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-FLrWbPPP5zhvZTGK #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-FLrWbPPP5zhvZTGK #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-FLrWbPPP5zhvZTGK .commit-id,#mermaid-svg-FLrWbPPP5zhvZTGK .commit-msg,#mermaid-svg-FLrWbPPP5zhvZTGK .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-FLrWbPPP5zhvZTGK .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-FLrWbPPP5zhvZTGK .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-FLrWbPPP5zhvZTGK g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-FLrWbPPP5zhvZTGK g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-FLrWbPPP5zhvZTGK g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-FLrWbPPP5zhvZTGK g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-FLrWbPPP5zhvZTGK g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-FLrWbPPP5zhvZTGK g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-FLrWbPPP5zhvZTGK .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-FLrWbPPP5zhvZTGK .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-FLrWbPPP5zhvZTGK .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-FLrWbPPP5zhvZTGK .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-FLrWbPPP5zhvZTGK .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-FLrWbPPP5zhvZTGK .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-FLrWbPPP5zhvZTGK .edgeLabel text{fill:#333}#mermaid-svg-FLrWbPPP5zhvZTGK .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-FLrWbPPP5zhvZTGK .node circle.state-start{fill:black;stroke:black}#mermaid-svg-FLrWbPPP5zhvZTGK .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-FLrWbPPP5zhvZTGK #statediagram-barbEnd{fill:#9370db}#mermaid-svg-FLrWbPPP5zhvZTGK .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-FLrWbPPP5zhvZTGK .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-FLrWbPPP5zhvZTGK .statediagram-state .divider{stroke:#9370db}#mermaid-svg-FLrWbPPP5zhvZTGK .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-FLrWbPPP5zhvZTGK .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-FLrWbPPP5zhvZTGK .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-FLrWbPPP5zhvZTGK .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-FLrWbPPP5zhvZTGK .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-FLrWbPPP5zhvZTGK .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-FLrWbPPP5zhvZTGK .note-edge{stroke-dasharray:5}#mermaid-svg-FLrWbPPP5zhvZTGK .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-FLrWbPPP5zhvZTGK .error-icon{fill:#522}#mermaid-svg-FLrWbPPP5zhvZTGK .error-text{fill:#522;stroke:#522}#mermaid-svg-FLrWbPPP5zhvZTGK .edge-thickness-normal{stroke-width:2px}#mermaid-svg-FLrWbPPP5zhvZTGK .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-FLrWbPPP5zhvZTGK .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-FLrWbPPP5zhvZTGK .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-FLrWbPPP5zhvZTGK .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-FLrWbPPP5zhvZTGK .marker{fill:#333}#mermaid-svg-FLrWbPPP5zhvZTGK .marker.cross{stroke:#333} :root { --mermaid-font-family: "trebuchet ms", verdana, arial;}</style> <style>#mermaid-svg-FLrWbPPP5zhvZTGK { color: rgba(0, 0, 0, 0.75); font: ; }</style> 呼叫者 Driver DriverManager LoadBalancedConnectionProxy JDBC4LoadBalancedMySQLConnection BalanceStrategy JDBC4Connection MysqlIO 呼叫者 Driver DriverManager LoadBalancedConnectionProxy JDBC4LoadBalancedMySQLConnection BalanceStrategy JDBC4Connection MysqlIO
  • 使用連接時各組件呼叫順序:
<style>#mermaid-svg-nYc5q2JNrSA8zQbq .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-nYc5q2JNrSA8zQbq .label text{fill:#333}#mermaid-svg-nYc5q2JNrSA8zQbq .node rect,#mermaid-svg-nYc5q2JNrSA8zQbq .node circle,#mermaid-svg-nYc5q2JNrSA8zQbq .node ellipse,#mermaid-svg-nYc5q2JNrSA8zQbq .node polygon,#mermaid-svg-nYc5q2JNrSA8zQbq .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-nYc5q2JNrSA8zQbq .node .label{text-align:center;fill:#333}#mermaid-svg-nYc5q2JNrSA8zQbq .node.clickable{cursor:pointer}#mermaid-svg-nYc5q2JNrSA8zQbq .arrowheadPath{fill:#333}#mermaid-svg-nYc5q2JNrSA8zQbq .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-nYc5q2JNrSA8zQbq .flowchart-link{stroke:#333;fill:none}#mermaid-svg-nYc5q2JNrSA8zQbq .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-nYc5q2JNrSA8zQbq .edgeLabel rect{opacity:0.9}#mermaid-svg-nYc5q2JNrSA8zQbq .edgeLabel span{color:#333}#mermaid-svg-nYc5q2JNrSA8zQbq .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-nYc5q2JNrSA8zQbq .cluster text{fill:#333}#mermaid-svg-nYc5q2JNrSA8zQbq div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-nYc5q2JNrSA8zQbq .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-nYc5q2JNrSA8zQbq text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-nYc5q2JNrSA8zQbq .actor-line{stroke:grey}#mermaid-svg-nYc5q2JNrSA8zQbq .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-nYc5q2JNrSA8zQbq .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-nYc5q2JNrSA8zQbq #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-nYc5q2JNrSA8zQbq .sequenceNumber{fill:#fff}#mermaid-svg-nYc5q2JNrSA8zQbq #sequencenumber{fill:#333}#mermaid-svg-nYc5q2JNrSA8zQbq #crosshead path{fill:#333;stroke:#333}#mermaid-svg-nYc5q2JNrSA8zQbq .messageText{fill:#333;stroke:#333}#mermaid-svg-nYc5q2JNrSA8zQbq .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-nYc5q2JNrSA8zQbq .labelText,#mermaid-svg-nYc5q2JNrSA8zQbq .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-nYc5q2JNrSA8zQbq .loopText,#mermaid-svg-nYc5q2JNrSA8zQbq .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-nYc5q2JNrSA8zQbq .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-nYc5q2JNrSA8zQbq .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-nYc5q2JNrSA8zQbq .noteText,#mermaid-svg-nYc5q2JNrSA8zQbq .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-nYc5q2JNrSA8zQbq .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-nYc5q2JNrSA8zQbq .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-nYc5q2JNrSA8zQbq .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-nYc5q2JNrSA8zQbq .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-nYc5q2JNrSA8zQbq .section{stroke:none;opacity:0.2}#mermaid-svg-nYc5q2JNrSA8zQbq .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-nYc5q2JNrSA8zQbq .section2{fill:#fff400}#mermaid-svg-nYc5q2JNrSA8zQbq .section1,#mermaid-svg-nYc5q2JNrSA8zQbq .section3{fill:#fff;opacity:0.2}#mermaid-svg-nYc5q2JNrSA8zQbq .sectionTitle0{fill:#333}#mermaid-svg-nYc5q2JNrSA8zQbq .sectionTitle1{fill:#333}#mermaid-svg-nYc5q2JNrSA8zQbq .sectionTitle2{fill:#333}#mermaid-svg-nYc5q2JNrSA8zQbq .sectionTitle3{fill:#333}#mermaid-svg-nYc5q2JNrSA8zQbq .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-nYc5q2JNrSA8zQbq .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-nYc5q2JNrSA8zQbq .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-nYc5q2JNrSA8zQbq .grid path{stroke-width:0}#mermaid-svg-nYc5q2JNrSA8zQbq .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-nYc5q2JNrSA8zQbq .task{stroke-width:2}#mermaid-svg-nYc5q2JNrSA8zQbq .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-nYc5q2JNrSA8zQbq .taskText:not([font-size]){font-size:11px}#mermaid-svg-nYc5q2JNrSA8zQbq .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-nYc5q2JNrSA8zQbq .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-nYc5q2JNrSA8zQbq .task.clickable{cursor:pointer}#mermaid-svg-nYc5q2JNrSA8zQbq .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-nYc5q2JNrSA8zQbq .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-nYc5q2JNrSA8zQbq .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-nYc5q2JNrSA8zQbq .taskText0,#mermaid-svg-nYc5q2JNrSA8zQbq .taskText1,#mermaid-svg-nYc5q2JNrSA8zQbq .taskText2,#mermaid-svg-nYc5q2JNrSA8zQbq .taskText3{fill:#fff}#mermaid-svg-nYc5q2JNrSA8zQbq .task0,#mermaid-svg-nYc5q2JNrSA8zQbq .task1,#mermaid-svg-nYc5q2JNrSA8zQbq .task2,#mermaid-svg-nYc5q2JNrSA8zQbq .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-nYc5q2JNrSA8zQbq .taskTextOutside0,#mermaid-svg-nYc5q2JNrSA8zQbq .taskTextOutside2{fill:#000}#mermaid-svg-nYc5q2JNrSA8zQbq .taskTextOutside1,#mermaid-svg-nYc5q2JNrSA8zQbq .taskTextOutside3{fill:#000}#mermaid-svg-nYc5q2JNrSA8zQbq .active0,#mermaid-svg-nYc5q2JNrSA8zQbq .active1,#mermaid-svg-nYc5q2JNrSA8zQbq .active2,#mermaid-svg-nYc5q2JNrSA8zQbq .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-nYc5q2JNrSA8zQbq .activeText0,#mermaid-svg-nYc5q2JNrSA8zQbq .activeText1,#mermaid-svg-nYc5q2JNrSA8zQbq .activeText2,#mermaid-svg-nYc5q2JNrSA8zQbq .activeText3{fill:#000 !important}#mermaid-svg-nYc5q2JNrSA8zQbq .done0,#mermaid-svg-nYc5q2JNrSA8zQbq .done1,#mermaid-svg-nYc5q2JNrSA8zQbq .done2,#mermaid-svg-nYc5q2JNrSA8zQbq .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-nYc5q2JNrSA8zQbq .doneText0,#mermaid-svg-nYc5q2JNrSA8zQbq .doneText1,#mermaid-svg-nYc5q2JNrSA8zQbq .doneText2,#mermaid-svg-nYc5q2JNrSA8zQbq .doneText3{fill:#000 !important}#mermaid-svg-nYc5q2JNrSA8zQbq .crit0,#mermaid-svg-nYc5q2JNrSA8zQbq .crit1,#mermaid-svg-nYc5q2JNrSA8zQbq .crit2,#mermaid-svg-nYc5q2JNrSA8zQbq .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-nYc5q2JNrSA8zQbq .activeCrit0,#mermaid-svg-nYc5q2JNrSA8zQbq .activeCrit1,#mermaid-svg-nYc5q2JNrSA8zQbq .activeCrit2,#mermaid-svg-nYc5q2JNrSA8zQbq .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-nYc5q2JNrSA8zQbq .doneCrit0,#mermaid-svg-nYc5q2JNrSA8zQbq .doneCrit1,#mermaid-svg-nYc5q2JNrSA8zQbq .doneCrit2,#mermaid-svg-nYc5q2JNrSA8zQbq .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-nYc5q2JNrSA8zQbq .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-nYc5q2JNrSA8zQbq .milestoneText{font-style:italic}#mermaid-svg-nYc5q2JNrSA8zQbq .doneCritText0,#mermaid-svg-nYc5q2JNrSA8zQbq .doneCritText1,#mermaid-svg-nYc5q2JNrSA8zQbq .doneCritText2,#mermaid-svg-nYc5q2JNrSA8zQbq .doneCritText3{fill:#000 !important}#mermaid-svg-nYc5q2JNrSA8zQbq .activeCritText0,#mermaid-svg-nYc5q2JNrSA8zQbq .activeCritText1,#mermaid-svg-nYc5q2JNrSA8zQbq .activeCritText2,#mermaid-svg-nYc5q2JNrSA8zQbq .activeCritText3{fill:#000 !important}#mermaid-svg-nYc5q2JNrSA8zQbq .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-nYc5q2JNrSA8zQbq g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-nYc5q2JNrSA8zQbq g.classGroup text .title{font-weight:bolder}#mermaid-svg-nYc5q2JNrSA8zQbq g.clickable{cursor:pointer}#mermaid-svg-nYc5q2JNrSA8zQbq g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-nYc5q2JNrSA8zQbq g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-nYc5q2JNrSA8zQbq .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-nYc5q2JNrSA8zQbq .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-nYc5q2JNrSA8zQbq .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-nYc5q2JNrSA8zQbq .dashed-line{stroke-dasharray:3}#mermaid-svg-nYc5q2JNrSA8zQbq #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-nYc5q2JNrSA8zQbq #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-nYc5q2JNrSA8zQbq #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-nYc5q2JNrSA8zQbq #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-nYc5q2JNrSA8zQbq #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-nYc5q2JNrSA8zQbq #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-nYc5q2JNrSA8zQbq #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-nYc5q2JNrSA8zQbq #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-nYc5q2JNrSA8zQbq .commit-id,#mermaid-svg-nYc5q2JNrSA8zQbq .commit-msg,#mermaid-svg-nYc5q2JNrSA8zQbq .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-nYc5q2JNrSA8zQbq .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-nYc5q2JNrSA8zQbq .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-nYc5q2JNrSA8zQbq g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-nYc5q2JNrSA8zQbq g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-nYc5q2JNrSA8zQbq g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-nYc5q2JNrSA8zQbq g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-nYc5q2JNrSA8zQbq g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-nYc5q2JNrSA8zQbq g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-nYc5q2JNrSA8zQbq .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-nYc5q2JNrSA8zQbq .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-nYc5q2JNrSA8zQbq .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-nYc5q2JNrSA8zQbq .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-nYc5q2JNrSA8zQbq .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-nYc5q2JNrSA8zQbq .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-nYc5q2JNrSA8zQbq .edgeLabel text{fill:#333}#mermaid-svg-nYc5q2JNrSA8zQbq .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-nYc5q2JNrSA8zQbq .node circle.state-start{fill:black;stroke:black}#mermaid-svg-nYc5q2JNrSA8zQbq .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-nYc5q2JNrSA8zQbq #statediagram-barbEnd{fill:#9370db}#mermaid-svg-nYc5q2JNrSA8zQbq .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-nYc5q2JNrSA8zQbq .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-nYc5q2JNrSA8zQbq .statediagram-state .divider{stroke:#9370db}#mermaid-svg-nYc5q2JNrSA8zQbq .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-nYc5q2JNrSA8zQbq .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-nYc5q2JNrSA8zQbq .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-nYc5q2JNrSA8zQbq .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-nYc5q2JNrSA8zQbq .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-nYc5q2JNrSA8zQbq .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-nYc5q2JNrSA8zQbq .note-edge{stroke-dasharray:5}#mermaid-svg-nYc5q2JNrSA8zQbq .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-nYc5q2JNrSA8zQbq .error-icon{fill:#522}#mermaid-svg-nYc5q2JNrSA8zQbq .error-text{fill:#522;stroke:#522}#mermaid-svg-nYc5q2JNrSA8zQbq .edge-thickness-normal{stroke-width:2px}#mermaid-svg-nYc5q2JNrSA8zQbq .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-nYc5q2JNrSA8zQbq .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-nYc5q2JNrSA8zQbq .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-nYc5q2JNrSA8zQbq .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-nYc5q2JNrSA8zQbq .marker{fill:#333}#mermaid-svg-nYc5q2JNrSA8zQbq .marker.cross{stroke:#333} :root { --mermaid-font-family: "trebuchet ms", verdana, arial;}</style> <style>#mermaid-svg-nYc5q2JNrSA8zQbq { color: rgba(0, 0, 0, 0.75); font: ; }</style> 呼叫者 動態連接 LoadBalancedConnectionProxy JDBC4LoadBalancedMySQLConnection JDBC4Connection MysqlIO BalanceStrategy 更換連接 呼叫者 動態連接 LoadBalancedConnectionProxy JDBC4LoadBalancedMySQLConnection JDBC4Connection MysqlIO BalanceStrategy

二、例外處理機制

1.構造階段

在我們的例里,url以jdbc:mysql:loadbalance:作為前綴,當我們使用DriverManager#getConnection命令獲取連接的時候,跟蹤呼叫鏈會一直來到NonRegisteringDriver#connect方法,而該方法分析引數url后,知道這是使用loadbalance模塊,因此就會進入NonRegisteringDriver#connectLoadBalanced方法,該方法將呼叫LoadBalancedConnectionProxy#createProxyInstance方法獲取連接,

<style>#mermaid-svg-vnBsUgUv1l2vJ77B .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-vnBsUgUv1l2vJ77B .label text{fill:#333}#mermaid-svg-vnBsUgUv1l2vJ77B .node rect,#mermaid-svg-vnBsUgUv1l2vJ77B .node circle,#mermaid-svg-vnBsUgUv1l2vJ77B .node ellipse,#mermaid-svg-vnBsUgUv1l2vJ77B .node polygon,#mermaid-svg-vnBsUgUv1l2vJ77B .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-vnBsUgUv1l2vJ77B .node .label{text-align:center;fill:#333}#mermaid-svg-vnBsUgUv1l2vJ77B .node.clickable{cursor:pointer}#mermaid-svg-vnBsUgUv1l2vJ77B .arrowheadPath{fill:#333}#mermaid-svg-vnBsUgUv1l2vJ77B .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-vnBsUgUv1l2vJ77B .flowchart-link{stroke:#333;fill:none}#mermaid-svg-vnBsUgUv1l2vJ77B .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-vnBsUgUv1l2vJ77B .edgeLabel rect{opacity:0.9}#mermaid-svg-vnBsUgUv1l2vJ77B .edgeLabel span{color:#333}#mermaid-svg-vnBsUgUv1l2vJ77B .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-vnBsUgUv1l2vJ77B .cluster text{fill:#333}#mermaid-svg-vnBsUgUv1l2vJ77B div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-vnBsUgUv1l2vJ77B .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-vnBsUgUv1l2vJ77B text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-vnBsUgUv1l2vJ77B .actor-line{stroke:grey}#mermaid-svg-vnBsUgUv1l2vJ77B .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-vnBsUgUv1l2vJ77B .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-vnBsUgUv1l2vJ77B #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-vnBsUgUv1l2vJ77B .sequenceNumber{fill:#fff}#mermaid-svg-vnBsUgUv1l2vJ77B #sequencenumber{fill:#333}#mermaid-svg-vnBsUgUv1l2vJ77B #crosshead path{fill:#333;stroke:#333}#mermaid-svg-vnBsUgUv1l2vJ77B .messageText{fill:#333;stroke:#333}#mermaid-svg-vnBsUgUv1l2vJ77B .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-vnBsUgUv1l2vJ77B .labelText,#mermaid-svg-vnBsUgUv1l2vJ77B .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-vnBsUgUv1l2vJ77B .loopText,#mermaid-svg-vnBsUgUv1l2vJ77B .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-vnBsUgUv1l2vJ77B .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-vnBsUgUv1l2vJ77B .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-vnBsUgUv1l2vJ77B .noteText,#mermaid-svg-vnBsUgUv1l2vJ77B .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-vnBsUgUv1l2vJ77B .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-vnBsUgUv1l2vJ77B .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-vnBsUgUv1l2vJ77B .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-vnBsUgUv1l2vJ77B .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vnBsUgUv1l2vJ77B .section{stroke:none;opacity:0.2}#mermaid-svg-vnBsUgUv1l2vJ77B .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-vnBsUgUv1l2vJ77B .section2{fill:#fff400}#mermaid-svg-vnBsUgUv1l2vJ77B .section1,#mermaid-svg-vnBsUgUv1l2vJ77B .section3{fill:#fff;opacity:0.2}#mermaid-svg-vnBsUgUv1l2vJ77B .sectionTitle0{fill:#333}#mermaid-svg-vnBsUgUv1l2vJ77B .sectionTitle1{fill:#333}#mermaid-svg-vnBsUgUv1l2vJ77B .sectionTitle2{fill:#333}#mermaid-svg-vnBsUgUv1l2vJ77B .sectionTitle3{fill:#333}#mermaid-svg-vnBsUgUv1l2vJ77B .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vnBsUgUv1l2vJ77B .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-vnBsUgUv1l2vJ77B .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vnBsUgUv1l2vJ77B .grid path{stroke-width:0}#mermaid-svg-vnBsUgUv1l2vJ77B .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-vnBsUgUv1l2vJ77B .task{stroke-width:2}#mermaid-svg-vnBsUgUv1l2vJ77B .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vnBsUgUv1l2vJ77B .taskText:not([font-size]){font-size:11px}#mermaid-svg-vnBsUgUv1l2vJ77B .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vnBsUgUv1l2vJ77B .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-vnBsUgUv1l2vJ77B .task.clickable{cursor:pointer}#mermaid-svg-vnBsUgUv1l2vJ77B .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-vnBsUgUv1l2vJ77B .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-vnBsUgUv1l2vJ77B .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-vnBsUgUv1l2vJ77B .taskText0,#mermaid-svg-vnBsUgUv1l2vJ77B .taskText1,#mermaid-svg-vnBsUgUv1l2vJ77B .taskText2,#mermaid-svg-vnBsUgUv1l2vJ77B .taskText3{fill:#fff}#mermaid-svg-vnBsUgUv1l2vJ77B .task0,#mermaid-svg-vnBsUgUv1l2vJ77B .task1,#mermaid-svg-vnBsUgUv1l2vJ77B .task2,#mermaid-svg-vnBsUgUv1l2vJ77B .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-vnBsUgUv1l2vJ77B .taskTextOutside0,#mermaid-svg-vnBsUgUv1l2vJ77B .taskTextOutside2{fill:#000}#mermaid-svg-vnBsUgUv1l2vJ77B .taskTextOutside1,#mermaid-svg-vnBsUgUv1l2vJ77B .taskTextOutside3{fill:#000}#mermaid-svg-vnBsUgUv1l2vJ77B .active0,#mermaid-svg-vnBsUgUv1l2vJ77B .active1,#mermaid-svg-vnBsUgUv1l2vJ77B .active2,#mermaid-svg-vnBsUgUv1l2vJ77B .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-vnBsUgUv1l2vJ77B .activeText0,#mermaid-svg-vnBsUgUv1l2vJ77B .activeText1,#mermaid-svg-vnBsUgUv1l2vJ77B .activeText2,#mermaid-svg-vnBsUgUv1l2vJ77B .activeText3{fill:#000 !important}#mermaid-svg-vnBsUgUv1l2vJ77B .done0,#mermaid-svg-vnBsUgUv1l2vJ77B .done1,#mermaid-svg-vnBsUgUv1l2vJ77B .done2,#mermaid-svg-vnBsUgUv1l2vJ77B .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-vnBsUgUv1l2vJ77B .doneText0,#mermaid-svg-vnBsUgUv1l2vJ77B .doneText1,#mermaid-svg-vnBsUgUv1l2vJ77B .doneText2,#mermaid-svg-vnBsUgUv1l2vJ77B .doneText3{fill:#000 !important}#mermaid-svg-vnBsUgUv1l2vJ77B .crit0,#mermaid-svg-vnBsUgUv1l2vJ77B .crit1,#mermaid-svg-vnBsUgUv1l2vJ77B .crit2,#mermaid-svg-vnBsUgUv1l2vJ77B .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-vnBsUgUv1l2vJ77B .activeCrit0,#mermaid-svg-vnBsUgUv1l2vJ77B .activeCrit1,#mermaid-svg-vnBsUgUv1l2vJ77B .activeCrit2,#mermaid-svg-vnBsUgUv1l2vJ77B .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-vnBsUgUv1l2vJ77B .doneCrit0,#mermaid-svg-vnBsUgUv1l2vJ77B .doneCrit1,#mermaid-svg-vnBsUgUv1l2vJ77B .doneCrit2,#mermaid-svg-vnBsUgUv1l2vJ77B .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-vnBsUgUv1l2vJ77B .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-vnBsUgUv1l2vJ77B .milestoneText{font-style:italic}#mermaid-svg-vnBsUgUv1l2vJ77B .doneCritText0,#mermaid-svg-vnBsUgUv1l2vJ77B .doneCritText1,#mermaid-svg-vnBsUgUv1l2vJ77B .doneCritText2,#mermaid-svg-vnBsUgUv1l2vJ77B .doneCritText3{fill:#000 !important}#mermaid-svg-vnBsUgUv1l2vJ77B .activeCritText0,#mermaid-svg-vnBsUgUv1l2vJ77B .activeCritText1,#mermaid-svg-vnBsUgUv1l2vJ77B .activeCritText2,#mermaid-svg-vnBsUgUv1l2vJ77B .activeCritText3{fill:#000 !important}#mermaid-svg-vnBsUgUv1l2vJ77B .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vnBsUgUv1l2vJ77B g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-vnBsUgUv1l2vJ77B g.classGroup text .title{font-weight:bolder}#mermaid-svg-vnBsUgUv1l2vJ77B g.clickable{cursor:pointer}#mermaid-svg-vnBsUgUv1l2vJ77B g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-vnBsUgUv1l2vJ77B g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-vnBsUgUv1l2vJ77B .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-vnBsUgUv1l2vJ77B .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-vnBsUgUv1l2vJ77B .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-vnBsUgUv1l2vJ77B .dashed-line{stroke-dasharray:3}#mermaid-svg-vnBsUgUv1l2vJ77B #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-vnBsUgUv1l2vJ77B #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-vnBsUgUv1l2vJ77B #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-vnBsUgUv1l2vJ77B #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-vnBsUgUv1l2vJ77B #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-vnBsUgUv1l2vJ77B #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-vnBsUgUv1l2vJ77B #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-vnBsUgUv1l2vJ77B #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-vnBsUgUv1l2vJ77B .commit-id,#mermaid-svg-vnBsUgUv1l2vJ77B .commit-msg,#mermaid-svg-vnBsUgUv1l2vJ77B .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vnBsUgUv1l2vJ77B .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vnBsUgUv1l2vJ77B .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vnBsUgUv1l2vJ77B g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vnBsUgUv1l2vJ77B g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-vnBsUgUv1l2vJ77B g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-vnBsUgUv1l2vJ77B g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-vnBsUgUv1l2vJ77B g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-vnBsUgUv1l2vJ77B g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-vnBsUgUv1l2vJ77B .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-vnBsUgUv1l2vJ77B .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-vnBsUgUv1l2vJ77B .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-vnBsUgUv1l2vJ77B .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-vnBsUgUv1l2vJ77B .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-vnBsUgUv1l2vJ77B .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-vnBsUgUv1l2vJ77B .edgeLabel text{fill:#333}#mermaid-svg-vnBsUgUv1l2vJ77B .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vnBsUgUv1l2vJ77B .node circle.state-start{fill:black;stroke:black}#mermaid-svg-vnBsUgUv1l2vJ77B .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-vnBsUgUv1l2vJ77B #statediagram-barbEnd{fill:#9370db}#mermaid-svg-vnBsUgUv1l2vJ77B .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-vnBsUgUv1l2vJ77B .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-vnBsUgUv1l2vJ77B .statediagram-state .divider{stroke:#9370db}#mermaid-svg-vnBsUgUv1l2vJ77B .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-vnBsUgUv1l2vJ77B .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-vnBsUgUv1l2vJ77B .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-vnBsUgUv1l2vJ77B .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-vnBsUgUv1l2vJ77B .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-vnBsUgUv1l2vJ77B .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-vnBsUgUv1l2vJ77B .note-edge{stroke-dasharray:5}#mermaid-svg-vnBsUgUv1l2vJ77B .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-vnBsUgUv1l2vJ77B .error-icon{fill:#522}#mermaid-svg-vnBsUgUv1l2vJ77B .error-text{fill:#522;stroke:#522}#mermaid-svg-vnBsUgUv1l2vJ77B .edge-thickness-normal{stroke-width:2px}#mermaid-svg-vnBsUgUv1l2vJ77B .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-vnBsUgUv1l2vJ77B .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-vnBsUgUv1l2vJ77B .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-vnBsUgUv1l2vJ77B .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-vnBsUgUv1l2vJ77B .marker{fill:#333}#mermaid-svg-vnBsUgUv1l2vJ77B .marker.cross{stroke:#333} :root { --mermaid-font-family: "trebuchet ms", verdana, arial;}</style> <style>#mermaid-svg-vnBsUgUv1l2vJ77B { color: rgba(0, 0, 0, 0.75); font: ; }</style> 呼叫方 DriverManager NonRegisteringDriver LoadBalancedConnectionProxy getConnection connect 分析url,發現要使用loadbalance connectLoadBalanced createProxyInstance 呼叫方 DriverManager NonRegisteringDriver LoadBalancedConnectionProxy

在上述呼叫鏈的程序中,并沒有捕獲例外的行為,因此,如果LoadBalancedConnectionProxy#createProxyInstance方法的底層拋出的任何例外都會直接拋向呼叫方,所以呼叫方通常都會有一個獲取SQLException例外的行為,
我們觀察下LoadBalancedConnectionProxy#createProxyInstance方法:

public static LoadBalancedConnection createProxyInstance(List<String> hosts, Properties props) throws SQLException {
        LoadBalancedConnectionProxy connProxy = new LoadBalancedConnectionProxy(hosts, props);

        return (LoadBalancedConnection) java.lang.reflect.Proxy.newProxyInstance(LoadBalancedConnection.class.getClassLoader(), INTERFACES_TO_PROXY, connProxy);
    }

這里只有兩行命令,第一行構造LoadBalancedConnectionProxy物件,根據命令的宣告,它有可能拋出SQLException;第二句是創建動態代理物件,根據命令的宣告,它有可能拋出IllegalArgumentException,這例外是RuntimeException的子類,因此我們只需要著重觀察LoadBalancedConnectionProxy物件的構造程序,

private LoadBalancedConnectionProxy(List<String> hosts, Properties props) throws SQLException {
        super();
	....
        
    }

私有的建構式首先會呼叫父類的建構式,里面會構造JDBC4LoadBalancedMySQLConnection物件,并作為thisAsConnection屬性值,而當前的LoadBalancedConnectionProxy物件作為JDBC4LoadBalancedMySQLConnection物件的thisAsProxy屬性值,在這個程序中,其實不會拋出SQLException例外,
我們再回到LoadBalancedConnectionProxy建構式:

private LoadBalancedConnectionProxy(List<String> hosts, Properties props) throws SQLException {
        super();
	....
        // hosts specifications may have been reset with settings from a previous connection group
        int numHosts = initializeHostsSpecs(hosts, props);

        this.liveConnections = new HashMap<String, ConnectionImpl>(numHosts);
        this.hostsToListIndexMap = new HashMap<String, Integer>(numHosts);
        for (int i = 0; i < numHosts; i++) {
            this.hostsToListIndexMap.put(this.hostList.get(i), i);
        }
        this.connectionsToHostsMap = new HashMap<ConnectionImpl, String>(numHosts);
    ....
        String strategy = this.localProps.getProperty("loadBalanceStrategy", "random");
        if ("random".equals(strategy)) {
            this.balancer = (BalanceStrategy) Util.loadExtensions(null, props, RandomBalanceStrategy.class.getName(), "InvalidLoadBalanceStrategy", null)
                    .get(0);
        } else if ("bestResponseTime".equals(strategy)) {
            this.balancer = (BalanceStrategy) Util
                    .loadExtensions(null, props, BestResponseTimeBalanceStrategy.class.getName(), "InvalidLoadBalanceStrategy", null).get(0);
        } else if ("serverAffinity".equals(strategy)) {
            this.balancer = (BalanceStrategy) Util.loadExtensions(null, props, ServerAffinityStrategy.class.getName(), "InvalidLoadBalanceStrategy", null)
                    .get(0);
        } else {
            this.balancer = (BalanceStrategy) Util.loadExtensions(null, props, strategy, "InvalidLoadBalanceStrategy", null).get(0);
        }
        
		String lbExceptionChecker = this.localProps.getProperty("loadBalanceExceptionChecker", "com.mysql.jdbc.StandardLoadBalanceExceptionChecker");
        this.exceptionChecker = (LoadBalanceExceptionChecker) Util.loadExtensions(null, props, lbExceptionChecker, "InvalidLoadBalanceExceptionChecker", null)
                .get(0);
    ....
        pickNewConnection();
    }

它根據url里的ip:port集合填充了hostsToListIndexMap屬性,key為ip:port的值,value為從0開始的下標值,同時也構造了與ConnectionImpl有關的屬性:liveConnections和connectionsToHostsMap,這兩屬性會在后面添加ConnectionImpl物件,
隨后還會根據url里的loadBalanceStrategy選項構造對應的策略物件,并作為balance屬性值,
最后初始化了例外檢查器exceptionChecker,如果url里有指定的就使用指定的類來構造,沒有的話就使用默認的com.mysql.jdbc.StandardLoadBalanceExceptionChecker物件,
在建構式的方法體里,pickNewConnection方法之前,雖然有些呼叫的方法會拋出例外,但都有捕獲行為,因此到目前為止仍然安全,我們接著進入非常眼熟的pickNewConnection方法一探究竟:

@Override
    synchronized void pickNewConnection() throws SQLException {
    ....
        for (int hostsTried = 0, hostsToTry = this.hostList.size(); hostsTried < hostsToTry; hostsTried++) {
            ConnectionImpl newConn = null;
            try {
                newConn = this.balancer.pickConnection(this, Collections.unmodifiableList(this.hostList), Collections.unmodifiableMap(this.liveConnections),
                        this.responseTimes.clone(), this.retriesAllDown);

                if (this.currentConnection != null) {
                    if (pingBeforeReturn) {
                        if (pingTimeout == 0) {
                            newConn.ping();
                        } else {
                            newConn.pingInternal(true, pingTimeout);
                        }
                    }

                    syncSessionState(this.currentConnection, newConn);
                }

                this.currentConnection = newConn;
                return;

            } catch (SQLException e) {
                if (shouldExceptionTriggerConnectionSwitch(e) && newConn != null) {
                    // connection error, close up shop on current connection
                    invalidateConnection(newConn);
                }
            }
        }

        // no hosts available to swap connection to, close up.
   ....
    }

該方法有一個很大的for回圈,嵌套其內的是很大的一個try catch 代碼塊,這段代碼的意思就是試圖通過策略物件來獲取連接物件,而最大次數為url里ip:port的數量,獲取到連接物件后,還要通過ping的行為確保連接正常,如果連接被成功構造,那么就作為currentConnection屬性值,因為這些行為都需要發生網路連接以及與mysql發生資料互動,所以都有可能產生例外,而根據《Mysql Connector/J 原始碼分析(普通Connection)》所述,構造連接程序中產生的通訊例外和資料例外都會統一以SQLException形式往外拋,因此,在此處就會被捕獲,例外的處理是本節關注的內容,所以我們先觀察catch部分,此處先呼叫shouldExceptionTriggerConnectionSwitch命令判斷例外,然后再判斷連接物件是否已經構造了,為什么要判斷連接物件是否已經構造了呢?貌似多余,實際是安全的做法,因為上面提過,是先構造連接,然后通過連接發送ping命令,這兩步是一前一后,可能在構造連接的時候網路是正常的,但發送ping命令時現狀況是有可能的,所以就會出現構造了連接物件但又會拋出例外的情況,

@Override
    boolean shouldExceptionTriggerConnectionSwitch(Throwable t) {
        return t instanceof SQLException && this.exceptionChecker.shouldExceptionTriggerFailover((SQLException) t);
    }

我們假設使用的是默認的例外檢查器com.mysql.jdbc.StandardLoadBalanceExceptionChecker,該類有兩個集合sqlStateList和sqlExClassList,而集合的元素分別來自于url的loadBalanceSQLStateFailover和loadBalanceSQLExceptionSubclassFailover選項,看客可以通過官網了解這兩選項,在此不詳述,
shouldExceptionTriggerFailover方法在以下情況此方法會回傳true:

  • 錯誤碼以08開頭
  • 錯誤碼雖然不以08開頭,但錯誤碼以sqlStateList任一元素值開頭
  • 拋出的例外是通訊例外CommunicationsException
  • 拋出的例外型別是sqlExClassList任一元素的例外型別相等或者是子類,
    也就是說,只要是通訊例外或者用戶指定的例外,該方法都會回傳true,
    我們把目光拉回到LoadBalancedConnectionProxy#pickNewConnection方法的捕獲例外位置:
@Override
    synchronized void pickNewConnection() throws SQLException {
        ....
        for (int hostsTried = 0, hostsToTry = this.hostList.size(); hostsTried < hostsToTry; hostsTried++) {
            ConnectionImpl newConn = null;
            try {
                ....

            } catch (SQLException e) {
                if (shouldExceptionTriggerConnectionSwitch(e) && newConn != null) {
                    // connection error, close up shop on current connection
                    invalidateConnection(newConn);
                }
            }
        }

        // no hosts available to swap connection to, close up.
        this.isClosed = true;
        this.closedReason = "Connection closed after inability to pick valid new connection during load-balance.";
    }

總的來說,當一個ip:port相對應的連接構造時或者執行ping命令時,底層拋出了SQLException后都會嘗試另一個ip:port,每個被嘗試建立的連接哪怕拋出SQLException(通訊例外和資料例外)都會被捕獲,如果每一組的ip:port都未能成功構造連接,那么currentConnection屬性值為null,isClosed為true,呼叫者是無法正常使用連接的,
經過上面的分析,在構造連接代理LoadBalancedConnectionProxy物件時,底層拋出的SQLException例外不會傳導到呼叫者,

2.使用階段

MultiHostConnectionProxy直接多載了java.lang.reflect.InvocationHandler介面的invoke方法,我們先觀察一下:

public synchronized Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        ....
        try {
            return invokeMore(proxy, method, args);
        } catch (InvocationTargetException e) {
            throw e.getCause() != null ? e.getCause() : e;
        } catch (Exception e) {
            // Check if the captured exception must be wrapped by an unchecked exception.
            Class<?>[] declaredException = method.getExceptionTypes();
            for (Class<?> declEx : declaredException) {
                if (declEx.isAssignableFrom(e.getClass())) {
                    throw e;
                }
            }
            throw new IllegalStateException(e.getMessage(), e);
        }
    }

該方法的省略部分不會拋例外,但它對invokeMore有一個獲取例外的行為,并且區分了InvocationTargetException和其他的情況的例外,從這里來看,底層拋上來的例外都會往呼叫者拋,而invokeMore方法被LoadBalancedConnectionProxy多載,我們進一步觀察下:

@Override
    public synchronized Object invokeMore(Object proxy, Method method, Object[] args) throws Throwable {
        String methodName = method.getName();

        if (this.isClosed && !allowedOnClosedConnection(method) && method.getExceptionTypes().length > 0) {
            if (this.autoReconnect && !this.closedExplicitly) {
                // try to reconnect first!
                this.currentConnection = null;
                pickNewConnection();
                this.isClosed = false;
                this.closedReason = null;
       ....

        Object result = null;

        try {
            result = method.invoke(this.thisAsConnection, args);

            if (result != null) {
                if (result instanceof com.mysql.jdbc.Statement) {
                    ((com.mysql.jdbc.Statement) result).setPingTarget(this);
                }
                result = proxyIfReturnTypeIsJdbcInterface(method.getReturnType(), result);
            }

        } catch (InvocationTargetException e) {
            dealWithInvocationException(e);

        } finally {
           ....
                pickNewConnection();
           ....
        }

        return result;
    }

在方法里我們看到有兩處呼叫了pickNewConnection方法,前文已經分析過,里面不會有例外拋出,那么,我們需要分析的點就集中在try catch 里了,因為以反射的形式呼叫,所以一旦有問題會以InvocationTargetException的形式拋出,然后進入MultiHostConnectionProxy#dealWithInvocationException方法進一步分析原始例外型別:

void dealWithInvocationException(InvocationTargetException e) throws SQLException, Throwable, InvocationTargetException {
        Throwable t = e.getTargetException();

        if (t != null) {
            if (this.lastExceptionDealtWith != t && shouldExceptionTriggerConnectionSwitch(t)) {
                invalidateCurrentConnection();
                pickNewConnection();
                this.lastExceptionDealtWith = t;
            }
            throw t;
        }
        throw e;
    }

從代碼結構上看,例外是一定會往上拋,因為MultiHostConnectionProxy#dealWithInvocationException方法是在LoadBalancedConnectionProxy#invokeMore的catch代碼塊里呼叫,而該catch代碼塊沒有進一步捕獲例外,因此例外將到達MultiHostConnectionProxy#invoke方法里的捕獲例外的代碼塊,前文通過觀察它的捕獲例外代碼塊可知道,例外最侄訓繼續向上層拋,也就是說,在使用連接的程序中,一旦遇到通訊例外或者資料例外,呼叫者都會感知到,當例外發生時,我們可以基本知道它經過的地方:

<style>#mermaid-svg-99rKuNKW9yNJN6iQ .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-99rKuNKW9yNJN6iQ .label text{fill:#333}#mermaid-svg-99rKuNKW9yNJN6iQ .node rect,#mermaid-svg-99rKuNKW9yNJN6iQ .node circle,#mermaid-svg-99rKuNKW9yNJN6iQ .node ellipse,#mermaid-svg-99rKuNKW9yNJN6iQ .node polygon,#mermaid-svg-99rKuNKW9yNJN6iQ .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-99rKuNKW9yNJN6iQ .node .label{text-align:center;fill:#333}#mermaid-svg-99rKuNKW9yNJN6iQ .node.clickable{cursor:pointer}#mermaid-svg-99rKuNKW9yNJN6iQ .arrowheadPath{fill:#333}#mermaid-svg-99rKuNKW9yNJN6iQ .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-99rKuNKW9yNJN6iQ .flowchart-link{stroke:#333;fill:none}#mermaid-svg-99rKuNKW9yNJN6iQ .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-99rKuNKW9yNJN6iQ .edgeLabel rect{opacity:0.9}#mermaid-svg-99rKuNKW9yNJN6iQ .edgeLabel span{color:#333}#mermaid-svg-99rKuNKW9yNJN6iQ .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-99rKuNKW9yNJN6iQ .cluster text{fill:#333}#mermaid-svg-99rKuNKW9yNJN6iQ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-99rKuNKW9yNJN6iQ .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-99rKuNKW9yNJN6iQ text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-99rKuNKW9yNJN6iQ .actor-line{stroke:grey}#mermaid-svg-99rKuNKW9yNJN6iQ .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-99rKuNKW9yNJN6iQ .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-99rKuNKW9yNJN6iQ #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-99rKuNKW9yNJN6iQ .sequenceNumber{fill:#fff}#mermaid-svg-99rKuNKW9yNJN6iQ #sequencenumber{fill:#333}#mermaid-svg-99rKuNKW9yNJN6iQ #crosshead path{fill:#333;stroke:#333}#mermaid-svg-99rKuNKW9yNJN6iQ .messageText{fill:#333;stroke:#333}#mermaid-svg-99rKuNKW9yNJN6iQ .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-99rKuNKW9yNJN6iQ .labelText,#mermaid-svg-99rKuNKW9yNJN6iQ .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-99rKuNKW9yNJN6iQ .loopText,#mermaid-svg-99rKuNKW9yNJN6iQ .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-99rKuNKW9yNJN6iQ .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-99rKuNKW9yNJN6iQ .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-99rKuNKW9yNJN6iQ .noteText,#mermaid-svg-99rKuNKW9yNJN6iQ .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-99rKuNKW9yNJN6iQ .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-99rKuNKW9yNJN6iQ .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-99rKuNKW9yNJN6iQ .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-99rKuNKW9yNJN6iQ .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-99rKuNKW9yNJN6iQ .section{stroke:none;opacity:0.2}#mermaid-svg-99rKuNKW9yNJN6iQ .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-99rKuNKW9yNJN6iQ .section2{fill:#fff400}#mermaid-svg-99rKuNKW9yNJN6iQ .section1,#mermaid-svg-99rKuNKW9yNJN6iQ .section3{fill:#fff;opacity:0.2}#mermaid-svg-99rKuNKW9yNJN6iQ .sectionTitle0{fill:#333}#mermaid-svg-99rKuNKW9yNJN6iQ .sectionTitle1{fill:#333}#mermaid-svg-99rKuNKW9yNJN6iQ .sectionTitle2{fill:#333}#mermaid-svg-99rKuNKW9yNJN6iQ .sectionTitle3{fill:#333}#mermaid-svg-99rKuNKW9yNJN6iQ .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-99rKuNKW9yNJN6iQ .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-99rKuNKW9yNJN6iQ .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-99rKuNKW9yNJN6iQ .grid path{stroke-width:0}#mermaid-svg-99rKuNKW9yNJN6iQ .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-99rKuNKW9yNJN6iQ .task{stroke-width:2}#mermaid-svg-99rKuNKW9yNJN6iQ .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-99rKuNKW9yNJN6iQ .taskText:not([font-size]){font-size:11px}#mermaid-svg-99rKuNKW9yNJN6iQ .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-99rKuNKW9yNJN6iQ .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-99rKuNKW9yNJN6iQ .task.clickable{cursor:pointer}#mermaid-svg-99rKuNKW9yNJN6iQ .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-99rKuNKW9yNJN6iQ .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-99rKuNKW9yNJN6iQ .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-99rKuNKW9yNJN6iQ .taskText0,#mermaid-svg-99rKuNKW9yNJN6iQ .taskText1,#mermaid-svg-99rKuNKW9yNJN6iQ .taskText2,#mermaid-svg-99rKuNKW9yNJN6iQ .taskText3{fill:#fff}#mermaid-svg-99rKuNKW9yNJN6iQ .task0,#mermaid-svg-99rKuNKW9yNJN6iQ .task1,#mermaid-svg-99rKuNKW9yNJN6iQ .task2,#mermaid-svg-99rKuNKW9yNJN6iQ .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-99rKuNKW9yNJN6iQ .taskTextOutside0,#mermaid-svg-99rKuNKW9yNJN6iQ .taskTextOutside2{fill:#000}#mermaid-svg-99rKuNKW9yNJN6iQ .taskTextOutside1,#mermaid-svg-99rKuNKW9yNJN6iQ .taskTextOutside3{fill:#000}#mermaid-svg-99rKuNKW9yNJN6iQ .active0,#mermaid-svg-99rKuNKW9yNJN6iQ .active1,#mermaid-svg-99rKuNKW9yNJN6iQ .active2,#mermaid-svg-99rKuNKW9yNJN6iQ .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-99rKuNKW9yNJN6iQ .activeText0,#mermaid-svg-99rKuNKW9yNJN6iQ .activeText1,#mermaid-svg-99rKuNKW9yNJN6iQ .activeText2,#mermaid-svg-99rKuNKW9yNJN6iQ .activeText3{fill:#000 !important}#mermaid-svg-99rKuNKW9yNJN6iQ .done0,#mermaid-svg-99rKuNKW9yNJN6iQ .done1,#mermaid-svg-99rKuNKW9yNJN6iQ .done2,#mermaid-svg-99rKuNKW9yNJN6iQ .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-99rKuNKW9yNJN6iQ .doneText0,#mermaid-svg-99rKuNKW9yNJN6iQ .doneText1,#mermaid-svg-99rKuNKW9yNJN6iQ .doneText2,#mermaid-svg-99rKuNKW9yNJN6iQ .doneText3{fill:#000 !important}#mermaid-svg-99rKuNKW9yNJN6iQ .crit0,#mermaid-svg-99rKuNKW9yNJN6iQ .crit1,#mermaid-svg-99rKuNKW9yNJN6iQ .crit2,#mermaid-svg-99rKuNKW9yNJN6iQ .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-99rKuNKW9yNJN6iQ .activeCrit0,#mermaid-svg-99rKuNKW9yNJN6iQ .activeCrit1,#mermaid-svg-99rKuNKW9yNJN6iQ .activeCrit2,#mermaid-svg-99rKuNKW9yNJN6iQ .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-99rKuNKW9yNJN6iQ .doneCrit0,#mermaid-svg-99rKuNKW9yNJN6iQ .doneCrit1,#mermaid-svg-99rKuNKW9yNJN6iQ .doneCrit2,#mermaid-svg-99rKuNKW9yNJN6iQ .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-99rKuNKW9yNJN6iQ .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-99rKuNKW9yNJN6iQ .milestoneText{font-style:italic}#mermaid-svg-99rKuNKW9yNJN6iQ .doneCritText0,#mermaid-svg-99rKuNKW9yNJN6iQ .doneCritText1,#mermaid-svg-99rKuNKW9yNJN6iQ .doneCritText2,#mermaid-svg-99rKuNKW9yNJN6iQ .doneCritText3{fill:#000 !important}#mermaid-svg-99rKuNKW9yNJN6iQ .activeCritText0,#mermaid-svg-99rKuNKW9yNJN6iQ .activeCritText1,#mermaid-svg-99rKuNKW9yNJN6iQ .activeCritText2,#mermaid-svg-99rKuNKW9yNJN6iQ .activeCritText3{fill:#000 !important}#mermaid-svg-99rKuNKW9yNJN6iQ .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-99rKuNKW9yNJN6iQ g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-99rKuNKW9yNJN6iQ g.classGroup text .title{font-weight:bolder}#mermaid-svg-99rKuNKW9yNJN6iQ g.clickable{cursor:pointer}#mermaid-svg-99rKuNKW9yNJN6iQ g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-99rKuNKW9yNJN6iQ g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-99rKuNKW9yNJN6iQ .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-99rKuNKW9yNJN6iQ .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-99rKuNKW9yNJN6iQ .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-99rKuNKW9yNJN6iQ .dashed-line{stroke-dasharray:3}#mermaid-svg-99rKuNKW9yNJN6iQ #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-99rKuNKW9yNJN6iQ #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-99rKuNKW9yNJN6iQ #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-99rKuNKW9yNJN6iQ #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-99rKuNKW9yNJN6iQ #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-99rKuNKW9yNJN6iQ #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-99rKuNKW9yNJN6iQ #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-99rKuNKW9yNJN6iQ #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-99rKuNKW9yNJN6iQ .commit-id,#mermaid-svg-99rKuNKW9yNJN6iQ .commit-msg,#mermaid-svg-99rKuNKW9yNJN6iQ .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-99rKuNKW9yNJN6iQ .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-99rKuNKW9yNJN6iQ .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-99rKuNKW9yNJN6iQ g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-99rKuNKW9yNJN6iQ g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-99rKuNKW9yNJN6iQ g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-99rKuNKW9yNJN6iQ g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-99rKuNKW9yNJN6iQ g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-99rKuNKW9yNJN6iQ g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-99rKuNKW9yNJN6iQ .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-99rKuNKW9yNJN6iQ .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-99rKuNKW9yNJN6iQ .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-99rKuNKW9yNJN6iQ .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-99rKuNKW9yNJN6iQ .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-99rKuNKW9yNJN6iQ .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-99rKuNKW9yNJN6iQ .edgeLabel text{fill:#333}#mermaid-svg-99rKuNKW9yNJN6iQ .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-99rKuNKW9yNJN6iQ .node circle.state-start{fill:black;stroke:black}#mermaid-svg-99rKuNKW9yNJN6iQ .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-99rKuNKW9yNJN6iQ #statediagram-barbEnd{fill:#9370db}#mermaid-svg-99rKuNKW9yNJN6iQ .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-99rKuNKW9yNJN6iQ .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-99rKuNKW9yNJN6iQ .statediagram-state .divider{stroke:#9370db}#mermaid-svg-99rKuNKW9yNJN6iQ .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-99rKuNKW9yNJN6iQ .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-99rKuNKW9yNJN6iQ .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-99rKuNKW9yNJN6iQ .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-99rKuNKW9yNJN6iQ .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-99rKuNKW9yNJN6iQ .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-99rKuNKW9yNJN6iQ .note-edge{stroke-dasharray:5}#mermaid-svg-99rKuNKW9yNJN6iQ .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-99rKuNKW9yNJN6iQ .error-icon{fill:#522}#mermaid-svg-99rKuNKW9yNJN6iQ .error-text{fill:#522;stroke:#522}#mermaid-svg-99rKuNKW9yNJN6iQ .edge-thickness-normal{stroke-width:2px}#mermaid-svg-99rKuNKW9yNJN6iQ .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-99rKuNKW9yNJN6iQ .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-99rKuNKW9yNJN6iQ .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-99rKuNKW9yNJN6iQ .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-99rKuNKW9yNJN6iQ .marker{fill:#333}#mermaid-svg-99rKuNKW9yNJN6iQ .marker.cross{stroke:#333} :root { --mermaid-font-family: "trebuchet ms", verdana, arial;}</style> <style>#mermaid-svg-99rKuNKW9yNJN6iQ { color: rgba(0, 0, 0, 0.75); font: ; }</style> Method#invoke LoadBalanceConnectionProxy#invokeMore的catch代碼塊 MultiHostConnectionProxy#dealWithInvocationException MultiHostConnectionProxy#invoke的catch代碼塊 呼叫者 Method#invoke LoadBalanceConnectionProxy#invokeMore的catch代碼塊 MultiHostConnectionProxy#dealWithInvocationException MultiHostConnectionProxy#invoke的catch代碼塊 呼叫者

我們把目光放回到MultiHostConnectionProxy#dealWithInvocationException:

void dealWithInvocationException(InvocationTargetException e) throws SQLException, Throwable, InvocationTargetException {
        Throwable t = e.getTargetException();

        if (t != null) {
            if (this.lastExceptionDealtWith != t && shouldExceptionTriggerConnectionSwitch(t)) {
                invalidateCurrentConnection();
                pickNewConnection();
                this.lastExceptionDealtWith = t;
            }
            throw t;
        }
        throw e;
    }

呼叫的shouldExceptionTriggerConnectionSwitch被LoadBalanceConnectionProxy多載了:

@Override
    boolean shouldExceptionTriggerConnectionSwitch(Throwable t) {
        return t instanceof SQLException && this.exceptionChecker.shouldExceptionTriggerFailover((SQLException) t);
    }

我們假設exceptionChecker使用的是默認的com.mysql.jdbc.StandardLoadBalanceExceptionChecker
該類有兩個集合sqlStateList和sqlExClassList,而集合的元素分別來自于url的loadBalanceSQLStateFailover和loadBalanceSQLExceptionSubclassFailover選項,看官可以通過官網了解這兩選項,在此不詳述,
shouldExceptionTriggerFailover方法在以下情況此方法會回傳true:

  • 錯誤碼以08開頭
  • 錯誤碼雖然不以08開頭,但錯誤碼以sqlStateList任一元素值開頭
  • 拋出的例外是通訊例外CommunicationsException
  • 拋出的例外型別是sqlExClassList任一元素的例外型別相等或者是子類,
    也就是說,只要是通訊例外或者用戶指定的例外,該方法都會回傳true,
    我們再次結合MultiHostConnectionProxy#dealWithInvocationException來看:
void dealWithInvocationException(InvocationTargetException e) throws SQLException, Throwable, InvocationTargetException {
        Throwable t = e.getTargetException();

        if (t != null) {
            if (this.lastExceptionDealtWith != t && shouldExceptionTriggerConnectionSwitch(t)) {
                invalidateCurrentConnection();
                pickNewConnection();
                this.lastExceptionDealtWith = t;
            }
            throw t;
        }
        throw e;
    }

如果例外屬于通訊例外以及用戶指定的情況一旦發生,首先更新連接,然后將例外往上拋,

小結

動態代理連接在構造階段即使底層無法成功建立連接,但動態代理連接是能夠成功構造并被呼叫者持有,而動態代理連接在使用程序中,一旦遇到任何例外,都會被呼叫者感知到,這一點與failover確實不一樣,呼叫者可以馬上停止當前事務程序而重新再來,這樣會讓事務操作變得可以掌控,也可以說事務的原子性得以保證,它的這種特性,也符合我們使用普通Connection時的編程規范(try…事務操作…catch…回滾事務…finally…關閉資源…),因此,如果我們專案一開始比較小,使用普通的Connection模式,到后來需要資料庫結構升級為支持負載均衡的時候,我們不需要修改代碼,只需要修改下url配置即可,

三、autoReconnect選項

根據官網介紹autoReconnect選項是failover模塊重要選項之一,在loadbalance的描述部分只字未提,但我們通過閱讀代碼時會看到它的身影,本節就一起分析該選項所起到的作用,
該選項對應著autoReconnect屬性,而該項屬性被使用到的地方僅在于
LoadBalancedConnectionProxy#invokeMore方法:

@Override
    public synchronized Object invokeMore(Object proxy, Method method, Object[] args) throws Throwable {
        String methodName = method.getName();
        if (this.isClosed && !allowedOnClosedConnection(method) && method.getExceptionTypes().length > 0) {
            if (this.autoReconnect && !this.closedExplicitly) {
                // try to reconnect first!
                this.currentConnection = null;
                pickNewConnection();
        	....
        Object result = null;
        try {
            result = method.invoke(this.thisAsConnection, args);
           ....
        return result;
    }

在Method#invke前,它是呼叫pickNewConnection的條件之一,我們分析下各項條件成立的因素:

  • this.isClosed為真值
    該值為真值的地方出現在:
    1)pickNewConnection方法體里,如果url的ip:port都不能成功建立連接,就為真值,
    2)doPing方法體里,如果未能ping成功,就為真值,
    3)執行連接的close方法
    4)執行連接的abortInternal方法
    5)執行連接的abort方法
  • !allowedOnClosedConnection為真值
    只要不執行連接的以下方法就會成立:
    1)連接的getAutoCommit方法
    2)連接的getCatalog方法
    3)連接的getTransactionIsolation方法
    4)連接的getSessionMaxRows方法
  • method.getExceptionTypes().length > 0
    只要方法宣告會拋例外即滿足,
  • this.autoReconnect
    url上添加autoReconnect=true選項即滿足,
  • !this.closedExplicitly
    不執行連接的close方法即可為真:

作用分析

所以假設用戶在url添加了autoReconnect=true的情況下,并且呼叫方呼叫動態連接的上一條命令不是close方法情況下,大致可以梳理出以下3個場景:
1)當用戶獲取連接動態代理時會執行pickNewConnection方法,當url里所有的ip:port都不能成功建立連接時isClosed為true,因為呼叫方不主動呼叫LoadBalancedConnectionProxy#getCurrentActiveHost方法是感知不到底層連接沒有建立,所以當他繼續使用動態代理連接執行查詢或者更新資料的SQL命令時,會先嘗試更新連接,
2)用戶已經成功獲取動態代理,在使用時由于網路原因使得url里所有的ip:port都不能成功建立連接,前文講述過遇到通訊例外時,會執行pickNewConnection方法并拋出通訊例外,如果url里所有的ip:port都未能成功建立連接,isClosed為true,當網路恢復正常,重新執行的操作會先嘗試更新連接,
3)呼叫者先呼叫接連的abort或者abortInternal方法,然后再執行增刪改查的命令,也就是說呼叫方可能通過呼叫abort或者abortInternal方法主動地更換連接,然后再操作資料,

對于前兩點,當網路出現極端情況下,極大方便了開發人員,因為他們不必考慮如何解決這種情況下的重連,另外,也可以理解為是一個兜底的保障作用,確保一旦網路恢復后,仍能正常執行SQL操作,
對于第三點,可理解為給呼叫者提供更靈活的手段,在需要的時候呼叫abort或者abortInternal命令更換連接,然后再進行SQL操作,所以說它是讓這個機制更完善,

所以,基于上面的分析,官網上其實可以多作介紹的,

四、何時重建連接

重建連接的操作以LoadBalancedConnectionProxy#pickNewConnection為入口,該方法會輪循url各組ip:port,只要有一組能夠連接成功就算建立了與Mysql的連接,如果沒有一組能夠成功建立連接,設定isClosed為true,
那么該方法中哪些時間點上會被呼叫呢?

  1. 構造動態代理連接j時,LoadBalancedConnectionProxy的建構式會呼叫該方法,
  2. 正式呼叫Method#invoke方法前有可能執行,請看上一節,
  3. 呼叫Method#invoke方法出現了通訊例外,
  4. 執行完commit或者rollback方法后,

所以,如果某個ip:port對應的底層連接能夠正常作業,它就有可能不會被更換,之所以說有可能,是因為當呼叫者關閉自動提交,執行完SQL命令后,發送commit或者rollback命令時,它將被更換,也就是說,當一筆事務完成了就需要更換底層連接,這樣從另一個角度看,它至少保證了事務內的命令只由一個底層的連接傳送,這樣就不會出現一筆事務內的命令有的在A服務器運行有的在B服務器運行,確保資料的一致性,如果呼叫者沒有關閉自動提交,執行查詢命令或者更新單筆資料,底層連接不會更換,

五、實用性分析

動態代理連接在使用中遇到例外能夠被呼叫方即時感知到,這使得loadbalance模式具有使用的價值,故名思義,此模式還能起到負載均衡的效果,用戶只需要在url上多配置幾組ip:port(mysql的服務器得跟上), 這樣就可以均衡各臺資料庫服務器的壓力,至少系統進行壓力測驗時可以輕松過關_
Loadbalance模式在遇到通訊例外時就會更換層底連接,所以需要保證各臺服務器上的資料是一致的,最好實作強一致性,

  • 對于微型專案,可以采用如下結構:
    在這里插入圖片描述讓兩臺Mysql服務器互為主備,

  • 讓高可用性上一個臺階,可使用Percona XtraDB:
    在這里插入圖片描述它可實作集群內所有Mysql服務器資料的強一致性,但是如果成員太多,為保持資料的一致性會導致性能有所下降,

  • 使用資料庫中間件和集群
    在這里插入圖片描述此方法由中間件分析出SQL操作是讀操作還是寫操作,然后將操作分配給專門用于讀的服務器或者專門用于接受寫命令的服務器,此方案要求最高,但它兼顧了高可用和性能,

小結

Loadbalance模式強調的是壓力的均衡,所以要使用好這種連接模式,用戶還需要考慮Mysql的拓撲和資料同步問題,這樣才能夠符合當代應用對于資料高可用的要求,如果用戶對于資料寫操作的耗時不敏感,用戶完全可以采用此模式,

六、總結

Loadbalance模式具有使用價值,其特點如下:

  • 呼叫者在使用程序中能夠感知到底層發生的例外,另外呼叫者只需構造一個動態代理連接即可,
  • 呼叫者在使用程序中遇到例外,處理方法與使用普通的Connection一樣,使用者如果想從普通的Connection升級到Loadbalance模式,在應用層面只需要修改url,無需改代碼,
  • 可擴展性強,用戶發現資料庫資源占用高,可以多安排設備,然后在url里添加更多的ip:port即可,
  • Loadbalance模式強調的是壓力的均衡,所以需要部署多臺Mysql服務器,另外,當前一筆事務正常運行完畢,下一筆事務遇到通訊例外,底層連接就會被替換,下次重新執行第二筆事務將在另外一臺服務器上執行,所以這里存在資料同步問題,所以使用此模式,需要掌握Mysql多機的部署知識,而且必須要將資料的一致性放在最首要的位置來考慮,
  • 高可用性,根據上一點,使用好Loadbalance模式,無形中實作了資料的高可用性,

根據“四、何時重建連接”一節可知,Loadbalace模式通常在遇到通訊例外或者提交/回滾事務的時候才更換連接,它的核心目的只不過是讓呼叫者可以重復使用動態代理連接,此Loadbalance與我們想象中的負載均衡不是同一個概念,如果用戶對回應速度有比較高的要求,這種模式就不能勝任了,后續文章將介紹replication模式,看看它在提高回應速度方面有何設計良方,

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/154951.html

標籤:其他

上一篇:Python學習2020/10/2

下一篇:python實作簡陋的貪吃蛇小游戲

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 網閘典型架構簡述

    網閘架構一般分為兩種:三主機的三系統架構網閘和雙主機的2+1架構網閘。 三主機架構分別為內端機、外端機和仲裁機。三機無論從軟體和硬體上均各自獨立。首先從硬體上來看,三機都用各自獨立的主板、記憶體及存盤設備。從軟體上來看,三機有各自獨立的作業系統。這樣能達到完全的三機獨立。對于“2+1”系統,“2”分為 ......

    uj5u.com 2020-09-10 02:00:44 more
  • 如何從xshell上傳檔案到centos linux虛擬機里

    如何從xshell上傳檔案到centos linux虛擬機里及:虛擬機CentOs下執行 yum -y install lrzsz命令,出現錯誤:鏡像無法找到軟體包 前言 一、安裝lrzsz步驟 二、上傳檔案 三、遇到的問題及解決方案 總結 前言 提示:其實很簡單,往虛擬機上安裝一個上傳檔案的工具 ......

    uj5u.com 2020-09-10 02:00:47 more
  • 一、SQLMAP入門

    一、SQLMAP入門 1、判斷是否存在注入 sqlmap.py -u 網址/id=1 id=1不可缺少。當注入點后面的引數大于兩個時。需要加雙引號, sqlmap.py -u "網址/id=1&uid=1" 2、判斷文本中的請求是否存在注入 從文本中加載http請求,SQLMAP可以從一個文本檔案中 ......

    uj5u.com 2020-09-10 02:00:50 more
  • Metasploit 簡單使用教程

    metasploit 簡單使用教程 浩先生, 2020-08-28 16:18:25 分類專欄: kail 網路安全 linux 文章標簽: linux資訊安全 編輯 著作權 metasploit 使用教程 前言 一、Metasploit是什么? 二、準備作業 三、具體步驟 前言 Msfconsole ......

    uj5u.com 2020-09-10 02:00:53 more
  • 游戲逆向之驅動層與用戶層通訊

    驅動層代碼: #pragma once #include <ntifs.h> #define add_code CTL_CODE(FILE_DEVICE_UNKNOWN,0x800,METHOD_BUFFERED,FILE_ANY_ACCESS) /* 更多游戲逆向視頻www.yxfzedu.com ......

    uj5u.com 2020-09-10 02:00:56 more
  • 北斗電力時鐘(北斗授時服務器)讓網路資料更精準

    北斗電力時鐘(北斗授時服務器)讓網路資料更精準 北斗電力時鐘(北斗授時服務器)讓網路資料更精準 京準電子科技官微——ahjzsz 近幾年,資訊技術的得了快速發展,互聯網在逐漸普及,其在人們生活和生產中都得到了廣泛應用,并且取得了不錯的應用效果。計算機網路資訊在電力系統中的應用,一方面使電力系統的運行 ......

    uj5u.com 2020-09-10 02:01:03 more
  • 【CTF】CTFHub 技能樹 彩蛋 writeup

    ?碎碎念 CTFHub:https://www.ctfhub.com/ 筆者入門CTF時時剛開始刷的是bugku的舊平臺,后來才有了CTFHub。 感覺不論是網頁UI設計,還是題目質量,賽事跟蹤,工具軟體都做得很不錯。 而且因為獨到的金幣制度的確讓人有一種想去刷題賺金幣的感覺。 個人還是非常喜歡這個 ......

    uj5u.com 2020-09-10 02:04:05 more
  • 02windows基礎操作

    我學到了一下幾點 Windows系統目錄結構與滲透的作用 常見Windows的服務詳解 Windows埠詳解 常用的Windows注冊表詳解 hacker DOS命令詳解(net user / type /md /rd/ dir /cd /net use copy、批處理 等) 利用dos命令制作 ......

    uj5u.com 2020-09-10 02:04:18 more
  • 03.Linux基礎操作

    我學到了以下幾點 01Linux系統介紹02系統安裝,密碼啊破解03Linux常用命令04LAMP 01LINUX windows: win03 8 12 16 19 配置不繁瑣 Linux:redhat,centos(紅帽社區版),Ubuntu server,suse unix:金融機構,證券,銀 ......

    uj5u.com 2020-09-10 02:04:30 more
  • 05HTML

    01HTML介紹 02頭部標簽講解03基礎標簽講解04表單標簽講解 HTML前段語言 js1.了解代碼2.根據代碼 懂得挖掘漏洞 (POST注入/XSS漏洞上傳)3.黑帽seo 白帽seo 客戶網站被黑帽植入劫持代碼如何處理4.熟悉html表單 <html><head><title>TDK標題,描述 ......

    uj5u.com 2020-09-10 02:04:36 more
最新发布
  • 2023年最新微信小程式抓包教程

    01 開門見山 隔一個月發一篇文章,不過分。 首先回顧一下《微信系結手機號資料庫被脫庫事件》,我也是第一時間得知了這個訊息,然后跟蹤了整件事情的經過。下面是這起事件的相關截圖以及近日流出的一萬條資料樣本: 個人認為這件事也沒什么,還不如關注一下之前45億快遞資料查詢渠道疑似在近日復活的訊息。 訊息是 ......

    uj5u.com 2023-04-20 08:48:24 more
  • web3 產品介紹:metamask 錢包 使用最多的瀏覽器插件錢包

    Metamask錢包是一種基于區塊鏈技術的數字貨幣錢包,它允許用戶在安全、便捷的環境下管理自己的加密資產。Metamask錢包是以太坊生態系統中最流行的錢包之一,它具有易于使用、安全性高和功能強大等優點。 本文將詳細介紹Metamask錢包的功能和使用方法。 一、 Metamask錢包的功能 數字資 ......

    uj5u.com 2023-04-20 08:47:46 more
  • vulnhub_Earth

    前言 靶機地址->>>vulnhub_Earth 攻擊機ip:192.168.20.121 靶機ip:192.168.20.122 參考文章 https://www.cnblogs.com/Jing-X/archive/2022/04/03/16097695.html https://www.cnb ......

    uj5u.com 2023-04-20 07:46:20 more
  • 從4k到42k,軟體測驗工程師的漲薪史,給我看哭了

    清明節一過,盲猜大家已經無心上班,在數著日子準備過五一,但一想到銀行卡里的余額……瞬間心情就不美麗了。最近,2023年高校畢業生就業調查顯示,本科畢業月平均起薪為5825元。調查一出,便有很多同學表示自己又被平均了。看著這一資料,不免讓人想到前不久中國青年報的一項調查:近六成大學生認為畢業10年內會 ......

    uj5u.com 2023-04-20 07:44:00 more
  • 最新版本 Stable Diffusion 開源 AI 繪畫工具之中文自動提詞篇

    🎈 標簽生成器 由于輸入正向提示詞 prompt 和反向提示詞 negative prompt 都是使用英文,所以對學習母語的我們非常不友好 使用網址:https://tinygeeker.github.io/p/ai-prompt-generator 這個網址是為了讓大家在使用 AI 繪畫的時候 ......

    uj5u.com 2023-04-20 07:43:36 more
  • 漫談前端自動化測驗演進之路及測驗工具分析

    隨著前端技術的不斷發展和應用程式的日益復雜,前端自動化測驗也在不斷演進。隨著 Web 應用程式變得越來越復雜,自動化測驗的需求也越來越高。如今,自動化測驗已經成為 Web 應用程式開發程序中不可或缺的一部分,它們可以幫助開發人員更快地發現和修復錯誤,提高應用程式的性能和可靠性。 ......

    uj5u.com 2023-04-20 07:43:16 more
  • CANN開發實踐:4個DVPP記憶體問題的典型案例解讀

    摘要:由于DVPP媒體資料處理功能對存放輸入、輸出資料的記憶體有更高的要求(例如,記憶體首地址128位元組對齊),因此需呼叫專用的記憶體申請介面,那么本期就分享幾個關于DVPP記憶體問題的典型案例,并給出原因分析及解決方法。 本文分享自華為云社區《FAQ_DVPP記憶體問題案例》,作者:昇騰CANN。 DVPP ......

    uj5u.com 2023-04-20 07:43:03 more
  • msf學習

    msf學習 以kali自帶的msf為例 一、msf核心模塊與功能 msf模塊都放在/usr/share/metasploit-framework/modules目錄下 1、auxiliary 輔助模塊,輔助滲透(埠掃描、登錄密碼爆破、漏洞驗證等) 2、encoders 編碼器模塊,主要包含各種編碼 ......

    uj5u.com 2023-04-20 07:42:59 more
  • Halcon軟體安裝與界面簡介

    1. 下載Halcon17版本到到本地 2. 雙擊安裝包后 3. 步驟如下 1.2 Halcon軟體安裝 界面分為四大塊 1. Halcon的五個助手 1) 影像采集助手:與相機連接,設定相機引數,采集影像 2) 標定助手:九點標定或是其它的標定,生成標定檔案及內參外參,可以將像素單位轉換為長度單位 ......

    uj5u.com 2023-04-20 07:42:17 more
  • 在MacOS下使用Unity3D開發游戲

    第一次發博客,先發一下我的游戲開發環境吧。 去年2月份買了一臺MacBookPro2021 M1pro(以下簡稱mbp),這一年來一直在用mbp開發游戲。我大致分享一下我的開發工具以及使用體驗。 1、Unity 官網鏈接: https://unity.cn/releases 我一般使用的Apple ......

    uj5u.com 2023-04-20 07:40:19 more