?
<!-- 配置數據源 -->
<bean id="propertyConfigurer"
?? class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
?? <property name="location" value="/WEB-INF/database-config.properties" />
</bean>
<bean ?id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default">
??
?? <property name="driverClass" value="${love.driver}"></property>
?? <property name="jdbcUrl" value="${love.url}"></property>
?? <property name="user" value="${love.username}"></property>
?? <property name="password" value="${love.password}"></property>
??
?? <!-- 詳細參數說明參見database-config.properties -->
?? <property name="initialPoolSize" value="${love.initialPoolSize}"></property>
?? <property name="minPoolSize" value="${love.minPoolSize}"></property>
?? <property name="maxPoolSize" value="${love.maxPoolSize}"></property>
?? <property name="maxIdleTime" value="${love.maxIdleTime}"></property>
?? <property name="acquireIncrement" value="${love.acquireIncrement}"></property>
?? <property name="idleConnectionTestPeriod" value="${love.idleConnectionTestPeriod}"></property>
?? <property name="acquireRetryAttempts" value="${love.acquireRetryAttempts}"></property>
?? <property name="breakAfterAcquireFailure" value="${love.breakAfterAcquireFailure}"></property>
?? <property name="maxStatements" value="${love.maxStatements}"></property>
?? <property name="testConnectionOnCheckout" value="${love.testConnectionOnCheckout}"></property>
</bean>
?
database-config.properties 配置:(mysql數據庫, 部份內容摘自互聯網)
##基礎配置:
love.driver=org.gjt.mm.mysql.Driver
love.url=jdbc:mysql://127.0.0.1:3306/love?autoReconnect=true&characterEncoding=utf-8
love.username=root
love.password=123456
##初始化時獲取的連接數,取值應在minPoolSize與maxPoolSize之間。Default: 3
love.initialPoolSize=10
##連接池中保留的最小連接數
love.minPoolSize=5
???
##連接池中保留的最大連接數,Default: 15
love.maxPoolSize=30
##最大空閑時間,60秒內未使用則連接被丟棄。若為0則永不丟棄。Default: 0
love.maxIdleTime=60
##當連接池中的連接耗盡的時候c3p0一次同時獲取的連接數。Default: 3
love.acquireIncrement=5
##每60秒檢查所有連接池中的空閑連接。Default: 0
love.idleConnectionTestPeriod=60
##定義在從數據庫獲取新連接失敗后重復嘗試的次數。Default: 30?
love.acquireRetryAttempts=20
##獲取連接失敗將會引起所有等待連接池來獲取連接的線程拋出異常。但是數據源仍有效
##保留,并在下次調用getConnection()的時候繼續嘗試獲取連接。如果設為true,那么在嘗試
##獲取連接失敗后該數據源將申明已斷開并永久關閉。Default: false
love.breakAfterAcquireFailure=true
##JDBC的標準參數,用以控制數據源內加載的PreparedStatements數量。但由于預緩存的statements
##屬于單個connection而不是整個連接池。所以設置這個參數需要考慮到多方面的因素。
##如果maxStatements與maxStatementsPerConnection均為0,則緩存被關閉。Default: 0
love.maxStatements=0
##因性能消耗大請只在需要的時候使用它。如果設為true那么在每個connection提交的
##時候都將校驗其有效性。建議使用idleConnectionTestPeriod或automaticTestTable
##等方法來提升連接測試的性能。Default: false
love.testConnectionOnCheckout=false
?
?
一.連接mysql數據庫
<
bean
id
="dataSource"
?class
="com.mchange.v2.c3p0.ComboPooledDataSource"
?destroy-method
="close"
abstract
="false"
singleton
="true"?
lazy-init
="default"
autowire
="default"
dependency-check
="default"
>
<
property
name
="driverClass"
>
<
value
>
com.mysql.jdbc.Driver
</
value
>
</
property
>
<
property
name
="jdbcUrl"
>
<
value
>
jdbc:mysql://192.168.0.1/databasename?useUnicode=true
&
characterEncoding=utf-8
&
autoReconnect=true
</
value
>
</
property
>
<
property
name
="user"
>
<
value
>
username
</
value
>
</
property
>
<
property
name
="password"
>
<
value
>
password
</
value
>
</
property
>
<
property
name
="minPoolSize"
>
<
value
>
10
</
value
>
</
property
>
<
property
name
="maxPoolSize"
>
<
value
>
20
</
value
>
</
property
>
<
property
name
="initialPoolSize"
>
<
value
>
10
</
value
>
</
property
>
</
bean
>
<
bean
id
="sessionFactory"
?class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
>
<
property
name
="dataSource"
>
<
ref
bean
="dataSource"
></
ref
>
</
property
>
<
property
name
="hibernateProperties"
>
<
props
>
<
prop
key
="hibernate.dialect"
>
org.hibernate.dialect.MySQLDialect
</
prop
>
<
prop
key
="hibernate.show_sql"
>
?false
</
prop
>
<
prop
key
="
hibernate.generate_statistics
"
>
true
</
prop
>
<
prop
key
="hibernate.cache.use_query_cache"
>
?true
</
prop
>
<
prop
key
="hibernate.cache.use_second_level_cache"
>
true
</
prop
>
<
prop
key
="hibernate.cache.provider_class"
>
net.sf.ehcache.hibernate.EhCacheProvider
</
prop
>
<
prop
key
="hibernate.connection.provider_class"
>
?org.hibernate.connection.C3P0ConnectionProvider
</
prop
>
<
prop
key
="hibernate.query.substitutions"
>
?true 1, false 0, yes 'Y', no 'N'
</
prop
>
<
prop?
key
="hibernate.bytecode.use_reflection_optimizer"
>
true
</
prop
>
<
prop
key
="hibernate.use_outer_join"
>
true
</
prop
>
<
prop
key
="hibernate.prepare_sql"
>
true
</
prop
>
<
prop
key
="hibernate.query.factory_class"
>
org.hibernate.hql.ast.ASTQueryTranslatorFactory
</
prop
>
</
props
>
</
property
>
<
property
name
="mappingResources"
>
<
list
>
<
value
>
xxx/xx/xx.hbm.xml
</
value
>
<
value
>
xxx/xx/xx.hbm.xml
</
value
>
</
list
>
</
property
>
</
bean
>
二.連接Oracle數據庫
<
property
name
="driverClass"
>
<
value
>
oracle.jdbc.driver.OracleDriver
</
value
>
</
property
>
<
property
name
="jdbcUrl"
>
<
value
>
jdbc:oracle:thin:@192.168.0.1:1521:orcl
</
value
>
</
property
>
<
prop
key
="hibernate.dialect"
>
org.hibernate.dialect.OracleDialect
</
prop
>
利用c3p0配置數據庫連接