日期:2014-05-16  浏览次数:20601 次

Spring 之dataSource配置(jcbc,c3p0,dbcp,dndi)
1. jdbc配置
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagrDataSource">
		<property name="url" value="" />
		<property name="username" value="" />
	</bean>


2.使用已有连接池。
dbcp:
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value=""/>
        <property name="username" value="xx"/>
        <property name="password" value="xx"/>
        <property name="maxActive" value="120"/>
        <property name="initialSize" value="50"/>
        <property name="maxIdle" value="100"/>
        <property name="minIdle" value="100"/>
        <property name="maxWait" value="150"/>
        <property name="defaultAutoCommit" value="true"/>
        <property name="removeAbandoned" value="true"/>
        <property name="removeAbandonedTimeout" value="600"/>
        <property name="logAbandoned" value="true"/>
        <property name="validationQuery" value="SELECT 1"/>
        <property name="testOnBorrow" value="true" />
        <property name="testOnReturn" value="false" />
        <property name="testWhileIdle" value="true"/>
        <property name="timeBetweenEvictionRunsMillis" value="600000"/>
        <property name="minEvictableIdleTimeMillis" value="900000"/>
    </bean>


c3p0:
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value=""/>
        <property name="user" value="xx"/>
        <property name="password" value="xx"/>

        <property name="initialPoolSize" value="60" />
        <property name="minPoolSize" value="50" />
        <property name="maxPoolSize" value="100" />
        <property name="maxIdleTime" value="7200" />

        <property name="idleConnectionTestPeriod" value="360" />
        <property name="preferredTestQuery" value="select 1" />
        <property name="acquireIncrement" value="5" />
        <property name="acquireRetryAttempts" value="50" />
        <property name="acquireRetryDelay" value="1000" />
        <property name="breakAfterAcquireFailure" value="true" />

        <property name="checkoutTimeout" value="1000" />

        <property name="autoCommitOnClose" value="false" />
        <property name="forceIgnoreUnresolvedTransactions" value="false" />
        <property name="unreturnedConnectionTimeout" value="1000" />


        <property name="maxStatements" value="0" />
        <property name="maxStatementsPerConnection" value="0" />

        <property name="testConnectionOnCheckin" value="true" />
        <property name="testConnectionOnCheckout" value="false" />
        <property name="usesTraditionalReflectiveProxies" value="false" />

        <property name="numHelperThreads" value="5" />
    </bean>


3.Jndi

tomcat--context.jndi
<Resource name="jdbc/crm" author="Container" type="javax.sql.DataResource" drivermanager="oracle.jdbc.driver.OracleDriver" 
 url="" username="" 
 password="" 
 maxActive="" 
 maxidle="" 
 maxWait="" />
 	


web.xml:
  <resource-ref>
  	<res-ref-name>jdbc/crm</res-ref-name>
  	<res-type>javax.sql.DataSource</res-type>
  	<res-auth>Container</res-auth>
  </resource-ref>