主頁 > 區塊鏈 > Mysql Connector/J 原始碼分析(LoadBalance)

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

2020-10-04 10:37:41 區塊鏈

文章目錄

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

五、實用性分析

動態代理連接在使用中遇到例外能夠被呼叫方即時感知到,這使得loadbalance模式具有使用的價值,故名思義,此模式還能起到負載均衡的效果,用戶只需要在url上多配置幾組ip:port(mysql的服務器得跟上), 這樣就可以均衡各臺資料庫服務器的壓力,至少系統進行極力測驗時可以輕松過關_
Loadbalance模式是依賴Strategy組件來分配任務到某一組ip:port對應的Mysql服務器上,而不并會區分任務是寫操作還是讀操作,所以這就要求Mysql服務器間必須具備資料同步功能,以保證各臺服務器上的資料是一致的,而且需要實作強一致性,否則無法達到事務隔離的Repeatable級別的要求,

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

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

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

小結

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

六、總結

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

  • 呼叫者在使用程序中能夠感知到底層發生的例外,另外呼叫者只需構造一個動態代理連接即可,
  • 呼叫者在使用程序中遇到例外,處理方法與使用普通的Connection一樣,使用者如果想從普通的Connection升級到Loadbalance模式,在應用層面只需要修改url,無需改代碼,
  • 可擴展性強,用戶發現資料庫資源占用高,可以多安排設備,然后在url里添加更多的ip:port即可,
  • Loadbalance模式強調的是壓力的均衡,所以需要部署多臺Mysql服務器,另外,當要執行一批的SQL 命令時,前后命令有可能會分配給不同的Mysql服務器執行,所以這里存在資料同步問題,所以使用此模式,需要掌握Mysql多機的部署知識,而且必須要將資料的一致性放在最首要的位置來考慮,
  • 高可用性,根據上一點,使用好Loadbalance模式,無形中實作了資料的高可用性,
  • 穩定性,因為將執行任務安排到不同的Mysql服務器上,降低各臺服務器的資源占用,從而為整個應用系統的穩定運行起到了一定的作用,

Loadbalace模式以將任務分配到不同的服務器上執行作為手段,實作整個資料庫系統的穩定運行,這種模式強調的是穩定性,如果用戶對回應速度要求極高,這種模式就不能勝任了,后續文章將介紹replication模式,看看它在提高回應速度方面有何設計良方,

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/154135.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)

