|
19 | 19 | package com.dtstack.flink.sql.core.rdb.util; |
20 | 20 |
|
21 | 21 | import com.dtstack.flink.sql.classloader.ClassLoaderManager; |
| 22 | +import com.dtstack.flink.sql.exception.ExceptionTrace; |
22 | 23 | import com.dtstack.flink.sql.util.ThreadUtil; |
23 | 24 | import com.google.common.base.Preconditions; |
24 | 25 | import org.apache.flink.runtime.execution.SuppressRestartsException; |
|
37 | 38 | * Date 2020-12-25 |
38 | 39 | * Company dtstack |
39 | 40 | */ |
40 | | -public class JdbcConnectUtil { |
| 41 | +public class JdbcConnectionUtil { |
41 | 42 | private static final int DEFAULT_RETRY_NUM = 3; |
42 | 43 | private static final long DEFAULT_RETRY_TIME_WAIT = 3L; |
43 | 44 | private static final int DEFAULT_VALID_TIME = 10; |
44 | | - private static final Logger LOG = LoggerFactory.getLogger(JdbcConnectUtil.class); |
| 45 | + private static final Logger LOG = LoggerFactory.getLogger(JdbcConnectionUtil.class); |
45 | 46 |
|
46 | 47 | /** |
47 | 48 | * 关闭连接资源 |
@@ -118,43 +119,49 @@ public static void rollBack(Connection conn) { |
118 | 119 | } |
119 | 120 |
|
120 | 121 | /** |
121 | | - * get connect from datasource and retry when failed. |
| 122 | + * get connection from datasource and retry when failed. |
122 | 123 | * |
123 | 124 | * @param driverName driver name for rdb datasource |
124 | 125 | * @param url connect url |
125 | 126 | * @param userName connect user name |
126 | 127 | * @param password password for user name |
127 | 128 | * @return a valid connection |
128 | 129 | */ |
129 | | - public static Connection getConnectWithRetry( |
130 | | - String driverName |
131 | | - , String url |
132 | | - , String userName |
133 | | - , String password) { |
134 | | - String errorMessage = "\nGet connect failed with properties: \nurl: " + url |
135 | | - + (Objects.isNull(userName) ? "" : "\nuserName: " + userName |
136 | | - + "\nerror message: "); |
137 | | - String errorCause = null; |
| 130 | + public static Connection getConnectionWithRetry(String driverName, |
| 131 | + String url, |
| 132 | + String userName, |
| 133 | + String password) { |
| 134 | + String message = "Get connection failed. " + |
| 135 | + "\nurl: [%s]" + |
| 136 | + "\nuserName: [%s]" + |
| 137 | + "\ncause: [%s]"; |
| 138 | + String errorCause; |
| 139 | + String errorMessage = ""; |
138 | 140 |
|
139 | | - ClassLoaderManager.forName(driverName, JdbcConnectUtil.class.getClassLoader()); |
| 141 | + ClassLoaderManager.forName(driverName, JdbcConnectionUtil.class.getClassLoader()); |
140 | 142 | Preconditions.checkNotNull(url, "url can't be null!"); |
141 | 143 |
|
142 | 144 | for (int i = 0; i < DEFAULT_RETRY_NUM; i++) { |
143 | 145 | try { |
144 | | - return Objects.isNull(userName) ? |
145 | | - DriverManager.getConnection(url) : DriverManager.getConnection(url, userName, password); |
| 146 | + Connection connection = |
| 147 | + Objects.isNull(userName) ? |
| 148 | + DriverManager.getConnection(url) : |
| 149 | + DriverManager.getConnection(url, userName, password); |
| 150 | + connection.setAutoCommit(false); |
| 151 | + return connection; |
146 | 152 | } catch (Exception e) { |
147 | | - if (Objects.isNull(e.getCause())) { |
148 | | - errorCause = e.getMessage(); |
149 | | - } else { |
150 | | - errorCause = e.getCause().toString(); |
151 | | - } |
152 | | - |
153 | | - LOG.warn(errorMessage + errorCause); |
| 153 | + errorCause = ExceptionTrace.traceOriginalCause(e); |
| 154 | + errorMessage = String.format( |
| 155 | + message, |
| 156 | + url, |
| 157 | + userName, |
| 158 | + errorCause |
| 159 | + ); |
| 160 | + LOG.warn(errorMessage); |
154 | 161 | LOG.warn("Connect will retry after [{}] s. Retry time [{}] ...", DEFAULT_RETRY_TIME_WAIT, i + 1); |
155 | 162 | ThreadUtil.sleepSeconds(DEFAULT_RETRY_TIME_WAIT); |
156 | 163 | } |
157 | 164 | } |
158 | | - throw new SuppressRestartsException(new Throwable(errorMessage + errorCause)); |
| 165 | + throw new SuppressRestartsException(new SQLException(errorMessage)); |
159 | 166 | } |
160 | 167 | } |
0 commit comments