说到连接池,最常见的就是dbcp和c3p0,关于druid,官方定义是为监控而生的数据库连接池。
官方中文文档地址:
下面我介绍如何在servlet即不使用任何框架的时刻下使用druid并显示监控页面。
连接池之间的性能测试
1、首先导入下面这2个jar包
链接:https://pan.baidu.com/s/1e9lLS3e0xFdUFsPTu9rSQA 密码:xj6v
2、在web.xml文件中加入如下配置:
druidWebStatFilter com.alibaba.druid.support.http.WebStatFilter exclusions /public/*,*.js,*.css,/druid*,*.jsp,*.swf druidWebStatFilter /* DruidStatView com.alibaba.druid.support.http.StatViewServlet allow 127.0.0.1 DruidStatView /druid/*
3、在src下建一个db_server.properties的配置文件
内容如下,可根据自身需求调整
driverClassName=com.mysql.jdbc.Driverurl=jdbc:mysql:///kaoqin?useSSL=trueusername=rootpassword=rootfilters=statinitialSize=2maxActive=300maxWait=60000timeBetweenEvictionRunsMillis=60000minEvictableIdleTimeMillis=300000validationQuery=SELECT 1testWhileIdle=truetestOnBorrow=falsetestOnReturn=falsepoolPreparedStatements=falsemaxPoolPreparedStatementPerConnectionSize=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.class), name, password); return user; }}
7、浏览地址:${pageContext.request.contextPath}/druid/index.html
效果图: