<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_user}" />
<property name="password" value="${jdbc_password}" /> <!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="1" />
<property name="minIdle" value="1" />
<property name="maxActive" value="20" /> <!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" /> <property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" /> <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> <!-- 配置监控统计拦截的filters -->
<property name="filters" value="stat" />
</bean>

通常来说,只需要配置URL、username、password以及MaxActive(最大连接数)就可以了。

PS:

如果用Oracle,则把poolPreparedStatements配置为true,mysql可以配置为false。分库分表较多的数据库,建议配置为false。

另加上java版的简单配置:

 import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import javax.sql.DataSource;
import java.sql.SQLException; @Configuration
public class DataConfig { @Bean ("dataSource")
public DataSource dataSource() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
// 数据库连接URL
dataSource.setUrl("url");
dataSource.setUsername("root"); // 数据库用户名
dataSource.setPassword("xxxxxx"); // 用户密码
dataSource.setInitialSize(3); // 初始化时建立物理连接的个数
dataSource.setMaxActive(20); // 最大连接池数量
dataSource.setMinIdle(2); // 最小连接池数量
dataSource.setMaxWait(2000); // 获取连接时最大等待时间,单位毫秒
// 是否缓存preparedStatement
dataSource.setPoolPreparedStatements(true);
dataSource.setMaxOpenPreparedStatements(100);
// 监控统计用的filter:stat 日志用的filter:log4j 防御sql注入的filter:wall
dataSource.setFilters("stat,wall");
return dataSource;
}
}

详细的配置信息,请参阅 http://www.cnblogs.com/SummerinShire/p/5828888.html

配置DruidDataSource参考(com.alibaba.druid.pool.DruidDataSource)的更多相关文章

  1. 阿里巴巴高新能数据源com.alibaba.druid.pool.DruidDataSource的jar包配置

    aspectjweaver-1.7.4.jar druid-0.2.9.jar 两个包,用于提供com.alibaba.druid.pool.DruidDataSource

  2. 【Druid】 阿里巴巴推出的国产数据库连接池com.alibaba.druid.pool.DruidDataSource

    阿里巴巴推出的国产数据库连接池,据网上测试对比,比目前的DBCP或C3P0数据库连接池性能更好   简单使用介绍 Druid与其他数据库连接池使用方法基本一样(与DBCP非常相似),将数据库的连接信息 ...

  3. com.alibaba.druid.pool.DruidDataSource : {dataSource-2} init error

    这几天准备写一个项目,其中的整合druid的时候,发现出现了下面这个错误.找了好久都没有找到.网上的各种解决方法都不对. 2018-11-07 16:26:28.940 INFO 19684 --- ...

  4. ERROR 14856 --- [reate-882003853] com.alibaba.druid.pool.DruidDataSource : create connection error, url: jdbc:mysql://localhost:3306/xhb?useUnicode=true&characterEncoding=UTF-8, errorCode 1045, sta

    ERROR 14856 --- [reate-882003853] com.alibaba.druid.pool.DruidDataSource : create connection error, ...

  5. Javassm连接数据库报错129 ERROR [com.alibaba.druid.pool.DruidDataSource] - {dataSource-1} init error

    Javassm连接数据库报错129 ERROR [com.alibaba.druid.pool.DruidDataSource] - {dataSource-1} init error 发现jdbc这 ...

  6. canal部署启动的时候报错com.alibaba.druid.pool.DruidDataSource - testWhileIdle is true, validationQuery not set

    报错信息:com.alibaba.druid.pool.DruidDataSource - testWhileIdle is true, validationQuery not set 解决方法: 找 ...

  7. com.alibaba.druid.pool.DruidDataSource

    https://www.cnblogs.com/wuyun-blog/p/5679073.html DRUID介绍 DRUID是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0.DBCP.PR ...

  8. Could not open JDBC Connection for transaction; nested exception is com.alibaba.druid.pool.GetConnection

    Could not open JDBC Connection for transaction; nested exception is com.alibaba.druid.pool.GetConnec ...

  9. com.alibaba.druid.pool.DruidPooledConnection cannot be cast to oracle.jdbc.OracleConnection 异常解决办法

    java.lang.ClassCastException: com.alibaba.druid.pool.DruidPooledConnection cannot be cast to oracle. ...

随机推荐

  1. matches field ID value 17.9.1 The LABEL element

    https://www.w3.org/TR/html401/interact/forms.html#edef-LABEL <!ELEMENT LABEL - - (%inline;)* -(LA ...

  2. IMAP和POP3有什么区别

    http://help.163.com/10/0203/13/5UJONJ4I00753VB8.html?servCode=6010237 IMAP和POP3有什么区别?   POP3协议允许电子邮件 ...

  3. apex-utility-ai-unity-survival-shooter

    The AI has the following actions available: Action Function Shoot Fires the Kalashnikov Reload Reloa ...

  4. Python中什么是set、更新、遍历set和set的特点

    dict的作用是建立一组 key 和一组 value 的映射关系,dict的key是不能重复的. 有的时候,我们只想要 dict 的 key,不关心 key 对应的 value,目的就是保证这个集合的 ...

  5. VS附加到进程调试的方法及应用场景

    应用场景:.Net做网站时,代码量很大的时候,每次调试一个网页都编译整个网站是不显示的,而且有时候整个网站是存在错误的,通不过编译.这时你又要调试某部分网页,就可以通过附加到进程调试.方法如下: (1 ...

  6. C#中扩展方法

    什么是扩展方法? 扩展方法顾名思义,就是允许向现有的“类型”添加方法,而无需创建派生类.重新编译或以其他方式修改原来类型. 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用. 扩 ...

  7. SQL Server中的Image数据类型的操作

    原文:SQL Server中的Image数据类型的操作 准备工作,在库Im_Test中建立一张表Im_Info,此表中有两个字段,分别为Pr_Id (INT),Pr_Info (IMAGE),用来存储 ...

  8. LightOj1385 - Kingdom Division(数学几何题)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1385 题意:下图中已知面积 a b c 求 d; 如果d的面积不确定,输出-1. 连接 ...

  9. QQ聊天气泡(图片拉伸不变样)、内容尺寸定制(高度随字数、字体而变)

    - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; /** QQ聊 ...

  10. How to delete expired archive log files using rman?

    he following commands will helpful to delete the expired archive log files using Oracle Recovery Man ...