|
22 | 22 | import com.dtstack.flink.sql.side.BaseSideInfo; |
23 | 23 | import com.dtstack.flink.sql.side.rdb.table.RdbSideTableInfo; |
24 | 24 | import com.dtstack.flink.sql.side.rdb.util.SwitchUtil; |
| 25 | +import com.dtstack.flink.sql.util.JDBCUtils; |
25 | 26 | import com.dtstack.flink.sql.util.RowDataComplete; |
26 | 27 | import com.dtstack.flink.sql.util.RowDataConvert; |
27 | 28 | import com.google.common.collect.Lists; |
@@ -125,36 +126,26 @@ public void flatMap(Row value, Collector<BaseRow> out) throws Exception { |
125 | 126 | } |
126 | 127 |
|
127 | 128 | private void loadData(Map<String, List<Map<String, Object>>> tmpCache) throws SQLException { |
128 | | - RdbSideTableInfo tableInfo = (RdbSideTableInfo) sideInfo.getSideTableInfo(); |
129 | | - Connection connection = null; |
| 129 | + queryAndFillData(tmpCache, getConnectionWithRetry((RdbSideTableInfo) sideInfo.getSideTableInfo())); |
| 130 | + } |
130 | 131 |
|
131 | | - try { |
132 | | - for (int i = 0; i < CONN_RETRY_NUM; i++) { |
| 132 | + private Connection getConnectionWithRetry(RdbSideTableInfo tableInfo) throws SQLException { |
| 133 | + String connInfo = "url:" + tableInfo.getUrl() + "; userName:" + tableInfo.getUserName(); |
| 134 | + String errorMsg = null; |
| 135 | + for (int i = 0; i < CONN_RETRY_NUM; i++) { |
| 136 | + try { |
| 137 | + return getConn(tableInfo.getUrl(), tableInfo.getUserName(), tableInfo.getPassword()); |
| 138 | + } catch (Exception e) { |
133 | 139 | try { |
134 | | - connection = getConn(tableInfo.getUrl(), tableInfo.getUserName(), tableInfo.getPassword()); |
135 | | - break; |
136 | | - } catch (Exception e) { |
137 | | - if (i == CONN_RETRY_NUM - 1) { |
138 | | - throw new RuntimeException("", e); |
139 | | - } |
140 | | - try { |
141 | | - String connInfo = "url:" + tableInfo.getUrl() + ";userName:" + tableInfo.getUserName() + ",pwd:" + tableInfo.getPassword(); |
142 | | - LOG.warn("get conn fail, wait for 5 sec and try again, connInfo:" + connInfo); |
143 | | - Thread.sleep(5 * 1000); |
144 | | - } catch (InterruptedException e1) { |
145 | | - LOG.error("", e1); |
146 | | - } |
| 140 | + LOG.warn("get conn fail, wait for 5 sec and try again, connInfo:" + connInfo); |
| 141 | + errorMsg = e.getCause().toString(); |
| 142 | + Thread.sleep(5 * 1000); |
| 143 | + } catch (InterruptedException e1) { |
| 144 | + LOG.error("", e1); |
147 | 145 | } |
148 | 146 | } |
149 | | - queryAndFillData(tmpCache, connection); |
150 | | - } catch (Exception e) { |
151 | | - LOG.error("", e); |
152 | | - throw new SQLException(e); |
153 | | - } finally { |
154 | | - if (connection != null) { |
155 | | - connection.close(); |
156 | | - } |
157 | 147 | } |
| 148 | + throw new SQLException("get conn fail. connInfo: " + connInfo + "\ncause by: " + errorMsg); |
158 | 149 | } |
159 | 150 |
|
160 | 151 | private void queryAndFillData(Map<String, List<Map<String, Object>>> tmpCache, Connection connection) throws SQLException { |
@@ -188,6 +179,7 @@ private void queryAndFillData(Map<String, List<Map<String, Object>>> tmpCache, C |
188 | 179 | tmpCache.computeIfAbsent(cacheKey, key -> Lists.newArrayList()) |
189 | 180 | .add(oneRow); |
190 | 181 | } |
| 182 | + JDBCUtils.closeConnectionResource(resultSet, statement, connection, false); |
191 | 183 | } |
192 | 184 |
|
193 | 185 | public int getFetchSize() { |
|
0 commit comments