熱門瀏覽
  • JAVA使用 web3j 進行token轉賬

    最近新學習了下區塊鏈這方面的知識,所學不多,給大家分享下。 # 1. 關于web3j web3j是一個高度模塊化,反應性,型別安全的Java和Android庫,用于與智能合約配合并與以太坊網路上的客戶端(節點)集成。 # 2. 準備作業 jdk版本1.8 引入maven <dependency> < ......

    uj5u.com 2020-09-10 03:03:06 more
  • 以太坊智能合約開發框架Truffle

    前言 部署智能合約有多種方式,命令列的瀏覽器的渠道都有,但往往跟我們程式員的風格不太相符,因為我們習慣了在IDE里寫了代碼然后打包運行看效果。 雖然現在IDE中已經存在了Solidity插件,可以撰寫智能合約,但是部署智能合約卻要另走他路,沒辦法進行一個快捷的部署與測驗。 如果團隊管理的區塊節點多、 ......

    uj5u.com 2020-09-10 03:03:12 more
  • 谷歌二次驗證碼成為區塊鏈專用安全碼,你怎么看?

    前言 谷歌身份驗證器,前些年大家都比較陌生,但隨著國內互聯網安全的加強,它越來越多地出現在大家的視野中。 比較廣泛接觸的人群是國際3A游戲愛好者,游戲盜號現象嚴重+國外賬號安全應用廣泛,這類游戲一般都會要求用戶系結名為“兩步驗證”、“雙重驗證”等,平臺一般都推薦用谷歌身份驗證器。 后來區塊鏈業務風靡 ......

    uj5u.com 2020-09-10 03:03:17 more
  • 密碼學DAY1

    目錄 ##1.1 密碼學基本概念 密碼在我們的生活中有著重要的作用,那么密碼究竟來自何方,為何會產生呢? 密碼學是網路安全、資訊安全、區塊鏈等產品的基礎,常見的非對稱加密、對稱加密、散列函式等,都屬于密碼學范疇。 密碼學有數千年的歷史,從最開始的替換法到如今的非對稱加密演算法,經歷了古典密碼學,近代密 ......

    uj5u.com 2020-09-10 03:03:50 more
  • 密碼學DAY1_02

    目錄 ##1.1 ASCII編碼 ASCII(American Standard Code for Information Interchange,美國資訊交換標準代碼)是基于拉丁字母的一套電腦編碼系統,主要用于顯示現代英語和其他西歐語言。它是現今最通用的單位元組編碼系統,并等同于國際標準ISO/IE ......

    uj5u.com 2020-09-10 03:04:50 more
  • 密碼學DAY2

    ##1.1 加密模式 加密模式:https://docs.oracle.com/javase/8/docs/api/javax/crypto/Cipher.html ECB ECB : Electronic codebook, 電子密碼本. 需要加密的訊息按照塊密碼的塊大小被分為數個塊,并對每個塊進 ......

    uj5u.com 2020-09-10 03:05:42 more
  • NTP時鐘服務器的特點(京準電子)

    NTP時鐘服務器的特點(京準電子) NTP時鐘服務器的特點(京準電子) 京準電子官V——ahjzsz 首先對時間同步進行了背景介紹,然后討論了不同的時間同步網路技術,最后指出了建立全球或區域時間同步網存在的問題。 一、概 述 在通信領域,“同步”概念是指頻率的同步,即網路各個節點的時鐘頻率和相位同步 ......

    uj5u.com 2020-09-10 03:05:47 more
  • 標準化考場時鐘同步系統推進智能化校園建設

    標準化考場時鐘同步系統推進智能化校園建設 標準化考場時鐘同步系統推進智能化校園建設 安徽京準電子科技官微——ahjzsz 一、背景概述隨著教育事業的快速發展,學校建設如雨后春筍,隨之而來的學校教育、管理、安全方面的問題成了學校管理人員面臨的最大的挑戰,這些問題同時也是學生家長所擔心的。為了讓學生有更 ......

    uj5u.com 2020-09-10 03:05:51 more
  • 位元幣入門

    引言 位元幣基本結構 位元幣基礎知識 1)哈希演算法 2)非對稱加密技術 3)數字簽名 4)MerkleTree 5)哪有位元幣,有的是UTXO 6)位元幣挖礦與共識 7)區塊驗證(共識) 總結 引言 上一篇我們已經知道了什么是區塊鏈,此篇說一下區塊鏈的第一個應用——位元幣。其實先有位元幣,后有的區塊 ......

    uj5u.com 2020-09-10 03:06:15 more
  • 北斗對時服務器(北斗對時設備)電力系統應用

    北斗對時服務器(北斗對時設備)電力系統應用 北斗對時服務器(北斗對時設備)電力系統應用 京準電子科技官微(ahjzsz) 中國北斗衛星導航系統(英文名稱:BeiDou Navigation Satellite System,簡稱BDS),因為是目前世界范圍內唯一可以大面積提供免費定位服務的系統,所以 ......

    uj5u.com 2020-09-10 03:06:20 more
