|
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; |
28 | 29 | import com.google.common.collect.Maps; |
29 | 30 | import org.apache.calcite.sql.JoinType; |
30 | 31 | import org.apache.commons.collections.CollectionUtils; |
31 | 32 | import org.apache.commons.lang3.StringUtils; |
32 | | -import org.apache.flink.api.common.typeinfo.TypeInformation; |
33 | 33 | import org.apache.flink.configuration.Configuration; |
34 | 34 | import org.apache.flink.table.dataformat.BaseRow; |
35 | | -import org.apache.flink.table.typeutils.TimeIndicatorTypeInfo; |
36 | 35 | import org.apache.flink.types.Row; |
37 | 36 | import org.apache.flink.util.Collector; |
38 | 37 | import org.slf4j.Logger; |
39 | 38 | import org.slf4j.LoggerFactory; |
40 | 39 |
|
41 | | -import java.sql.*; |
42 | | -import java.time.LocalDateTime; |
| 40 | +import java.sql.Connection; |
| 41 | +import java.sql.ResultSet; |
| 42 | +import java.sql.SQLException; |
| 43 | +import java.sql.Statement; |
43 | 44 | import java.util.ArrayList; |
44 | 45 | import java.util.Calendar; |
45 | | -import java.util.HashMap; |
46 | 46 | import java.util.List; |
47 | 47 | import java.util.Map; |
48 | 48 | import java.util.Objects; |
@@ -128,36 +128,26 @@ public void flatMap(Row value, Collector<BaseRow> out) throws Exception { |
128 | 128 | } |
129 | 129 |
|
130 | 130 | private void loadData(Map<String, List<Map<String, Object>>> tmpCache) throws SQLException { |
131 | | - RdbSideTableInfo tableInfo = (RdbSideTableInfo) sideInfo.getSideTableInfo(); |
132 | | - Connection connection = null; |
| 131 | + queryAndFillData(tmpCache, getConnectionWithRetry((RdbSideTableInfo) sideInfo.getSideTableInfo())); |
| 132 | + } |
133 | 133 |
|
134 | | - try { |
135 | | - for (int i = 0; i < CONN_RETRY_NUM; i++) { |
| 134 | + private Connection getConnectionWithRetry(RdbSideTableInfo tableInfo) throws SQLException { |
| 135 | + String connInfo = "url:" + tableInfo.getUrl() + "; userName:" + tableInfo.getUserName(); |
| 136 | + String errorMsg = null; |
| 137 | + for (int i = 0; i < CONN_RETRY_NUM; i++) { |
| 138 | + try { |
| 139 | + return getConn(tableInfo.getUrl(), tableInfo.getUserName(), tableInfo.getPassword()); |
| 140 | + } catch (Exception e) { |
136 | 141 | try { |
137 | | - connection = getConn(tableInfo.getUrl(), tableInfo.getUserName(), tableInfo.getPassword()); |
138 | | - break; |
139 | | - } catch (Exception e) { |
140 | | - if (i == CONN_RETRY_NUM - 1) { |
141 | | - throw new RuntimeException("", e); |
142 | | - } |
143 | | - try { |
144 | | - String connInfo = "url:" + tableInfo.getUrl() + ";userName:" + tableInfo.getUserName() + ",pwd:" + tableInfo.getPassword(); |
145 | | - LOG.warn("get conn fail, wait for 5 sec and try again, connInfo:" + connInfo); |
146 | | - Thread.sleep(5 * 1000); |
147 | | - } catch (InterruptedException e1) { |
148 | | - LOG.error("", e1); |
149 | | - } |
| 142 | + LOG.warn("get conn fail, wait for 5 sec and try again, connInfo:" + connInfo); |
| 143 | + errorMsg = e.getCause().toString(); |
| 144 | + Thread.sleep(5 * 1000); |
| 145 | + } catch (InterruptedException e1) { |
| 146 | + LOG.error("", e1); |
150 | 147 | } |
151 | 148 | } |
152 | | - queryAndFillData(tmpCache, connection); |
153 | | - } catch (Exception e) { |
154 | | - LOG.error("", e); |
155 | | - throw new SQLException(e); |
156 | | - } finally { |
157 | | - if (connection != null) { |
158 | | - connection.close(); |
159 | | - } |
160 | 149 | } |
| 150 | + throw new SQLException("get conn fail. connInfo: " + connInfo + "\ncause by: " + errorMsg); |
161 | 151 | } |
162 | 152 |
|
163 | 153 | private void queryAndFillData(Map<String, List<Map<String, Object>>> tmpCache, Connection connection) throws SQLException { |
@@ -191,6 +181,7 @@ private void queryAndFillData(Map<String, List<Map<String, Object>>> tmpCache, C |
191 | 181 | tmpCache.computeIfAbsent(cacheKey, key -> Lists.newArrayList()) |
192 | 182 | .add(oneRow); |
193 | 183 | } |
| 184 | + JDBCUtils.closeConnectionResource(resultSet, statement, connection, false); |
194 | 185 | } |
195 | 186 |
|
196 | 187 | public int getFetchSize() { |
|
0 commit comments