What is Druid?
Druid is the best database connection pool in the Java language. Druid can provide powerful monitoring and extension functions.
Project configuration
1. Add dependency
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.12</version> </dependency>
2. Configure data source
applicationContext.xml
<bean id="dataSource" class= "com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="validationQuery" value="select 1"/> <property name="testWhileIdle" value="true"/> <property name="testOnBorrow" value="false"/> <property name="testOnReturn" value="false"/> <property name="keepAlive" value="true"/> <property name="phyMaxUseCount" value="100000"/> <!-- Configure monitoring statistics intercepted filters --> <property name="filters" value="config,stat,wall"/> <!--Database driven --> <property name="driverClassName" value="${jdbc.driver}"/> <!--Connected to the database url --> <property name="url" value="${jdbc.url}"/> <!--User name to connect to the database --> <property name="username" value="${jdbc.username}"/> <!--Password to connect to the database --> <property name="password" value="${jdbc.password}"/> <property name="connectionProperties" value="config.decrypt=true;config.decrypt.key=${jdbc.publicKey}"/> <!--Number of initial connections --> <property name="initialSize" value="${jdbc.initialSize}"/> <!--maximum connection --> <property name="maxActive" value="${jdbc.maxActive}"/> <!--Minimum free connection --> <property name="minIdle" value="${jdbc.minIdle}"/> </bean>
db.properties
jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver jdbc.url=jdbc:sqlserver://localhost:1433;database=ClearGds jdbc.username=sa jdbc.password=bGX7eER+IjwQNjxrqsmRXndos+v6zFZ+WCLwrPcTsBppgrZjidA9v8ROsljKVCy2qmWlYQI18JqiCqLuw82tuw== <!--Initial value when connection pool starts--> jdbc.publicKey=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJ92V6E9LQPNMeu5duQWSGKSgmAieB7B6rjaF6W1IhcEalq57sXee6MdUthxEx24f0sQwdctmXszmbrsdnmlXGcCAwEAAQ== jdbc.initialSize=3 <!--maximum connection--> jdbc.maxActive=30 <!--Minimum idle value. When the number of idle connections is less than the threshold value, the connection pool will pre apply for some connections, so as not to be late to apply when the flood peak comes--> jdbc.minIdle=1
The database password here is encrypted, and the encrypted password content is obtained by executing the command
3,Web.xml
Configure StatViewServlet
Druid built-in provides a StatViewServlet for displaying Druid statistics.
The purpose of this StatViewServlet includes:
1. Provide html page of monitoring information display
2. JSON API to provide monitoring information
<servlet> <servlet-name>DruidStatView</servlet-name> <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class> <init-param> <!-- Allow to clear statistics --> <param-name>resetEnable</param-name> <param-value>true</param-value> </init-param> <init-param> <!-- User name --> <param-name>loginUsername</param-name> <param-value>druid</param-value> </init-param> <init-param> <!-- Password --> <param-name>loginPassword</param-name> <param-value>druid</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DruidStatView</servlet-name> <url-pattern>/druid/*</url-pattern> </servlet-mapping>
Configure WebStatFilter
<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>principalSessionName</param-name> <param-value>user.user</param-value> </init-param> </filter> <filter-mapping> <filter-name>DruidWebStatFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>