最新发布
  • web3 產品介紹:metamask 錢包 使用最多的瀏覽器插件錢包

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

    uj5u.com 2023-04-20 08:46:47 more
  • Hyperledger Fabric 使用 CouchDB 和復雜智能合約開發

    在上個實驗中,我們已經實作了簡單智能合約實作及客戶端開發,但該實驗中智能合約只有基礎的增刪改查功能,且其中的資料管理功能與傳統 MySQL 比相差甚遠。本文將在前面實驗的基礎上,將 Hyperledger Fabric 的默認資料庫支持 LevelDB 改為 CouchDB 模式,以實作更復雜的資料... ......

    uj5u.com 2023-04-16 07:28:31 more
  • .NET Core 波場鏈離線簽名、廣播交易(發送 TRX和USDT)筆記

    Get Started NuGet You can run the following command to install the Tron.Wallet.Net in your project. PM> Install-Package Tron.Wallet.Net 配置 public reco ......

    uj5u.com 2023-04-14 08:08:00 more
  • DKP 黑客分析——不正確的代幣對比率計算

    概述: 2023 年 2 月 8 日,針對 DKP 協議的閃電貸攻擊導致該協議的用戶損失了 8 萬美元,因為 execute() 函式取決于 USDT-DKP 對中兩種代幣的余額比率。 智能合約黑客概述: 攻擊者的交易:0x0c850f,0x2d31 攻擊者地址:0xF38 利用合同:0xf34ad ......

    uj5u.com 2023-04-07 07:46:09 more
  • Defi開發簡介

    Defi開發簡介 介紹 Defi是去中心化金融的縮寫, 是一項旨在利用區塊鏈技術和智能合約創建更加開放,可訪問和透明的金融體系的運動. 這與傳統金融形成鮮明對比,傳統金融通常由少數大型銀行和金融機構控制 在Defi的世界里,用戶可以直接從他們的電腦或移動設備上訪問廣泛的金融服務,而不需要像銀行或者信 ......

    uj5u.com 2023-04-05 08:01:34 more
  • solidity簡單的ERC20代幣實作

    // SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.7.0 <0.9.0; import "hardhat/console.sol"; //ERC20 同質化代幣,每個代幣的本質或性質都是相同 //ETH 是原生代幣,它不是ERC20代幣, ......

    uj5u.com 2023-03-21 07:56:29 more
  • solidity 參考型別修飾符memory、calldata與storage 常量修飾符C

    在solidity語言中 參考型別修飾符(參考型別為存盤空間不固定的數值型別) memory、calldata與storage,它們只能修飾參考型別變數,比如字串、陣列、位元組等... memory 適用于方法傳參、返參或在方法體內使用,使用完就會清除掉,釋放記憶體 calldata 僅適用于方法傳參 ......

    uj5u.com 2023-03-08 07:57:54 more
  • solidity注解標簽

    在solidity語言中 注釋符為// 注解符為/* 內容*/ 或者 是 ///內容 注解中含有這幾個標簽給予我們使用 @title 一個應該描述合約/介面的標題 contract, library, interface @author 作者的名字 contract, library, interf ......

    uj5u.com 2023-03-08 07:57:49 more
  • 評價指標:相似度、GAS消耗

    【代碼注釋自動生成方法綜述】 這些評測指標主要來自機器翻譯和文本總結等研究領域,可以評估候選文本(即基于代碼注釋自動方法而生成)和參考文本(即基于手工方式而生成)的相似度. BLEU指標^[^?88^^?^]^:其全稱是bilingual evaluation understudy.該指標是最早用于 ......

    uj5u.com 2023-02-23 07:27:39 more
  • 基于NOSTR協議的“公有制”版本的Twitter,去中心化社交軟體Damus

    最近,一個幽靈,Web3的幽靈,在網路游蕩,它叫Damus,這玩意詮釋了什么叫做病毒式營銷,滑稽的是,一個Web3產品卻在Web2的產品鏈上瘋狂傳銷,各方大佬紛紛為其背書,到底發生了什么?Damus的葫蘆里,賣的是什么藥? 注冊和簡單實用 很少有什么產品在用戶注冊環節會有什么噱頭,但Damus確實出 ......

    uj5u.com 2023-02-05 06:48:39 more