summaryrefslogtreecommitdiff
path: root/src/main/java/cn/ac/iie/dao/DbConnect.java
diff options
context:
space:
mode:
authorcaohui <[email protected]>2020-04-29 14:32:05 +0800
committercaohui <[email protected]>2020-04-29 14:32:05 +0800
commitd15d7536f385ec4a1250ed15ed52fd6c05eb7431 (patch)
tree737ec8462ef62ac70caeee1533cbee4e76ceef98 /src/main/java/cn/ac/iie/dao/DbConnect.java
VoIP Knowledge Base sip-voip-completion Initial commit 202004291431HEADmaster
Diffstat (limited to 'src/main/java/cn/ac/iie/dao/DbConnect.java')
-rw-r--r--src/main/java/cn/ac/iie/dao/DbConnect.java102
1 files changed, 102 insertions, 0 deletions
diff --git a/src/main/java/cn/ac/iie/dao/DbConnect.java b/src/main/java/cn/ac/iie/dao/DbConnect.java
new file mode 100644
index 0000000..0635e04
--- /dev/null
+++ b/src/main/java/cn/ac/iie/dao/DbConnect.java
@@ -0,0 +1,102 @@
+package cn.ac.iie.dao;
+
+
+import com.alibaba.druid.pool.DruidDataSource;
+import com.alibaba.druid.pool.DruidPooledConnection;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.util.Properties;
+
+/**
+ * Druid连接信息
+ *
+ * @author antlee
+ * @date 2018/8/20
+ */
+public class DbConnect {
+ private static DruidDataSource dataSource = null;
+ private static DbConnect dbConnect = null;
+ private static Properties props = new Properties();
+
+ static {
+ getDbConnect();
+ }
+
+ private static void getDbConnect() {
+ try {
+ if (dataSource == null) {
+ dataSource = new DruidDataSource();
+ props.load(DbConnect.class.getClassLoader().getResourceAsStream("clickhouse.properties"));
+ //设置连接参数
+ dataSource.setUrl("jdbc:clickhouse://" + props.getProperty("db.id"));
+ dataSource.setDriverClassName(props.getProperty("drivers"));
+ dataSource.setUsername(props.getProperty("mdb.user"));
+ dataSource.setPassword(props.getProperty("mdb.password"));
+ //配置初始化大小、最小、最大
+ dataSource.setInitialSize(Integer.parseInt(props.getProperty("initialsize")));
+ dataSource.setMinIdle(Integer.parseInt(props.getProperty("minidle")));
+ dataSource.setMaxActive(Integer.parseInt(props.getProperty("maxactive")));
+ //连接泄漏监测
+ dataSource.setRemoveAbandoned(true);
+ dataSource.setRemoveAbandonedTimeout(30);
+ dataSource.setDefaultAutoCommit(false);
+ //配置获取连接等待超时的时间
+ dataSource.setMaxWait(30000);
+ //配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
+ dataSource.setTimeBetweenEvictionRunsMillis(2000);
+ //防止过期
+ dataSource.setValidationQuery("SELECT 1");
+ dataSource.setTestWhileIdle(true);
+ dataSource.setTestOnBorrow(true);
+ dataSource.setKeepAlive(true);
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+
+ }
+ }
+
+ /**
+ * 数据库连接池单例
+ *
+ * @return dbConnect
+ */
+ public static synchronized DbConnect getInstance() {
+ if (null == dbConnect) {
+ dbConnect = new DbConnect();
+ }
+ return dbConnect;
+ }
+
+ /**
+ * 返回druid数据库连接
+ *
+ * @return 连接
+ * @throws SQLException sql异常
+ */
+ public DruidPooledConnection getConnection() throws SQLException {
+ return dataSource.getConnection();
+ }
+
+ /**
+ * 清空PreparedStatement、Connection对象,未定义的置空。
+ *
+ * @param pstmt PreparedStatement对象
+ * @param connection Connection对象
+ */
+ public void clear(PreparedStatement pstmt, Connection connection) {
+ try {
+ if (pstmt != null) {
+ pstmt.close();
+ }
+ if (connection != null) {
+ connection.close();
+ }
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+
+ }
+} \ No newline at end of file