2020import com .alibaba .druid .util .StringUtils ;
2121import com .google .common .collect .Lists ;
2222import lombok .extern .slf4j .Slf4j ;
23+
2324import javax .sql .DataSource ;
24- import java .io .*;
25+ import java .io .BufferedReader ;
26+ import java .io .File ;
27+ import java .io .FileInputStream ;
28+ import java .io .InputStreamReader ;
2529import java .nio .charset .StandardCharsets ;
2630import java .sql .*;
27- import java .util .HashMap ;
2831import java .util .List ;
29- import java .util .Map ;
3032
3133/**
3234 * @author /
@@ -36,20 +38,6 @@ public class SqlUtils {
3638
3739 public static final String COLON = ":" ;
3840
39- private static volatile Map <String , DruidDataSource > map = new HashMap <>();
40-
41- private static String getKey (String jdbcUrl , String username , String password ) {
42- StringBuilder sb = new StringBuilder ();
43- if (!StringUtils .isEmpty (username )) {
44- sb .append (username );
45- }
46- if (!StringUtils .isEmpty (password )) {
47- sb .append (COLON ).append (password );
48- }
49- sb .append (COLON ).append (jdbcUrl .trim ());
50-
51- return SecureUtil .md5 (sb .toString ());
52- }
5341
5442 /**
5543 * 获取数据源
@@ -60,55 +48,44 @@ private static String getKey(String jdbcUrl, String username, String password) {
6048 * @return DataSource
6149 */
6250 private static DataSource getDataSource (String jdbcUrl , String userName , String password ) {
63- String key = getKey (jdbcUrl , userName , password );
64- if (!map .containsKey (key ) || null == map .get (key )) {
65- DruidDataSource druidDataSource = new DruidDataSource ();
66-
67- String className ;
68- try {
69- className = DriverManager .getDriver (jdbcUrl .trim ()).getClass ().getName ();
70- } catch (SQLException e ) {
71- throw new RuntimeException ("Get class name error: =" + jdbcUrl );
72- }
73- if (StringUtils .isEmpty (className )) {
74- DataTypeEnum dataTypeEnum = DataTypeEnum .urlOf (jdbcUrl );
75- if (null == dataTypeEnum ) {
76- throw new RuntimeException ("Not supported data type: jdbcUrl=" + jdbcUrl );
77- }
78- druidDataSource .setDriverClassName (dataTypeEnum .getDriver ());
79- } else {
80- druidDataSource .setDriverClassName (className );
51+ DruidDataSource druidDataSource = new DruidDataSource ();
52+ String className ;
53+ try {
54+ className = DriverManager .getDriver (jdbcUrl .trim ()).getClass ().getName ();
55+ } catch (SQLException e ) {
56+ throw new RuntimeException ("Get class name error: =" + jdbcUrl );
57+ }
58+ if (StringUtils .isEmpty (className )) {
59+ DataTypeEnum dataTypeEnum = DataTypeEnum .urlOf (jdbcUrl );
60+ if (null == dataTypeEnum ) {
61+ throw new RuntimeException ("Not supported data type: jdbcUrl=" + jdbcUrl );
8162 }
63+ druidDataSource .setDriverClassName (dataTypeEnum .getDriver ());
64+ } else {
65+ druidDataSource .setDriverClassName (className );
66+ }
8267
8368
84- druidDataSource .setUrl (jdbcUrl );
85- druidDataSource .setUsername (userName );
86- druidDataSource .setPassword (password );
87- // 配置获取连接等待超时的时间
88- druidDataSource .setMaxWait (3000 );
89- // 配置初始化大小、最小、最大
90- druidDataSource .setInitialSize (1 );
91- druidDataSource .setMinIdle (1 );
92- druidDataSource .setMaxActive (1 );
93-
94- // 配置间隔多久才进行一次检测需要关闭的空闲连接,单位是毫秒
95- druidDataSource .setTimeBetweenEvictionRunsMillis (50000 );
96- // 配置一旦重试多次失败后等待多久再继续重试连接,单位是毫秒
97- druidDataSource .setTimeBetweenConnectErrorMillis (18000 );
98- // 配置一个连接在池中最小生存的时间,单位是毫秒
99- druidDataSource .setMinEvictableIdleTimeMillis (300000 );
100- // 这个特性能解决 MySQL 服务器8小时关闭连接的问题
101- druidDataSource .setMaxEvictableIdleTimeMillis (25200000 );
69+ druidDataSource .setUrl (jdbcUrl );
70+ druidDataSource .setUsername (userName );
71+ druidDataSource .setPassword (password );
72+ // 配置获取连接等待超时的时间
73+ druidDataSource .setMaxWait (3000 );
74+ // 配置初始化大小、最小、最大
75+ druidDataSource .setInitialSize (1 );
76+ druidDataSource .setMinIdle (1 );
77+ druidDataSource .setMaxActive (1 );
10278
103- try {
104- druidDataSource .init ( );
105- } catch ( SQLException e ) {
106- log . error ( "Exception during pool initialization" , e );
107- throw new RuntimeException ( e . getMessage ());
108- }
109- map . put ( key , druidDataSource );
79+ // 如果链接出现异常则直接判定为失败而不是一直重试
80+ druidDataSource .setBreakAfterAcquireFailure ( true );
81+ try {
82+ druidDataSource . init ( );
83+ } catch ( SQLException e ) {
84+ log . error ( "Exception during pool initialization" , e );
85+ throw new RuntimeException ( e . getMessage () );
11086 }
111- return map .get (key );
87+
88+ return druidDataSource ;
11289 }
11390
11491 private static Connection getConnection (String jdbcUrl , String userName , String password ) {
@@ -187,14 +164,14 @@ public static String executeFile(String jdbcUrl, String userName, String passwor
187164 * @param sqlList /
188165 */
189166 public static void batchExecute (Connection connection , List <String > sqlList ) throws SQLException {
190- Statement st = connection .createStatement ();
191- for (String sql : sqlList ) {
192- if (sql .endsWith (";" )) {
193- sql = sql .substring (0 , sql .length () - 1 );
194- }
195- st .addBatch (sql );
167+ Statement st = connection .createStatement ();
168+ for (String sql : sqlList ) {
169+ if (sql .endsWith (";" )) {
170+ sql = sql .substring (0 , sql .length () - 1 );
196171 }
197- st .executeBatch ();
172+ st .addBatch (sql );
173+ }
174+ st .executeBatch ();
198175 }
199176
200177 /**
0 commit comments