3838 * Date 2020-12-25
3939 * Company dtstack
4040 */
41- public class JdbcConnectUtil {
41+ public class JdbcConnectionUtil {
4242 private static final int DEFAULT_RETRY_NUM = 3 ;
4343 private static final long DEFAULT_RETRY_TIME_WAIT = 3L ;
4444 private static final int DEFAULT_VALID_TIME = 10 ;
45- private static final Logger LOG = LoggerFactory .getLogger (JdbcConnectUtil .class );
45+ private static final Logger LOG = LoggerFactory .getLogger (JdbcConnectionUtil .class );
4646
4747 /**
4848 * 关闭连接资源
@@ -119,25 +119,26 @@ public static void rollBack(Connection conn) {
119119 }
120120
121121 /**
122- * get connect from datasource and retry when failed.
122+ * get connection from datasource and retry when failed.
123123 *
124124 * @param driverName driver name for rdb datasource
125125 * @param url connect url
126126 * @param userName connect user name
127127 * @param password password for user name
128128 * @return a valid connection
129129 */
130- public static Connection getConnectWithRetry (
131- String driverName
132- , String url
133- , String userName
134- , String password ) {
135- String errorMessage = "\n Get connect failed with properties: \n url: " + url
136- + (Objects .isNull (userName ) ? "" : "\n userName: " + userName
137- + "\n error message: " );
138- 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+ "\n url: [%s]" +
136+ "\n userName: [%s]" +
137+ "\n cause: [%s]" ;
138+ String errorCause ;
139+ String errorMessage = "" ;
139140
140- ClassLoaderManager .forName (driverName , JdbcConnectUtil .class .getClassLoader ());
141+ ClassLoaderManager .forName (driverName , JdbcConnectionUtil .class .getClassLoader ());
141142 Preconditions .checkNotNull (url , "url can't be null!" );
142143
143144 for (int i = 0 ; i < DEFAULT_RETRY_NUM ; i ++) {
@@ -150,11 +151,17 @@ public static Connection getConnectWithRetry(
150151 return connection ;
151152 } catch (Exception e ) {
152153 errorCause = ExceptionTrace .traceOriginalCause (e );
153- LOG .warn (errorMessage + errorCause );
154+ errorMessage = String .format (
155+ message ,
156+ url ,
157+ userName ,
158+ errorCause
159+ );
160+ LOG .warn (errorMessage );
154161 LOG .warn ("Connect will retry after [{}] s. Retry time [{}] ..." , DEFAULT_RETRY_TIME_WAIT , i + 1 );
155162 ThreadUtil .sleepSeconds (DEFAULT_RETRY_TIME_WAIT );
156163 }
157164 }
158- throw new SuppressRestartsException (new SQLException (errorMessage + errorCause ));
165+ throw new SuppressRestartsException (new SQLException (errorMessage ));
159166 }
160167}
0 commit comments