首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

BoneCP数据源配备详解

2012-10-27 
BoneCP数据源配置详解BoneCP是一个快速,开源的数据库连接池。帮你管理数据连接让你的应用程序能更快速地访

BoneCP数据源配置详解

BoneCP是一个快速,开源的数据库连接池。帮你管理数据连接让你的应用程序能更快速地访问数据库。

比C3P0/DBCP连接池快25倍。

该项目主页:http://jolbox.com/about.html

为什么 BoneCP 连接池的性能这么高呢?

1. BoneCP 不用 synchronized 关键字来处理多线程对资源的争用,而是使用 java.util.concurrent 包中的锁机制,这个包是在 JDK 1.5 才开始有的;

2. 分区机制,尽管使用了锁,但还是存在着资源争用的问题,因此 BoneCP 可配置多个连接池分区,每个分区独立管理,互不影响。

?

尽管连接池的性能并不会是一个系统中的瓶颈,但是我们单纯从连接池这个角度来看 BoneCP ,也是值得我们去学习的。

?

2???? JDBC连接属于数据源

// load the DB driver

Class.forName("org.hsqldb.jdbcDriver"); ?????

?

// create a new datasource object

BoneCPDataSource ds = new BoneCPDataSource();?

?

// set the JDBC url

ds.setJdbcUrl("jdbc:hsqldb:mem:test");??????????????

?

// set the username

ds.setUsername("sa");????????????????????????

?

// set the password

ds.setPassword("");??????????????????????????

?

// (other config options here)

ds.setXXXX(...);?????????????????????????????

???????

Connection connection;

connection = ds.getConnection(); ????????????

// fetch a connection

???????

...? do something with the connection here ...

?

// close the connection

connection.close();??????????????????????????

// close the datasource pool

ds.close();????????????????

?

2???? Spring配置数据源

<!-- BoneCP configuration -->
<bean id="mainDataSource" destroy-method="close">
?? <property name="driverClass" value="com.mysql.jdbc.Driver" />
?? <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1/yourdb" />
?? <property name="username" value="root"/>
?? <property name="password" value="abcdefgh"/>
?? <property name="idleConnectionTestPeriod" value="60"/>
?? <property name="idleMaxAge" value="240"/>
?? <property name="maxConnectionsPerPartition" value="30"/>
?? <property name="minConnectionsPerPartition" value="10"/>
?? <property name="partitionCount" value="3"/>
?? <property name="acquireIncrement" value="5"/>
?? <property name="statementCacheSize" value="100"/>
?? <property name="releaseHelperThreads" value="3"/>
</bean>

?

?

2???? Spring+Hibernate配置数据源

<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" ??????????????autowire="autodetect">
<property name="hibernateProperties">
<props>
<prop key="hibernate.connection.provider_class">com.jolbox.bonecp.provider.BoneCPConnectionProvider</prop>
<prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
<prop key="hibernate.connection.url">jdbc:mysql://127.0.0.1/yourdb</prop>
<prop key="hibernate.connection.username">root</prop>
<prop key="hibernate.connection.password">abcdefgh</prop>
<prop key="bonecp.idleMaxAge">240</prop>
<prop key="bonecp.idleConnectionTestPeriod">60</prop>
<prop key="bonecp.partitionCount">3</prop>
<prop key="bonecp.acquireIncrement">10</prop>
<prop key="bonecp.maxConnectionsPerPartition">60</prop>
<prop key="bonecp.minConnectionsPerPartition">20</prop>
<prop key="bonecp.preparedStatementCacheSize">50</prop>
<prop key="bonecp.statementsCachedPerConnection">30</prop>
<prop key="bonecp.releaseHelperThreads">3</prop>
</props>
</property>
</bean>

?

热点排行