druid连接池的简单使用
2017-10-06 21:11
465 查看
- druid连接池介绍
为监控而生的数据库连接池,它是阿里巴巴开源平台上的一个项目。Druid是Java语言中最好的数据库连接池,Druid能够提供强大的监控和扩展功能(黄婆卖瓜,自卖自夸哈哈)。它可以替换DBCP和C3P0连接池。Druid提供了一个高效、功能强大、可扩展性好的数据库连接池。它与其他数据库连接池的不同是因为
1.其可以监控数据库访问性能,Druid内置提供了一个功能强大的StatFilter插件,能够详细统计SQL的执行性能,这对于线上分析数据库访问性能有帮助。
2.可以对数据库密码进行加密。直接把数据库密码写在配置文件中,这是不好的行为,容易导致安全问题。DruidDruiver和DruidDataSource都支持PasswordCallback。
3.SQL执行日志,Druid提供了不同的LogFilter,能够支持Common-Logging、Log4j和JdkLog,你可以按需要选择相应的LogFilter,监控你应用的数据库访问情况。
4.扩展JDBC,如果你要对JDBC层有编程的需求,可以通过Druid提供的Filter机制,很方便编写JDBC层的扩展插件。
在项目中使用Druid非常简单,只要修改下配置文件就可以了
目前我接触到的还只有1,2,3点
- druid的简单应用
使用方式和c3p0数据库连接池差不多。
1.数据库配置文件:
db.properties
Spring配置文件:
1.如何对数据库密码进行加密:
在命令行中执行如下命令:
可以获得加密以后的publickey和password,然后在配置文件中开启对密码的解密
2.配置文件中为什么没有driverClassName
因druid会自动根据url自动识别DriverClass的,通过识别url的前缀实现
3.一些常用配置属性:可以参考:
https://github.com/alibaba/druid/wiki/DruidDataSource%E9%85%8D%E7%BD%AE%E5%B1%9E%E6%80%A7%E5%88%97%E8%A1%A8
为监控而生的数据库连接池,它是阿里巴巴开源平台上的一个项目。Druid是Java语言中最好的数据库连接池,Druid能够提供强大的监控和扩展功能(黄婆卖瓜,自卖自夸哈哈)。它可以替换DBCP和C3P0连接池。Druid提供了一个高效、功能强大、可扩展性好的数据库连接池。它与其他数据库连接池的不同是因为
1.其可以监控数据库访问性能,Druid内置提供了一个功能强大的StatFilter插件,能够详细统计SQL的执行性能,这对于线上分析数据库访问性能有帮助。
2.可以对数据库密码进行加密。直接把数据库密码写在配置文件中,这是不好的行为,容易导致安全问题。DruidDruiver和DruidDataSource都支持PasswordCallback。
3.SQL执行日志,Druid提供了不同的LogFilter,能够支持Common-Logging、Log4j和JdkLog,你可以按需要选择相应的LogFilter,监控你应用的数据库访问情况。
4.扩展JDBC,如果你要对JDBC层有编程的需求,可以通过Druid提供的Filter机制,很方便编写JDBC层的扩展插件。
在项目中使用Druid非常简单,只要修改下配置文件就可以了
目前我接触到的还只有1,2,3点
- druid的简单应用
使用方式和c3p0数据库连接池差不多。
1.数据库配置文件:
db.properties
url=jdbc:mysql://localhost:3306/resturant username=root publickey=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIwaWa8iwmhHzR19o9QCF1gkgtU+F3fCKIBGdOOjG6fESPks2mNxnBdYp1qPdqLCidRCpjE+28QGpgLZA7/KL9sCAwEAAQ== password=c95kreacIlDNCrI4g7lYnLTOFFZnqZHHKt75hj/qo0uFAcYICyP8Ew1a/hPtfGKhs8abDmQfyooQWDPn6IcUmw== filters=stat,config initialSize=2 maxActive=300 minIdle=2 maxWait=60000 timeBetweenEvictionRunsMillis=60000 minEvictableIdleTimeMillis=300000 validationQuery=SELECT 1 testWhileIdle=true testOnBorrow=false testOnReturn=false removeAbandoned=true logAbandoned=true poolPreparedStatements=false removeAbandonedTimeout=1800 maxOpenPreparedStatements=100
Spring配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--读取配置文件--> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:db.properties</value> </list> </property> </bean> <context:component-scan base-package="com.cqupt.ssm"/> <!--数据源--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${url}"/> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <!--提示Druid数据源需要对数据库密码进行解密--> <property name="connectionProperties" value="config.decrypt=true;config.decrypt.key=${publickey}" /> <!-- 初始化连接大小 --> <property name="initialSize" value="${initialSize}" /> <!-- 连接池最大使用连接数量 --> <property name="maxActive" value="${maxActive}"/> <!-- 连接池最小空闲 --> <property name="minIdle" value="${minIdle}"/> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="${maxWait}"/> <!-- 缓存PreparedStatements,也就是PSCache,支持游标的数据库才有用 如oracle mysql5.5以上 --> <property name="poolPreparedStatements" value="${poolPreparedStatements}"/> <!--要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动修改为true。 --> <property name="maxOpenPreparedStatements" value="${maxOpenPreparedStatements}"/> <!--用来检测连接是否有效的sql,要求是一个查询。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会其作用。--> <property name="validationQuery" value="${validationQuery}" /> <!--申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。--> <property name="testOnBorrow" value="${testOnBorrow}" /> <!--关闭连接时validationQuery检测链接是否有效,做了这个配置会降低性能--> <property name="testOnReturn" value="${testOnReturn}" /> <!--建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。--> <property name="testWhileIdle" value="${testWhileIdle}" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}"/> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}"/> <!-- 对长时间不使用的连接进行关闭 --> <property name="removeAbandoned" value="${removeAbandoned}"/> <!-- 1800秒,也就是30分钟 --> <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}"/> <!-- 关闭abanded连接时输出错误日志 --> <property name="logAbandoned" value="${logAbandoned}"/> <!-- 监控数据库 --> <property name="filters" value="${filters}"/> </bean> <!--SqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" scope="prototype"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!--sqlSessionTemplate--> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!--使用注解管理事务--> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> <bean id="configurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.cqupt.ssm" /> <property name="sqlSessionTemplateBeanName" value="sqlSessionTemplate" /> <property name="annotationClass" value="org.springframework.stereotype.Repository"/> </bean> </beans>
1.如何对数据库密码进行加密:
在命令行中执行如下命令:
java -cp druid-1.0.16.jar com.alibaba.druid.filter.config.ConfigTools you_password
可以获得加密以后的publickey和password,然后在配置文件中开启对密码的解密
2.配置文件中为什么没有driverClassName
因druid会自动根据url自动识别DriverClass的,通过识别url的前缀实现
3.一些常用配置属性:可以参考:
https://github.com/alibaba/druid/wiki/DruidDataSource%E9%85%8D%E7%BD%AE%E5%B1%9E%E6%80%A7%E5%88%97%E8%A1%A8
相关文章推荐
- Druid集连接池简单使用
- DRUID连接池的简单使用
- 阿里巴巴连接池Druid简单使用
- druid连接池的使用。
- DRUID连接池的使用
- C#使用简单的连接池完成P2P聊天程序(核心代码)
- JDBC使用Druid作为连接池
- druid连接池使用
- Spring Boot下Druid连接池的使用配置分析
- JDBC之连接池DBCP与c3p0的简单使用
- SpringBoot下Druid连接池的使用配置
- [置顶] 中国最好的jdbc连接池-------Druid开源连接池的使用
- Druid数据库连接池的简单使用
- Druid连接池简单入门配置
- druid连接池使用过程遇到的一个奇怪异常
- Druid连接池简单入门配置
- 程序调试优化二:使用Druid的数据库连接池和SQL监控
- spring+mybatis+c3p0数据库连接池或druid连接池使用配置整理
- Spring Boot下Druid连接池的使用配置分析
- 连接池DBCP的简单使用