Spring配置dbcp数据源

本文介绍如何配置 Spring 中的 DBCP 数据库连接池,特别是针对数据库连接失败后的恢复策略,如使用 testWhileIdle 和 testOnBorrow 属性确保连接的有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.spring的配置文件
springmvc-servlet.xml

  1. <bean id="config" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  2. <property name="location">
  3. <value>classpath:jdbc.properties</value>
  4. </property>
  5. </bean>
  6. <!-- 注解扫描包 -->
  7. <context:component-scan base-package="com"/>
  8. <!--配置注解 -->
  9. <mvc:annotation-driven/>
  10. <mvc:resources location="/js/" mapping="/js/**"/>
  11. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  12. <property name="driverClassName" value="${driverClassName}"/>
  13. <property name="url" value="${url}"/>
  14. <property name="username" value="${username}"/>
  15. <property name="password" value="${password}"/>
  16. <!--maxActive: 最大连接数量-->
  17. <property name="maxActive" value="${maxActive}"/>
  18. <!--minIdle: 最小空闲连接-->
  19. <property name="minIdle" value="${minIdle}"/>
  20. <!--maxIdle: 最大空闲连接-->
  21. <property name="maxIdle" value="${maxIdle}"/>
  22. <!--initialSize: 初始化连接-->
  23. <property name="initialSize" value="${initialSize}"/>
  24. <!-- 连接被泄露时是否打印 -->
  25. <property name="logAbandoned" value="${logAbandoned}"/>
  26. <!-- removeAbandoned: 是否自动回收超时连接 -->
  27. <property name="removeAbandoned" value="${removeAbandoned}"/>
  28. <!-- removeAbandonedTimeout: 超时时间(以秒数为单位) -->
  29. <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}"/>
  30. <!-- maxWait: 超时等待时间以毫秒为单位 1000等于60秒 -->
  31. <property name="maxWait" value="${maxWait}"/>
  32. <!-- 在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位. -->
  33. <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}"/>
  34. <!-- 在每次空闲连接回收器线程(如果有)运行时检查的连接数量 -->
  35. <property name="numTestsPerEvictionRun" value="${numTestsPerEvictionRun}"/>
  36. <!-- 1000 * 60 * 30 连接在池中保持空闲而不被空闲连接回收器线程 -->
  37. <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}"/>
  38. <property name="validationQuery" value="${validationQuery}"/>
  39. <!-- 定时对线程池中的链接进行validateObject校验,对无效的链接进行关闭 -->
  40. <!-- <property name="testWhileIdle" value="${testWhileIdle}"/> -->
  41. <!-- 指定在从连接池中拿连接时,要检查连接是否有效,若无效会将连接从连接池中移除掉 -->
  42. <property name="testOnBorrow" value="${testOnBorrow}"/>
  43. </bean>
  44. <!-- 创建spring jdbcTemplate -->
  45. <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
  46. <property name="dataSource" ref="dataSource"></property>
  47. </bean>

jdbc.properties

  1. #db:oracle
  2. #driverClassName:oracle.jdbc.driver.OracleDriver
  3. #url:jdbc:oracle:thin:@10.4.39.67:11521/yqdb
  4. #username:om
  5. #password:Om4wd5qa
  6. db:oracle
  7. driverClassName:oracle.jdbc.driver.OracleDriver
  8. url:jdbc:oracle:thin:@localhost:1521:orcl
  9. username:lyx
  10. password:lyx
  11. maxActive:100
  12. initialSize:10
  13. maxWait:60000
  14. minIdle:10
  15. maxIdle:15
  16. logAbandoned:true
  17. removeAbandoned:true
  18. removeAbandonedTimeout:10
  19. timeBetweenEvictionRunsMillis:10000
  20. numTestsPerEvictionRun:10
  21. minEvictableIdleTimeMillis:10000
  22. validationQuery:SELECT 1 FROM DUAL
  23. #\u6821\u9A8C\u94FE\u63A5
  24. testWhileIdle:true
  25. testOnBorrow:true

建立数据库链接:

  1. package com.dbcp.conn;
  2. import java.util.List;
  3. import java.util.Map;
  4. import org.springframework.beans.BeansException;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.ApplicationContextAware;
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;
  8. import org.springframework.context.support.FileSystemXmlApplicationContext;
  9. import org.springframework.jdbc.core.JdbcTemplate;
  10. public class TestDbcp implements ApplicationContextAware
  11. {
  12. private static ApplicationContext ctx ;
  13. //获取上下文
  14. @Override
  15. public void setApplicationContext(ApplicationContext context)
  16. throws BeansException
  17. {
  18. // TODO Auto-generated method stub
  19. ctx = context;
  20. }
  21. /**
  22. * @param args
  23. */
  24. public static void main(String[] args)
  25. {
  26. // TODO Auto-generated method stub
  27. if(ctx == null)
  28. {
  29. String path = System.getProperty("user.dir") + "\\src\\springmvc-servlet.xml";
  30. //获取上下文
  31. ctx = new FileSystemXmlApplicationContext(path);
  32. //ClassPathXmlApplicationContext("classpath*:springmvc-servlet.xml");
  33. }
  34. JdbcTemplate jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");
  35. List<Map<String,Object>> resultList = jdbcTemplate.queryForList("select t.singerid,t.singername,t.singercountry from SINGER t");
  36. List<Map<String,Object>> resList = jdbcTemplate.queryForList("select * from COMMENTARY t");
  37. System.out.println("歌手表 输出查询结果:-----------------------");
  38. for (Map<String, Object> map : resultList)
  39. {
  40. System.out.println(map.get("singerid")+","+map.get("singername")+","+map.get("singercountry"));
  41. }
  42. System.out.println("评论表 输出查询结果:-----------------------");
  43. System.out.println();
  44. for (Map<String, Object> map : resList)
  45. {
  46. System.out.println(map.get("commentaryId")+","+map.get("commentary_context")+","+map.get("commentary_time"));
  47. }
  48. }
  49. }

Question

dbcp连接池数据库不能连接,当数据库恢复后,不重启应用,链接无法链接。

解决这个问题需要上数据库链接配置中的连接池配置中加上对发起链接是否有效的判断,如果无效则关闭删除,或是清空链接池中无效的连接。便于建立新的连接。


在spring的配置文件中设置:

<!-- 定时对线程池中的链接进行validateObject校验,对无效的链接进行关闭 -->
<property name="testWhileIdle" value="true"/>   

或是
<!-- 指定在从连接池中拿连接时,要检查连接是否有效,若无效会将连接从连接池中移除掉 -->
<property name="testOnBorrow" value="true"/> 

这两个属性配置其中一个都可以。



DBCP介绍:


1. DBCP连接数据库相关介绍:

http://blog.youkuaiyun.com/bonnie_ting/article/details/6822677


2. testOnBorrow和testOnReturn在生产环境一般是不开启的,主要是性能考虑。失效连接主要通过testWhileIdle保证,如果获取到了不可用的数据库连接,一般由应用处理异常。

http://www.oschina.net/question/219875_2143124

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值