Usage:
Xml:
<!--Druid Data source for capturing data from WEB-JDBC associated monitoring -<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></Filter><filter-mapping> <Filter-name>Druidwebstatfilter</Filter-name> <Url-pattern>/*</Url-pattern></filter-mapping>
View Code
Pom.xml:
<!--Database Ali Connection Pool - <Dependency> <groupId>Com.alibaba</groupId> <Artifactid>Druid</Artifactid> <version>1.0.4</version> </Dependency>
View Code
Spring-related configuration xml: (removeabandoned section Reference article: @ Reference Article 1,@ reference article 2)
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-4.2.xsd "> <!--Configuring the data source using Ali's druid start - <BeanID= "DataSource"class= "Com.alibaba.druid.pool.DruidDataSource"Init-method= "Init"Destroy-method= "Close"> < Propertyname= "url"value= "Jdbc:mysql://127.0.0.1:3306/yanan?useunicode=true&characterencoding=utf-8&autoreconnect=true &allowmultiqueries=true "/> < Propertyname= "username"value= "Yanan"/> < Propertyname= "Password"value= "Yanan"/> <!--Initialize connection size - < Propertyname= "InitialSize"value= " the"/> <!--Initialize connection pool maximum number of connections used - < Propertyname= "Maxactive"value= "Max"/> <!--Initialize connection pool min idle - < Propertyname= "Minidle"value= " the"/> <!--Gets the connection maximum wait time, in milliseconds - < Propertyname= "Maxwait"value= "60000"/> <!--How often the configuration interval is detected to detect idle connections that need to be closed, in milliseconds - < Propertyname= "Timebetweenevictionrunsmillis"value= "60000"/> <!--configures the minimum lifetime of a connection in a pool, in milliseconds - < Propertyname= "Minevictableidletimemillis"value= "25200000"/> <!--whether to cache PreparedStatement, that is, Pscache. Pscache has a huge performance boost for cursors-enabled databases, such as Oracle. The Pscache feature is not available in the version below mysql5.5, it is recommended to close it off. A csdn author uses pscache in version 5.5 to discover that Pscache has a cache hit rate record through the monitoring interface, which should be supported by Pscache. - < Propertyname= "Poolpreparedstatements"value= "false" /> <!--to enable Pscache, it must be configured to be greater than 0 when the Poolpreparedstatements Auto trigger is modified to true when it is greater than 0 o'clock. In Druid, there is no problem that Oracle has too much memory in the lower Pscache, you can configure this value to be larger, for example , - < Propertyname= "Maxpoolpreparedstatementperconnectionsize"value= "-1" /> < Propertyname= "Validationquery"value= "Select 1"/> < Propertyname= "Testwhileidle"value= "true"/> < Propertyname= "Testonborrow"value= "true"/> < Propertyname= "Testonreturn"value= "false"/> <!--turn on the removeabandoned feature and exceed the time limit for recycling. Track and recover those connections that cannot be managed. When the number of available connections is less used, the connection pool finds and reuses these connections that cannot be managed. - < Propertyname= "removeabandoned"value= "true"/> <!--1800 seconds, that's 30 minutes. - < Propertyname= "Removeabandonedtimeout"value= "1800"/> <!--output error log when abanded connection is closed - < Propertyname= "logabandoned"value= "true"/> <!--The property type is a string that configures the extension by alias, monitoring the filter:log4j used by the Filter:stat log for statistics. In addition, common plug-ins also have the filter:wall of defending SQL injection - < Propertyname= "Filters"value= "stat,log4j"/> </Bean> <!--using Ali's druid to configure the data source end -</Beans>
View Code
Attached: Detailed configuration parameters (@ Source article)
Configuration |
Default value |
Description |
Name |
|
The point of configuring this property is that if there are multiple data sources, the monitoring can be distinguished by name. If not configured, a name will be generated with the format: "datasource-" + system.identityhashcode (This) |
Jdbcurl |
|
The URL of the connection database is different from the database. For example: Mysql:jdbc:mysql://10.20.153.104:3306/druid2 Oracle:jdbc:oracle:thin:@10.20.149.85:1521:ocnauto |
Username |
|
User name of the connection database |
Password |
|
The password for the connection database. If you do not want the password to be written directly in the configuration file, you can use Configfilter. See more here: Https://github.com/alibaba/druid/wiki/%E4%BD%BF%E7%94%A8ConfigFilter |
Driverclassname |
Automatic recognition by URL |
This one can not be matched, if not configured Druid will automatically identify the dbtype based on the URL, and then select the appropriate driverclassname (under the recommended configuration) |
InitialSize |
0 |
The number of physical connections that were established when initializing. Initialization occurs when a call to the Init method is displayed, or the first time getconnection |
Maxactive |
8 |
Maximum number of connection pools |
Maxidle |
8 |
is no longer in use, configured and no effect |
Minidle |
|
Minimum number of connection pools |
Maxwait |
|
Gets the maximum wait time, in milliseconds, for a connection. After Maxwait is configured, a fair lock is enabled by default, and concurrency efficiency decreases, and if necessary, an unfair lock can be used by configuring the Useunfairlock property to True. |
Poolpreparedstatements |
False |
Whether to cache PreparedStatement, that is, Pscache. Pscache has a huge performance boost for cursors-enabled databases, such as Oracle. It is recommended to close under MySQL. |
Maxopenpreparedstatements |
-1 |
To enable Pscache, it must be configured to be greater than 0 when the Poolpreparedstatements Auto trigger is modified to true when it is greater than 0 o'clock. In Druid, there is no problem that Oracle Pscache takes up too much memory, so you can configure this value a bit larger, say 100. |
Validationquery |
|
The SQL that is used to detect whether a connection is valid is a query statement. If the validationquery for Null,testonborrow, Testonreturn, Testwhileidle will not function. |
Testonborrow |
True |
When you request a connection, perform a validationquery to detect if the connection is valid, and this configuration will degrade performance. |
Testonreturn |
False |
When you return the connection, perform validationquery to detect if the connection is valid, and this configuration will degrade performance |
Testwhileidle |
False |
It is recommended to configure true to not affect performance and ensure security. When applying for connection detection, if idle time is greater than Timebetweenevictionrunsmillis, perform validationquery to detect if the connection is valid. |
Timebetweenevictionrunsmillis |
|
There are two meanings: 1) Destroy thread will detect the connection time interval 2) Testwhileidle the basis of the judgment, detailed see Testwhileidle attribute description |
Numtestsperevictionrun |
|
No longer used, a druiddatasource only supports one Evictionrun |
Minevictableidletimemillis |
|
|
Connectioninitsqls |
|
SQL executed when the physical connection is initialized |
Exceptionsorter |
Automatic identification according to DBTYPE |
Discard the connection when the database throws some unrecoverable exceptions |
Filters |
|
The property type is a string, and the extension is configured by alias, and the commonly used plugins are: Filter:stat Log for monitoring statistics with FILTER:LOG4J defense SQL injection Filter:wall |
Proxyfilters |
|
The type is list<com.alibaba.druid.filter.filter> if both filters and Proxyfilters are configured, it is a composite relationship, not a replacement relationship |
About the log configuration in filters, there are 4 related configurations:log4j、
log4j2、
slf4j、
commonlogging(或
commonLogging
)
Detailed configuration please visit @ Configure _logfilter
Database Ali Connection Pool configuration