Using Druid as a data source

Druid claims to be the best database connection pool in the Java language.

1) It can monitor database access performance. Druid provides a built-in powerful StatFilter plug-in, which can calculate the execution performance of SQL in detail, which is helpful for online analysis of database access performance.

 

2) Replace DBCP and C3P0 . Druid provides an efficient, powerful, and scalable database connection pool.

3) Database password encryption. It is bad behavior to directly write the database password in the configuration file, which can easily lead to security problems. Both DruidDruiver and DruidDataSource support PasswordCallback.

4) SQL execution log, Druid provides different LogFilters, which can support Common-Logging , Log4j and JdkLog. You can select the corresponding LogFilter as needed to monitor the database access of your application.

5) Can provide plug-in system based on Filter-Chain mode.

 

The following describes how to integrate Druid in the project:

1. maven dependencies

<dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>${druid-version}</version>
    </dependency>

 2. Configure web.xml

	<!-- druid -->
	<servlet>
		<servlet-name>DruidStatView</servlet-name>
		<servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>DruidStatView</servlet-name>
		<url-pattern>/druid/*</url-pattern>
	</servlet-mapping>

	<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>
	</filter>
	<filter-mapping>
		<filter-name>DruidWebStatFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

 3. Configure the druid data source in spring.xml

	<!-- data source druid -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
	init-method="init" destroy-method="close">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		
		 <!-- Configure initialization size, minimum, maximum -->  
	    <property name="initialSize" value="${druid.pool.size.init}" />  
	    <property name="minIdle" value="${druid.pool.size.min}" />   
	    <property name="maxActive" value="${druid.pool.size.max}" />  
	    
	    <!-- Configure the filters for monitoring statistics interception, after removing it, the monitoring interface sql cannot be counted -->
	    <property name="filters" value="stat" />
	</bean>

 

Druid FAQ:

 

1. How to open Druid's monitoring and statistics function

 Druid's monitoring and statistics function is implemented through the filter-chain extension. If you want to enable the monitoring and statistics function, configure StatFilter .

https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_StatFilter

 

2. How to use Druid's built-in monitoring page

The built-in monitoring page is a servlet .

https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_StatViewServlet%E9%85%8D%E7%BD%AE

 

3. How to configure the Web and Spring associated monitoring in the built-in monitoring?

Web Association Monitoring Configuration 

https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_%E9%85%8D%E7%BD%AEWebStatFilter

Spring association monitoring configuration 

https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_Druid%E5%92%8CSpring%E5%85%B3%E8%81%94%E7%9B%91%E6%8E%A7%E9%85%8D%E7%BD%AE

 

4. How to configure defense against SQL injection attacks

Druid provides WallFilter, which is based on SQL semantic analysis to defend against SQL injection attacks.

https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE-wallfilter

 

5. Is there any reference configuration for Druid?

Different business scenarios have different requirements. You can use our reference configuration, but it is recommended that you read the relevant documents carefully and make customized configurations after understanding. https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_DruidDataSource%E5%8F%82%E8%80%83%E9%85%8D%E7%BD%AE

 

6. I want to log the SQL executed by JDBC, how to configure it

Druid provides Log4jFilter, CommonsLogFilter and Slf4jFilter.

https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_LogFilter

 

7. My program may have a connection leak, what can I do?

Druid provides several means of monitoring connection leaks.

https://github.com/alibaba/druid/wiki/%E8%BF%9E%E6%8E%A5%E6%B3%84%E6%BC%8F%E7%9B%91%E6%B5%8B

 

8. Will the memory usage be too large when using PSCache in Druid?

Connecting to the Oracle database and opening PSCache will cause excessive memory usage in other database connection pools. Druid is the only connection pool that solves this problem.

https://github.com/alibaba/druid/wiki/Oracle%E6%95%B0%E6%8D%AE%E5%BA%93%E4%B8%8BPreparedStatementCache%E5%86%85%E5%AD%98%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88

 

9. Is there any comparison with other database connection pools?

https://github.com/alibaba/druid/wiki/%E5%90%84%E7%A7%8D%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9E%E6%8E%A5%E6%B1%A0%E5%AF%B9%E6%AF%94

 

10、从其他连接池迁移要注意什么?

不同连接池的参数参照对比:http://code.alibabatech.com/wiki/pages/viewpage.action?pageId=6947005

DBCP迁移 https://github.com/alibaba/druid/wiki/DBCP%E8%BF%81%E7%A7%BB

 

11、Druid中有没有类似Jboss DataSource中的ExceptionSorter

ExceptionSorter是JBoss DataSource中的优秀特性,Druid也有一样功能的ExceptionSorter,但不用手动配置,自动识别生效的。

具体看这里:http://code.alibabatech.com/wiki/display/Druid/ExceptionSorter

 

12、Druid中的maxIdle为什么是没用的?

maxIdle是Druid为了方便DBCP用户迁移而增加的,maxIdle是一个混乱的概念。连接池只应该有maxPoolSize和minPoolSize,druid只保留了maxActive和minIdle,分别相当于maxPoolSize和minPoolSize。

 

13、我的应用配置的是JNDI数据源,可以用DruidDataSource么?

DruidDataSource支持JNDI配置,具体看这里:https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_JNDI_Tomcat 

具体实现的类是这个:com.alibaba.druid.pool.DruidDataSourceFactory,你可以阅读代码加深理解。

 

14、我的应用已使用DBCP,是代码中写死的,怎样更换为Druid?

可以的,Druid提供了一个中完全平滑迁移DBCP的办法。

 1) 从http://repo1.maven.org/maven2/com/alibaba/druid/druid-wrapper/ 下载druid-wrapper-xxx.jar 

2) 加入druid-xxx.jar 

3) 从你的WEB-INF/lib/中删除dbcp-xxx.jar 

4) 按需要加上配置,比如JVM启动参数加上-Ddruid.filters=stat,动态配置druid的filters 

这种用法,使得可以在一些非自己开发的应用中使用Druid,例如在sonar中部署druid,sonar是一个使用jruby开发的web应用,写死了DBCP,只能够通过这种方法来更换。

 

 

15、有一些SQL执行很慢,我希望日志记录下来,怎么设置?

在StatFilter配置中有慢SQL执行日志记录。

看这里 https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_StatFilter

 

16、我希望加密我的数据库密码怎么办?

运维和DBA都不希望把密码明文直接写在配置文件中,Druid提供了数据库秘密加密的功能。

具体看这里:https://github.com/alibaba/druid/wiki/%E4%BD%BF%E7%94%A8ConfigFilter

 

17、如果DruidDataSource在init的时候失败了,不再使用,是否需要close

是的,如果DruidDataSource不再使用,必须调用close来释放资源,释放的资源包括关闭Create和Destory线程。

 

18、DruidDataSource支持哪些数据库?

理论上说,支持所有有jdbc驱动的数据库。

 

19、Oracle下jdbc executeBatch时,更新行数计算不正确

使用jdbc的executeBatch 方法,如果数据库为oracle,则无论是否成功更新到数据,返回值都是-2,而不是真正被sql更新到的记录数,这是Oracle JDBC Driver的问题,Druid不作特殊处理。

 

20、Druid如何自动根据URL自动识别DriverClass的

Druid是根据url前缀来识别DriverClass的,这样使得配置更方便简洁。

 

21、如何保存监控记录

https://github.com/alibaba/druid/wiki/%E6%80%8E%E4%B9%88%E4%BF%9D%E5%AD%98Druid%E7%9A%84%E7%9B%91%E6%8E%A7%E8%AE%B0%E5%BD%95

 

22、我想Log输出SQL执行的信息怎么办?

https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_LogFilter

 

23、如何配置Druid内置的log实现

https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AEdruid%E5%86%85%E7%BD%AE%E7%9A%84log%E5%AE%9E%E7%8E%B0

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326945280&siteId=291194637
Recommended