spring 配置各种数据源

本文介绍了Spring框架的配置方式及多种数据库连接池的配置细节,包括dbcp和c3p0连接池的配置参数说明。
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.         xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
  4.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee"
  5.         xmlns:tx="http://www.springframework.org/schema/tx"
  6.         xsi:schemaLocation="
  7.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  8.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  9.             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
  10.             http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
  11.             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  12.     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  13.         <!-- property name="location" value="/WEB-INF/jdbc.properties"/>-->
  14.         <property name="locations">
  15.             <list>
  16.                 <!-- value>WEB-INF/mail.properties</value-->
  17.                 <value>classpath:jdbc.properties</value>
  18.             </list>
  19.         </property>
  20.     </bean>
  21.     
  22.     <!-- dbcp config -->
  23.         
  24.     <bean id="mysqldataSource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  25.         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  26.         <property name="url" value="jdbc:mysql://localhost:3306/temp?useUnicode=true&characterEncoding=utf8"/>
  27.         <property name="username" value="root"/>
  28.         <property name="password" value="sa"/>
  29.     </bean>
  30.     
  31.     <bean id="oracledataSource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  32.         <property name="driverClassName">
  33.             <value>oracle.jdbc.driver.OracleDriver</value>
  34.         </property>
  35.         <property name="url">
  36.             <value>jdbc:oracle:thin:@192.168.1.168:1521:databasename</value>
  37.         </property>
  38.         <property name="username">
  39.             <value>club</value>
  40.         </property>
  41.         <property name="password">
  42.             <value>jh89j3ekx</value>
  43.         </property>
  44.     </bean>
  45.     
  46.     <!--  c3p0 config  -->
  47.     
  48.     <bean id="mysqldataSource2"    class="com.mchange.v2.c3p0.ComboPooledDataSource"    destroy-method="close">
  49.         <!-- 指定连接数据库的驱动 -->
  50.         <property name="driverClass"
  51.             value="com.mysql.jdbc.Driver" />
  52.         <!-- 指定连接数据库的URL -->
  53.         <property name="jdbcUrl"
  54.             value="jdbc:mysql://localhost:3306/temp?useUnicode=true&characterEncoding=utf8" />
  55.         <!-- 指定连接数据库的用户名 -->
  56.         <property name="user" value="root" />
  57.         <!-- 指定连接数据库的密码 -->
  58.         <property name="password" value="sa" />
  59.         <!-- 指定连接数据库连接池的最大连接数 -->
  60.         <property name="maxPoolSize" value="20" />
  61.         <!-- 指定连接数据库连接池的最小连接数 -->
  62.         <property name="minPoolSize" value="1" />
  63.         <!-- 指定连接数据库连接池的初始化连接数 -->
  64.         <property name="initialPoolSize" value="1" />
  65.         <!-- 指定连接数据库连接池的连接的最大空闲时间 -->
  66.         <property name="maxIdleTime" value="20" />
  67.     </bean>
  68.     
  69.     
  70.     
  71.     <bean id="oracledataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">   
  72.         <property name="driverClass">   
  73.             <value>oracle.jdbc.driver.OracleDriver</value>   
  74.         </property>   
  75.         <property name="jdbcUrl">              
  76.             <value>jdbc:oracle:thin:@192.168.1.101:1521:databasename</value>   
  77.         </property>   
  78.         <property name="user">   
  79.             <value>xiaonei</value>   
  80.         </property>   
  81.         <property name="password">   
  82.             <value>p5nxtick</value>   
  83.         </property>   
  84.     </bean>   
  85.     <bean id="mysqldataSource3" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
  86.             <property name="driverClass">
  87.                 <value>com.mysql.jdbc.Driver</value>
  88.             </property>
  89.             <property name="jdbcUrl">
  90.                 <value>jdbc:mysql://localhost:3306/temp?useUnicode=true&characterEncoding=utf8</value>
  91.             </property>
  92.             <property name="user">
  93.                 <value>root</value>
  94.             </property>
  95.             <property name="password">
  96.                 <value>sa</value>
  97.             </property>
  98.      <!--连接池中保留的最小连接数。-->
  99.             <property name="minPoolSize">
  100.                 <value>5</value>
  101.             </property>
  102.      <!--连接池中保留的最大连接数。Default: 15 -->
  103.             <property name="maxPoolSize">
  104.                 <value>30</value>
  105.             </property>
  106.     <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
  107.             <property name="initialPoolSize">
  108.                 <value>10</value>
  109.             </property>
  110.      <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
  111.             <property name="maxIdleTime">
  112.                 <value>60</value>
  113.             </property>
  114.      <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
  115.             <property name="acquireIncrement">
  116.                 <value>5</value>
  117.             </property>
  118.      <!--
  119.          JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements   属于单个connection而不是整个连接池。
  120.          所以设置这个参数需要考虑到多方面的因素。   如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0
  121.      -->
  122.             <property name="maxStatements">
  123.                 <value>0</value>
  124.             </property>
  125.              <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
  126.             <property name="idleConnectionTestPeriod">
  127.                 <value>60</value>
  128.             </property>
  129.      <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
  130.             <property name="acquireRetryAttempts">
  131.                 <value>5</value>
  132.             </property>
  133.      <!--
  134.          获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效   保留,并在下次调用getConnection()的时候继续尝试获取连接。
  135.          如果设为true,那么在尝试   获取连接失败后该数据源将申明已断开并永久关闭。Default: false
  136.      -->
  137.             <property name="breakAfterAcquireFailure">
  138.                 <value>true</value>
  139.             </property>
  140.      <!--
  141.          因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的   时候都将校验其有效性。
  142.          建议使用idleConnectionTestPeriod或automaticTestTable等方法来提升连接测试的性能。Default: false
  143.      -->
  144.             <property name="testConnectionOnCheckout">
  145.                 <value>false</value>
  146.             </property>
  147.         </bean>
  148.         
  149. </beans>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值