文章目錄
前言 名詞定義 一、Loadbalance的邏輯結構 二、例外處理機制
三、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, 那么該方法中哪些時間點上會被呼叫呢?
構造動態代理連接j時,LoadBalancedConnectionProxy的建構式會呼叫該方法, 正式呼叫Method#invoke方法前有可能執行,請看上一節, 呼叫Method#invoke方法出現了通訊例外, 執行完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模式,看看它在提高回應速度方面有何設計良方,