javalite 使用druid数据库连接池配置

  • 在pom文件中引入jar包
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>
  • 在web.xml中引入Durid的过滤器 DruidWebStatFilter
  <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>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value>
      </init-param>
      <init-param>
          <param-name>profileEnable</param-name>
          <param-value>true</param-value>
      </init-param>
      <init-param>
          <param-name>principalCookieName</param-name>
          <param-value>USER_COOKIE</param-value>
      </init-param>
      <init-param>
          <param-name>principalSessionName</param-name>
          <param-value>USER_SESSION</param-value>
      </init-param>
 </filter>
 <filter-mapping>
      <filter-name>DruidWebStatFilter</filter-name>
      <url-pattern>/*</url-pattern>
 </filter-mapping>

注意这个过滤器要放在 org.javalite.activeweb.RequestDispatcher 的前面,因为activeWeb 中请求的入口是就是这个过滤器,如果请求先被RequestDispatcher 捕获了的话就直接返回相应的静态页面或者请求结果了。这样的话,就不能捕获到API了。

然后还要在web.xml中添加druid的Servlet 用于展现监控的静态页面,注意的是这里需要设置用户名密码以及客户端的ip,一般设置为只能本机访问,更多详细的配置参考
StatViewServlet配置

  • app.config.DbConfig 类中配置数据源

因为项目中没有引入spring来实现bean的自动管理,所以我们这里需要自己把 com.alibaba.druid.pool.DruidDataSource new 出来,相应的属性配置也是用java代码来实现,如下:

    public void init(AppContext context) {
        String password = context.get("config_password", String.class);
        System.setProperty("druid.wall.logViolation", "true");      //对被认为是攻击的SQL进行LOG.error输出,设置为true表示输入日志
        System.setProperty("druid.wall.throwException", "false");   //对被认为是攻击的SQL抛出SQLException 设置为false表示不抛出异常
        Properties properties = new Properties();
        try {
            properties.load(DbConfig.class.getClassLoader().getResourceAsStream("druidConfig.properties"));
        } catch (IOException e) {
            logger.error("读取druid配置失败");
        }
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setPassword(password);                           // 这里的密码使用的是config.txt中的密码
        dataSource.configFromPropety(properties);
        dataSource.setConnectionProperties("druid.stat.mergeSql=true");            // 相似的sql语句自动融合
        
        environment("development").dataSource(dataSource);
        environment("development").testing().dataSource(dataSource);
        environment("production").dataSource(dataSource);
    }

在代码中读取 druidConfig.properties 配置文件,并把配置文件中的值设置到 DruidDataSource 当中,这个文件的内容如下

druid.url = jdbc:mysql://192.168.2.113:3306/jhbims?useSSL=false&useUnicode=true&characterEncoding=UTF-8&amp;autoReconnect=true
druid.username = jhbims
druid.password = jhbims
druid.driverClassName = com.mysql.jdbc.Driver

druid.initialSize = 5
druid.minIdle = 3
druid.maxActive = 100
drud.maxWait = 1000
druid.testOnBorrow = true
druid.filters = stat,wall

这个文件的更多配置见 DruidDataSource配置属性列表

本文由博客一文多发平台 OpenWrite 发布!

猜你喜欢

转载自www.cnblogs.com/theone67/p/12204657.html