BoneCP使用配置

官方網站介紹的關於BoneCP

BoneCP is a fast, free, open-source, Java database connection pool (JDBC Pool) library. If you are familiar with C3P0 and DBCP then you already know what this means. For the rest, this is a library that will manage a database connection for you to get faster database access in your application.


在Spring 當中配置BoneCP
可以使用方式:Spring+LazyDataSource(目前使用的是這樣的配置)

1
數據庫驅動包等等的都不羅嗦了。說一下依賴包
Google Guava包和 SLF4J logging 包.其中SLF4J logging需要的是slf4j-api-1.6.4.jar和slf4j-log4j12-1.6.4.jar兩個即可。
2
配置文件編寫
applicationContext.xml

<!-- Spring bean configuration. Tell Spring to bounce off BoneCP -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
<property name="targetDataSource">
<ref local="mainDataSource" />
</property>
</bean>
<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:init.properties</value>
</property>
</bean>
<bean id="mainDataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close" dependency-check="none">
<property name="driverClass">
<value>${datasource.driverClassName}</value>
</property>
<property name="jdbcUrl">
<value>${datasource.url}</value>
</property>
<property name="username">
<value>${datasource.username}</value>
</property>
<property name="password">
<value>${datasource.password}</value>
</property>

<property name="idleConnectionTestPeriod">
<value>${boneCP.idleConnectionTestPeriod}</value>
</property>

<property name="idleMaxAge">
<value>${boneCP.idleMaxAge}</value>
</property>
<property name="maxConnectionsPerPartition">
<value>${boneCP.maxConnectionsPerPartition}</value>
</property>
<property name="minConnectionsPerPartition">
<value>${boneCP.minConnectionsPerPartition}</value>
</property>
<property name="partitionCount">
<value>${boneCP.partitionCount}</value>
</property>
<property name="acquireIncrement">
<value>${boneCP.acquireIncrement}</value>
</property>
<property name="statementsCacheSize">
<value>${boneCP.statementsCacheSize}</value>
</property>
<property name="statementsCachedPerConnection">
<value>${boneCP.statementsCachedPerConnection}</value>
</property>
<property name="releaseHelperThreads">
<value>${boneCP.releaseHelperThreads}</value>
</property>
</bean>

init.properties

datasource.type=oracle
datasource.driverClassName=oracle.jdbc.driver.OracleDriver
datasource.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
datasource.username=scott
datasource.password=tiger


boneCP.idleConnectionTestPeriod=60
boneCP.idleMaxAge=240
boneCP.maxConnectionsPerPartition=30
boneCP.minConnectionsPerPartition=10
boneCP.partitionCount=3

boneCP.acquireIncrement=5
boneCP.statementsCacheSize=100
boneCP.statementsCachedPerConnection=30
boneCP.releaseHelperThreads=3

3
配置slf4j時候報錯:

Exception in thread "main" java.lang.IllegalAccessError: tried to access field
org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory
at org.slf4j.LoggerFactory.<clinit>(LoggerFactory.java:60)

解決辦法是slf4j的版本太低。更換最新的版本即可。
至此結束。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章