zoukankan      html  css  js  c++  java
  • 初探Druid

    说到连接池,最常见的就是dbcp和c3p0,关于druid,官方定义是为监控而生的数据库连接池。

    官方中文文档地址:https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98

    下面我介绍如何在servlet即不使用任何框架的时刻下使用druid并显示监控页面。

    连接池之间的性能测试

    1、首先导入下面这2个jar包

    链接:https://pan.baidu.com/s/1e9lLS3e0xFdUFsPTu9rSQA 密码:xj6v

     

    2、在web.xml文件中加入如下配置:

    <filter>
            <filter-name>druidWebStatFilter</filter-name>
            <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>
            <init-param>
                <param-name>exclusions</param-name>
                <param-value>/public/*,*.js,*.css,/druid*,*.jsp,*.swf</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>druidWebStatFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        <servlet>
            <servlet-name>DruidStatView</servlet-name>
            <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
            <init-param>
                <param-name>allow</param-name>
                <param-value>127.0.0.1</param-value>
            </init-param>
        </servlet>
        <servlet-mapping>
            <servlet-name>DruidStatView</servlet-name>
            <url-pattern>/druid/*</url-pattern>
        </servlet-mapping>

    3、在src下建一个db_server.properties的配置文件

    内容如下,可根据自身需求调整

    driverClassName=com.mysql.jdbc.Driver
    url=jdbc:mysql:///kaoqin?useSSL=true
    username=root
    password=root
    filters=stat
    initialSize=2
    maxActive=300
    maxWait=60000
    timeBetweenEvictionRunsMillis=60000
    minEvictableIdleTimeMillis=300000
    validationQuery=SELECT 1
    testWhileIdle=true
    testOnBorrow=false
    testOnReturn=false
    poolPreparedStatements=false
    maxPoolPreparedStatementPerConnectionSize=200

    4、创建一个DruidConnection类

    public class DruidConnection {
        private static Properties properties = null;
        private static DataSource dataSource = null;
        private volatile static DruidConnection instatce = null;
        private Connection connection = null;
    
        //私有构造函数,防止实例化对象
        private DruidConnection() {
    
        }
    
    
        static {
            try {
                properties = new Properties();
                // 1.加载properties文件
                InputStream is = DruidConnection.class.getClassLoader().getResourceAsStream("db_server.properties");
    
                // 2.加载输入流
                properties.load(is);
    
                // 3.获取数据源
                dataSource = getDatasource();
    
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 用简单单例模式确保只返回一个链接对象
         * 
         * @return
         */
        public static  DruidConnection getInstace() {
            if(instatce == null) {
                synchronized (DruidConnection.class) {
                    if(instatce == null) {
                        instatce = new DruidConnection();
                    }
                }
            }
            return instatce;
        }
    
        // 返回一个数据源
        public DataSource getDataSource() {
            return dataSource;
        }
    
        // 返回一个链接
        public Connection getConnection() {
            try {
                connection = dataSource.getConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return connection;
        }
    
        // 加载数据源
        private static DataSource getDatasource() {
            DataSource source = null;
            try {
                source = DruidDataSourceFactory.createDataSource(properties);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return source;
        }
    }

    5、创建工具类DruidUtils

    public class DruidUtils {
    
        private static Connection connection = null;
        //获取元数据
        public static DataSource getDatasource() {
            DataSource dataSource = DruidConnection.getInstace().getDataSource();
            return dataSource;
        }
    
        //获取链接
        public static Connection getConnection() {
             connection = DruidConnection.getInstace().getConnection();
            return connection;
        }
    
        //归还资源
        public void release() {
            try {
                if(connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    6、在DAO层应用(举例)

    public class UserDao {
    
        public User login(String name,String password) throws SQLException {
            // TODO Auto-generated method stub
            QueryRunner runner = new QueryRunner(DruidUtils.getDatasource());
            String sql = "select * from user where name=? and password=?";
            User user = runner.query(sql, new BeanHandler<User>(User.class), name, password);
            return user;
        }
    
    }

    7、浏览地址:${pageContext.request.contextPath}/druid/index.html

    效果图:

  • 相关阅读:
    java 后端博客系统文章系统——No5
    啃碎并发(11):内存模型之重排序
    java 后端博客系统文章系统——No4
    啃碎并发(10):内存模型之内部原理
    java 后端博客系统文章系统——No3
    啃碎并发(九):内存模型之基础概述
    啃碎并发(八):深入分析wait&notify原理 猿码架构
    快速平方根倒数算法
    查找:哈希表
    注意力机制论文 --- Crop leaf disease recognition based on Self-Attention convolutional neural network
  • 原文地址:https://www.cnblogs.com/yg1024/p/9326518.html
Copyright © 2011-2022 走看看