inleft
2022-03-02 42b7d05dfdf60ee67c876084a3240ff48a3bf3a5
commit | author | age
9bcb19 1 /*
I 2 Copyright [2020] [https://www.xiaonuo.vip]
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8   http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
17
18 1.请不要删除和修改根目录下的LICENSE文件。
19 2.请不要删除和修改Snowy源码头部的版权声明。
20 3.请保留源码和相关描述文件的项目出处,作者声明等。
21 4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy
22 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy
23 6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
24  */
25 package vip.xiaonuo.core.pojo.druid;
26
27 import cn.hutool.log.Log;
28 import com.alibaba.druid.pool.DruidDataSource;
29 import lombok.Data;
30 import vip.xiaonuo.core.enums.DbIdEnum;
31
32 import java.sql.SQLException;
33 import java.util.Properties;
34
35 /**
36  * <p>数据库数据源配置</p>
37  * <p>说明:类中属性包含默认值的不要在这里修改,应该在"application.yml"中配置</p>
38  *
39  * @author yubaoshan
40  * @date 2017/5/21 11:18
41  */
42 @Data
43 public class DruidProperties {
44
45     private static final Log log = Log.get();
46
47     /**
48      * mysql校验语句
49      */
50     private final String MYSQL_VALIDATE_QUERY_SQL = "select 1";
51
52     /**
53      * oracle校验语句
54      */
55     private final String ORACLE_VALIDATE_QUERY_SQL = "select 1 from dual";
56
57     /**
58      * postgresql校验语句
59      */
60     private final String POSTGRESQL_VALIDATE_QUERY_SQL = "select version()";
61
62     /**
63      * sqlserver校验语句
64      */
65     private final String SQLSERVER_VALIDATE_QUERY_SQL = "select 1";
66
67     /**
68      * 达梦数据库校验语句
69      */
70     private final String DM_VALIDATE_QUERY_SQL = "select 1";
71
72     /**
73      * 人大金仓数据库校验语句
74      */
75     private final String KINGBASEES_VALIDATE_QUERY_SQL = "select 1";
76
77     private String url;
78
79     private String username;
80
81     private String password;
82
83     private String driverClassName;
84
85     private Integer initialSize = 2;
86
87     private Integer minIdle = 1;
88
89     private Integer maxActive = 20;
90
91     private Integer maxWait = 60000;
92
93     private Integer timeBetweenEvictionRunsMillis = 60000;
94
95     private Integer minEvictableIdleTimeMillis = 300000;
96
97     private String validationQuery;
98
99     private Boolean testWhileIdle = true;
100
101     private Boolean testOnBorrow = true;
102
103     private Boolean testOnReturn = true;
104
105     private Boolean poolPreparedStatements = true;
106
107     private Integer maxPoolPreparedStatementPerConnectionSize = 20;
108
109     private String filters = "stat,slf4j";
110
111     private String dataSourceName;
112
113     public void config(DruidDataSource dataSource) {
114
115         dataSource.setUrl(url);
116         dataSource.setUsername(username);
117         dataSource.setPassword(password);
118
119         dataSource.setDriverClassName(driverClassName);
120         //定义初始连接数
121         dataSource.setInitialSize(initialSize);
122         //最小空闲
123         dataSource.setMinIdle(minIdle);
124         //定义最大连接数
125         dataSource.setMaxActive(maxActive);
126         //最长等待时间
127         dataSource.setMaxWait(maxWait);
128
129         //配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
130         dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
131
132         //配置一个连接在池中最小生存的时间,单位是毫秒
133         dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
134         dataSource.setValidationQuery(getValidateQueryByUrl(url));
135         dataSource.setTestWhileIdle(testWhileIdle);
136         dataSource.setTestOnBorrow(testOnBorrow);
137         dataSource.setTestOnReturn(testOnReturn);
138
139         //打开PSCache,并且指定每个连接上PSCache的大小
140         dataSource.setPoolPreparedStatements(poolPreparedStatements);
141         dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
142
143         try {
144             dataSource.setFilters(filters);
145         } catch (SQLException e) {
146             log.error(">>> 数据库连接池初始化异常:{}", e.getMessage());
147         }
148     }
149
150     public Properties createProperties() {
151         Properties properties = new Properties();
152         properties.put("url", this.url);
153         properties.put("username", this.username);
154         properties.put("password", this.password);
155         properties.put("driverClassName", this.driverClassName);
156         properties.put("initialSize", this.initialSize);
157         properties.put("maxActive", this.maxActive);
158         properties.put("minIdle", this.minIdle);
159         properties.put("maxWait", this.maxWait);
160         properties.put("poolPreparedStatements", this.poolPreparedStatements);
161         properties.put("maxPoolPreparedStatementPerConnectionSize", this.maxPoolPreparedStatementPerConnectionSize);
162         properties.put("validationQuery", getValidateQueryByUrl(this.url));
163         properties.put("testOnBorrow", this.testOnBorrow);
164         properties.put("testOnReturn", this.testOnReturn);
165         properties.put("testWhileIdle", this.testWhileIdle);
166         properties.put("timeBetweenEvictionRunsMillis", this.timeBetweenEvictionRunsMillis);
167         properties.put("minEvictableIdleTimeMillis", this.minEvictableIdleTimeMillis);
168         properties.put("filters", this.filters);
169         return properties;
170     }
171
172     private String getValidateQueryByUrl(String url) {
173         if (url.contains(DbIdEnum.ORACLE.getName())) {
174             return ORACLE_VALIDATE_QUERY_SQL;
175         } else if (url.contains(DbIdEnum.PG_SQL.getName())) {
176             return POSTGRESQL_VALIDATE_QUERY_SQL;
177         } else if (url.contains(DbIdEnum.MS_SQL.getName())) {
178             return SQLSERVER_VALIDATE_QUERY_SQL;
179         } else if (url.contains(DbIdEnum.DM_SQL.getName())) {
180             return DM_VALIDATE_QUERY_SQL;
181         } else if (url.contains(DbIdEnum.KINGBASE_ES.getName())) {
182             return KINGBASEES_VALIDATE_QUERY_SQL;
183         } else {
184             return MYSQL_VALIDATE_QUERY_SQL;
185         }
186     }
187
188 }