javalite database connection pool configuration using druid

  • Jar package introduced in the pom file
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>
  • Durid filter incorporated in web.xml 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>

Note that this filter should be placed org.javalite.activeweb.RequestDispatcher foregoing, since the entrance is requested activeWeb is this filter, if the request was first RequestDispatcher captured then returns directly to the appropriate static page or a request result. In this case, it can not capture the API.

And then also in the web.xml add druid in Servlet for presenting the monitoring of static pages, noting that this requires the user name and password for the client's ip, generally set the machine to access only, more detailed configuration reference
StatViewServlet configuration

  • In app.config.DbConfig configuration data source class

Because the project does not introduce spring to achieve automatic management of bean, so here we need to own the com.alibaba.druid.pool.DruidDataSource new new out, the corresponding attribute is configured with java code to achieve, as follows:

    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);
    }

Reading the code druidConfig.properties profile, and the profile set to a value which DruidDataSource, this document is as follows

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

More configure this file, see the configuration list of attributes DruidDataSource

This article from the blog article multiple platforms OpenWrite release!

Guess you like

Origin www.cnblogs.com/theone67/p/12204657.html