spring配置 数据源datasource和连接池 的7种方式

1、spring-jdbc-DriverManagerDataSource

使用org.springframework.jdbc.datasource.DriverManagerDataSource 

说明:需要jar包:spring-jdbc.jar。DriverManagerDataSource只是新建连接,根本没有连接池的作用,不推荐使用

 
  1. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

  2. <property name="driverClassName" value="${jdbc.driverClassName}" />

  3. <property name="url" value="${jdbc.url}"/>

  4. <property name="username" value="${jdbc.username}"/>

  5. <property name="password" value="${jdbc.password}"/>

  6. </bean>

2、spring-JNDI

使用org.springframework.jndi.JndiObjectFactoryBean 

说明:需要在web server中配置数据源,不方便于部署,如果使用高性能服务器WebLogic或Websphere,可以配置

 
  1. <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">

  2. <property name="jndiName" value="java:comp/env/jdbc/bbt"/>

  3. </bean>

3、apache-DBCP

使用org.apache.commons.dbcp.BasicDataSource 

说明:没有自动回收空闲连接的功能,没有连接池监控,高并发的压力下稳定性有所下降,hibernate已经放弃了DBCP

 
  1. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

  2. <property name="driverClassName" value="${jdbc.driverClassName}"/>

  3. <property name="url" value="${jdbc.url}"/>

  4. <property name="username" value="${jdbc.username}"/>

  5. <property name="password" value="${jdbc.password}"/>

  6. <property name="initialSize" value="${jdbc.initialSize}"/>

  7. <property name="maxActive" value="${jdbc.maxActive}"/>

  8. <property name="maxIdle" value="${jdbc.maxIdle}"/>

  9. <property name="minIdle" value="${jdbc.minIdle}"/>

  10. <property name="maxWait" value="${jdbc.maxWait}"></property>

  11. <property name="maxOpenPreparedStatements" value="${jdbc.maxOpenPreparedStatements}" />

  12. </bean>

 

 

4、c3p0

使用com.mchange.v2.c3p0.ComboPooledDataSource

说明:需要jar包c3p0-0.9.5.1.jar

jdbc.properties

 
  1. driver=com.mysql.jdbc.Driver

  2. url=jdbc:mysql://127.0.0.1:3306/steamedbun?useUnicode=true&characterEncoding=UTF-8

  3. username=root

  4. password=root

  5.  
  6. #连接池初始化时创建的连接数

  7. c3p0.initialPoolSize=3

  8. #连接池中保留的最小连接数

  9. c3p0.minPoolSize=5

  10. #连接池中保留的最大连接数

  11. c3p0.maxPoolSize=100

  12. #连接池无空闲连接可用时,一次性创建的新连接数

  13. c3p0.acquireIncrement=3

  14. #最大空闲时间

  15. c3p0.maxIdleTime=60

  16. #每60秒检查所有连接池中的空闲连接

  17. c3p0.idleConnectionTestPeriod=60

  18. #连接池在获得新连接失败时重试的次数

  19. c3p0.acquireRetryAttempts=30

  20. #用以控制数据源内加载的PreparedStatements数量

  21. c3p0.maxStatements=100

  22. #c3p0是异步的,可以提升性能,通过多线程实现多个操作同时被执行

  23. c3p0.numHelperThreads=10

spring-mybatis.xml

 
  1. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">

  2. <property name="driverClassName" value="${driver}" />

  3. <property name="url" value="${url}" />

  4. <property name="username" value="${username}" />

  5. <property name="password" value="${password}" />

  6.  
  7. <property name="password" value="${c3p0.initialPoolSize}" />

  8. <property name="password" value="${c3p0.minPoolSize}" />

  9. <property name="password" value="${c3p0.maxPoolSize}" />

  10. <property name="password" value="${c3p0.acquireIncrement}" />

  11. <property name="password" value="${c3p0.maxIdleTime}" />

  12. <property name="password" value="${c3p0.idleConnectionTestPeriod}" />

  13. <property name="password" value="${c3p0.acquireRetryAttempts}" />

  14. <property name="password" value="${c3p0.maxStatements}" />

  15. <property name="password" value="${c3p0.numHelperThreads}" />

  16. </bean>

5、proxool使用的人比较少,有明显的性能问题,但由于它具备监控功能,因此建议在开发测试时使用

6、weblogic(oracle)的连接池和websphere(IBM)的连接池

7、alibaba druid(德鲁伊)连接池

说明:需要druid.jar,使用com.alibaba.druid.pool.DruidDataSource,据网上测试对比,比目前的DBCP或C3P0数据库连接池性能更好

web.xml

 
  1. <servlet>

  2. <servlet-name>DruidStatView</servlet-name>

  3. <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>

  4. <init-param>

  5. <!-- 允许清空统计数据 -->

  6. <param-name>resetEnable</param-name>

  7. <param-value>true</param-value>

  8. </init-param>

  9. <init-param>

  10. <!-- 用户名 -->

  11. <param-name>loginUsername</param-name>

  12. <param-value>druid</param-value>

  13. </init-param>

  14. <init-param>

  15. <!-- 密码 -->

  16. <param-name>loginPassword</param-name>

  17. <param-value>druid</param-value>

  18. </init-param>

  19. </servlet>

  20. <servlet-mapping>

  21. <servlet-name>DruidStatView</servlet-name>

  22. <url-pattern>/druid/*</url-pattern>

  23. </servlet-mapping>

  24. <filter>

  25. <filter-name>druidWebStatFilter</filter-name>

  26. <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>

  27. <init-param>

  28. <param-name>exclusions</param-name>

  29. <param-value>/public/*,*.js,*.css,/druid*,*.jsp,*.swf</param-value>

  30. </init-param>

  31. <init-param>

  32. <param-name>principalSessionName</param-name>

  33. <param-value>sessionInfo</param-value>

  34. </init-param>

  35. <init-param>

  36. <param-name>profileEnable</param-name>

  37. <param-value>true</param-value>

  38. </init-param>

  39. </filter>

  40. <filter-mapping>

  41. <filter-name>druidWebStatFilter</filter-name>

  42. <url-pattern>/*</url-pattern>

  43. </filter-mapping>

spring-mybatis.xml

 
  1. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">

  2. <property name="url" value="${db.master.url}" />

  3. <property name="username" value="${db.master.user}" />

  4. <property name="password" value="${db.master.password}" />

  5.  
  6. <property name="filters" value="mergeStat,wall,log4j2" /> <!-- 配置监控统计拦截的filters -->

  7. <property name="initialSize" value="5" />

  8. <property name="maxActive" value="100" />

  9. <property name="minIdle" value="10" />

  10. <property name="maxWait" value="60000" />

  11. <property name="validationQuery" value="SELECT 'x'" />

  12. <property name="testOnBorrow" value="true" />

  13. <property name="testOnReturn" value="true" />

  14. <property name="testWhileIdle" value="true" />

  15. <property name="timeBetweenEvictionRunsMillis" value="60000" />

  16. <property name="minEvictableIdleTimeMillis" value="300000" />

  17. <property name="removeAbandoned" value="true" />

  18. <property name="removeAbandonedTimeout" value="1800" />

  19. <property name="logAbandoned" value="true" />

  20. </bean>

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页