使用druid数据池连接数据库

1.需要导入的包

com.alibaba
druid
1.1.12

2.具体程序实现
package com.zlkj.data.portals.utils;

import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.hibernate.service.spi.ServiceException;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.stat.DruidDataSourceStatManager;
import com.zlkj.data.portals.request.dataSource.TestDataSourceRequest;
import lombok.extern.slf4j.Slf4j;

/**

  • 连接池相关系统监测

  • @author admin
    */
    @Slf4j
    public class DataSourceTools {

    /**

    • 获取当前连接池信息
    • @param dsName
    • @return
      */
      public static DruidDataSource getDataSource(String dsName) {
      DruidDataSource ds = null;
      for (DruidDataSource datasource : DruidDataSourceStatManager.getDruidDataSourceInstances()) {
      if (dsName.equals(datasource.getName())) {
      ds = datasource;
      break;
      }
      }
      return ds;
      }

    /**

    • 获取当前连接池信息
    • @param dsName
    • @return
      */
      public static Map<String, Object> getDataSourceStat(String dsName) {
      DruidDataSource ds = getDataSource(dsName);
      return ds != null ? ds.getStatData() : new HashMap<String, Object>();
      }

    /**

    • 创建数据库连接池

    • @param testDataSourceRequest

    • @return

    • @throws SQLException

    • @throws NoSuchAlgorithmException
      */
      public static DruidDataSource createDataSource(TestDataSourceRequest testDataSourceRequest) throws SQLException, NoSuchAlgorithmException {
      DruidDataSource druidDataSource = new DruidDataSource();

      //定义数据池中数据源名称
      String dsName = testDataSourceRequest.getDataSourceType() + testDataSourceRequest.getAddress() + testDataSourceRequest.getPort() + testDataSourceRequest.getDataBaseName()
      + testDataSourceRequest.getUserName() + testDataSourceRequest.getUserPassword();
      Connection conn = null;
      try {
      if (testDataSourceRequest.getDataSourceType().equals(“mysql”)) {
      druidDataSource.setDriverClassName(“com.mysql.jdbc.Driver”);
      }
      String url = “jdbc:” + testDataSourceRequest.getDataSourceType() + “?/” + testDataSourceRequest.getAddress() + “:” + testDataSourceRequest.getPort() + “/” + testDataSourceRequest.getDataBaseName() + “?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false”;
      String username = testDataSourceRequest.getUserName();
      String password = testDataSourceRequest.getUserPassword();
      druidDataSource.setName(dsName);
      druidDataSource.setUrl(url);
      druidDataSource.setUsername(username);
      druidDataSource.setPassword(password);

       druidDataSource.setInitialSize(5);
       druidDataSource.setMinIdle(5);
       druidDataSource.setMaxActive(5);
       druidDataSource.setMaxWait(6000);
       druidDataSource.setMinEvictableIdleTimeMillis(300000);
       druidDataSource.setRemoveAbandoned(false);//超过时间限制是否回收
       druidDataSource.setRemoveAbandonedTimeout(180);//超过时间限制多长
       druidDataSource.setTimeBetweenEvictionRunsMillis(60000);//配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
       druidDataSource.setTestWhileIdle(true);
       druidDataSource.setTestOnBorrow(false);
       druidDataSource.setTestOnReturn(false);
       druidDataSource.setPoolPreparedStatements(true);
       druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(20);
       druidDataSource.setTimeBetweenLogStatsMillis(3600000);
       druidDataSource.addConnectionProperty("remarksReporting", "true");
       conn = druidDataSource.getConnection(username, password);
      

      } catch (SQLException e) {
      closeDataSource(dsName);
      e.printStackTrace();
      throw new ServiceException(“连接数据源失败 : {}” + e.getMessage());
      } finally {
      try {
      if (conn != null) {
      conn.close();
      }
      } catch (SQLException e) {
      closeDataSource(dsName);
      e.printStackTrace();
      throw new ServiceException(“连接数据源关闭失败 : {}” + e.getMessage());
      }
      }
      return druidDataSource;
      }

    /**

    • 关闭数据库连接池
    • @param dsName
      */
      public static void closeDataSource(String dsName) {
      if (getDataSource(dsName) != null) {
      getDataSource(dsName).close();
      }
      }
      }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大风吱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值