?
<!-- 配置數(shù)據(jù)源 -->
<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>
??
?? <!-- 詳細(xì)參數(shù)說明參見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數(shù)據(jù)庫, 部份內(nèi)容摘自互聯(lián)網(wǎng))
##基礎(chǔ)配置:
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
##初始化時(shí)獲取的連接數(shù),取值應(yīng)在minPoolSize與maxPoolSize之間。Default: 3
love.initialPoolSize=10
##連接池中保留的最小連接數(shù)
love.minPoolSize=5
???
##連接池中保留的最大連接數(shù),Default: 15
love.maxPoolSize=30
##最大空閑時(shí)間,60秒內(nèi)未使用則連接被丟棄。若為0則永不丟棄。Default: 0
love.maxIdleTime=60
##當(dāng)連接池中的連接耗盡的時(shí)候c3p0一次同時(shí)獲取的連接數(shù)。Default: 3
love.acquireIncrement=5
##每60秒檢查所有連接池中的空閑連接。Default: 0
love.idleConnectionTestPeriod=60
##定義在從數(shù)據(jù)庫獲取新連接失敗后重復(fù)嘗試的次數(shù)。Default: 30?
love.acquireRetryAttempts=20
##獲取連接失敗將會(huì)引起所有等待連接池來獲取連接的線程拋出異常。但是數(shù)據(jù)源仍有效
##保留,并在下次調(diào)用getConnection()的時(shí)候繼續(xù)嘗試獲取連接。如果設(shè)為true,那么在嘗試
##獲取連接失敗后該數(shù)據(jù)源將申明已斷開并永久關(guān)閉。Default: false
love.breakAfterAcquireFailure=true
##JDBC的標(biāo)準(zhǔn)參數(shù),用以控制數(shù)據(jù)源內(nèi)加載的PreparedStatements數(shù)量。但由于預(yù)緩存的statements
##屬于單個(gè)connection而不是整個(gè)連接池。所以設(shè)置這個(gè)參數(shù)需要考慮到多方面的因素。
##如果maxStatements與maxStatementsPerConnection均為0,則緩存被關(guān)閉。Default: 0
love.maxStatements=0
##因性能消耗大請(qǐng)只在需要的時(shí)候使用它。如果設(shè)為true那么在每個(gè)connection提交的
##時(shí)候都將校驗(yàn)其有效性。建議使用idleConnectionTestPeriod或automaticTestTable
##等方法來提升連接測(cè)試的性能。Default: false
love.testConnectionOnCheckout=false
?
?
一.連接mysql數(shù)據(jù)庫
<
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數(shù)據(jù)庫
<
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配置數(shù)據(jù)庫連接