栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

德鲁伊配置及使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

德鲁伊配置及使用

1--依赖


    mysql
    mysql-connector-java
    runtime



    com.alibaba
    druid
    1.1.20

2--连接参数

        

datasource:
  type: com.alibaba.druid.pool.DruidDataSource
  driverClassName: com.mysql.cj.jdbc.Driver
  url: jdbc:mysql://ip:端口/数据库名?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
  username: 账号
  password: 密码
  # 初始连接数
  initialSize: 5
  # 最小连接池数量
  minIdle: 10
  # 最大连接池数量
  maxActive: 100
  # 配置获取连接等待超时的时间
  maxWait: 60000
  # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
  timeBetweenEvictionRunsMillis: 60000
  # 配置一个连接在池中最小生存的时间,单位是毫秒
  minEvictableIdleTimeMillis: 300000
  # 配置一个连接在池中最大生存的时间,单位是毫秒
  maxEvictableIdleTimeMillis: 900000
  # 配置检测连接是否有效
  validationQuery: SELECt 1 FROM DUAL
  #申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
  testWhileIdle: true
  #配置从连接池获取连接时,是否检查连接有效性,true每次都检查;false不检查。做了这个配置会降低性能。
  testOnBorrow: false
  #配置向连接池归还连接时,是否检查连接有效性,true每次都检查;false不检查。做了这个配置会降低性能。
  testOnReturn: false
  #打开PsCache,并且指定每个连接上PSCache的大小
  poolPreparedStatements: true
  maxPoolPreparedStatementPerConnectionSize: 20
  #   配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
  filters: stat,wall,log4j
  #合并多个DruidDatasource的监控数据
  useGlobalDataSourceStat: true
  #通过connectProperties属性来打开mergesql功能罗慢sQL记录
  connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500;

形式二:

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://82.156.13.174:3306/history?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
username=root
password=Xs123456.
# 初始化连接数量
initialSize=10
minIdle=10
maxActive=20
# 最大等待时间
maxWait=3000

附:
批处理
Connection conn = null;
PreparedStatement preparedStatement = null;
try {
    conn = DruidUtil.getConnection();
    String sql = "insert into ref_role_menu values(?,?)";
    preparedStatement = conn.prepareStatement(sql);
    for (int i = 0; i < menuIdList.size(); i++) {
        preparedStatement.setLong(1, roleId);
        preparedStatement.setLong(2, menuIdList.get(i));
        preparedStatement.addBatch();
        if (i % 1000 == 0) {
            preparedStatement.executeBatch();
            preparedStatement.clearBatch();//清空批处理内容
        }
    }
    preparedStatement.executeBatch();
} catch (SQLException e) {
    e.printStackTrace();
} finally {
    DruidUtil.close(preparedStatement, conn);
}

3--工具类

public class DruidUtil {
    private static DataSource dataSource;
    
    static {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("src/main/java/com/util/druid.properties"));
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        getConnection();
    }

    public static Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    
    public static void close(Statement stmt, Connection conn) {
        close(null, stmt, conn);
    }

    public static void close(ResultSet rs, Statement stmt, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();//归还连接
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static DataSource getDataSource() {
        return dataSource;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/632098.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号