項(xiàng)目中用到的配置文件真實(shí)代碼:
<bean id="realPoolDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
<property name="username"><value>d1xn_user</value></property>
<property name="password"><value>d1xn_user</value></property>
<property name="url"><value>jdbc:oracle:thin:@192.168.6.80:1521:C2S</value></property>
<property name="maxActive" value="100"/>
<property name="maxIdle" value="20"/>
<property name="maxWait" value="1000"/>
<property name="defaultAutoCommit" value="false"/>
<property name="removeAbandoned" value="true"/>
<property name="removeAbandonedTimeout" value="120"/>
</bean>
<bean id="realCenterEntityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="file:/opt/webApplication/system-config/new/centerPersistence.xml"/>
<property name="dataSource" ref="realPoolDataSource"/>
<property name="persistenceUnitName" value="CENTER_PU" />
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
</bean>
<bean id="realCenterJpaTxManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="realCenterEntityManagerFactory" />
</bean>
<tx:advice id="realCeneterTxAdvice" transaction-manager="realCenterJpaTxManager">
<tx:attributes>
<tx:method name="*" />
<tx:method name="add*" read-only="true" />
<tx:method name="delete*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="get*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="realCenterUserOperation" expression_r="execution(public * *..*DBService.*(..))" />
<aop:advisor advice-ref="realCeneterTxAdvice" pointcut-ref="realCenterUserOperation" />
</aop:config>
在配置時(shí),主要難以理解的主要有:removeAbandoned 、logAbandoned、removeAbandonedTimeout、maxWait這四個(gè)參數(shù),設(shè)置了rmoveAbandoned=true 那么在getNumActive()快要到getMaxActive()的時(shí)候,系統(tǒng)會(huì)進(jìn)行無效的Connection的回收,回收的 Connection為removeAbandonedTimeout(默認(rèn)300秒)中設(shè)置的秒數(shù)后沒有使用的Connection,激活回收機(jī)制好像是getNumActive()=getMaxActive()-2。 :) 有點(diǎn)忘了。
logAbandoned=true的話,將會(huì)在回收事件后,在log中打印出回收Connection的錯(cuò)誤信息,包括在哪個(gè)地方用了Connection卻忘記關(guān)閉了,在調(diào)試的時(shí)候很有用。
在這里私人建議maxWait的時(shí)間不要設(shè)得太長,maxWait如果設(shè)置太長那么客戶端會(huì)等待很久才激發(fā)回收事件。
以下是我的配置的properties文件:
#連接設(shè)置
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:DBSERVER
jdbc.username=user
jdbc.password=pass
#<!-- 初始化連接 -->
dataSource.initialSize=10
#<!-- 最大空閑連接 -->
dataSource.maxIdle=20
#<!-- 最小空閑連接 -->
dataSource.minIdle=5
#最大連接數(shù)量
dataSource.maxActive=50
#是否在自動(dòng)回收超時(shí)連接的時(shí)候打印連接的超時(shí)錯(cuò)誤
dataSource.logAbandoned=true
#是否自動(dòng)回收超時(shí)連接
dataSource.removeAbandoned=true
#超時(shí)時(shí)間(以秒數(shù)為單位)
#設(shè)置超時(shí)時(shí)間有一個(gè)要注意的地方,超時(shí)時(shí)間=現(xiàn)在的時(shí)間-程序中創(chuàng)建Connection的時(shí)間,如果 maxActive比較大,比如超過100,那么removeAbandonedTimeout可以設(shè)置長一點(diǎn)比如180,也就是三分鐘無響應(yīng)的連接進(jìn)行回收,當(dāng)然應(yīng)用的不同設(shè)置長度也不同。
dataSource.removeAbandonedTimeout=180
#<!-- 超時(shí)等待時(shí)間以毫秒為單位 -->
#maxWait代表當(dāng)Connection用盡了,多久之后進(jìn)行回收丟失連接
dataSource.maxWait=1000