阿里Druid資料庫連線工具類

你別皺眉Chen發表於2020-10-09


本處使用了阿里的連線池

1.依賴匯入

    <!--mysql驅動-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.20</version>
    </dependency>
    <!--阿里連線池-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.23</version>
    </dependency>
    <!--日誌依賴-->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.2</version>
    </dependency>

2.druid配置檔案:

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://112.125.89.39/xiaomi?useSSL=false&characterEnding=utf8&serverTimezone=Asia/Shanghai
username=aa123456
password=123456
initialSize=10
maxActive=10
maxWait=60000
#配置間隔多久才進行一次檢測,檢測需要關閉的空閒連線,單位是毫秒
time-between-eviction-runs-millis=60000
#druid recycle
druid.removeAbandoned=true
druid.removeAbandonedTimeout=1000
druid.logAbandoned=false

3.資料庫連線工具類

package utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @Description:  連線池工具類
 * @Author one world
 * @Date 2020/9/19 0012 11:02
 */
public class ConnUtil {
    private static InputStream in;
    private static DataSource dataSource;
    private static final Logger logger = LoggerFactory.getLogger(ConnUtil.class);

    static {
        /**
         * 載入配置檔案,用阿里的工廠建立資料來源
         */
        in = ConnUtil.class.getResourceAsStream("/druid.properties");
        Properties properties = new Properties();
        try {
            properties.load(in);
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (IOException e) {
         throw new RuntimeException("連線工具類異常");
           logger.error("連線工具類異常",e);
        } catch (Exception e) {
        throw new RuntimeException("連線工具類異常");
            logger.error("連線工具類異常",e);
        }
    }

    /**
     * 資料庫連線的獲取
     *
     * @return Connection
     */
    public static Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
           throw new RuntimeException("連線工具類異常");
            logger.error("連線工具類getConnection方法異常",e);
        }
        return null;
    }

    /**
     * 資料來源獲取
     *
     * @return
     */
    public static DataSource getDataSource() {
        return dataSource;
    }

    /**
     * 資源關閉
     *
     * @param conn
     * @param statement
     * @param rs
     */
    public static void close(Connection conn, Statement statement, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
            throw new RuntimeException("連線工具類異常");
                logger.error("連線工具類關閉結果集異常",e);
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
            throw new RuntimeException("連線工具類異常");
                logger.error("連線工具類關閉statement異常",e);
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
            throw new RuntimeException("連線工具類異常");
                logger.error("連線工具類關閉連線異常",e);
            }
        }
    }

}

相關文章