Spring框架学习[数据源和数据库连接池配置](一)

2014-11-24 02:55:47 · 作者: · 浏览: 3

一、 数据源的配置:
* 与 Hibernate 集成最常见的一种:

< bean id = sessionFactory class = org.springframework.orm.hibernate3.LocalSessionFactoryBean >

< property name = configLocation >

< value > classpath:hibernate.cfg .xml

< bean id = transactionManager class = org.springframework.orm.hibernate3.HibernateTransactionManager >

< property name = sessionFactory >

< ref local = sessionFactory />

* DataSource 单独配置:

< bean id = dataSource class = org.apache.commons.dbcp.BasicDataSource >

< property name = driverClassName value = com.mysql.jdbc.Driver />

< property name = url value = jdbc:mysql://127.0.0.1/test />

< property name = username value = root />

< property name = password value = root />

< bean id = sessionFactory class = org.springframework.orm.hibernate3.LocalSessionFactoryBean >

< property name = dataSource ref = dataSource >

< property name = configLocation value = classpath:hibernate.cfg.xml >

< bean id = transactionManager class = org.springframework.jdbc.datasource.DataSourceTransactionManager >

< property name = dataSource >

< ref local = dataSource />

*取消掉 hibernate.cfg.xml 的配置文件直接配置在 Spring 中

org.hibernate.dialect.MySQLDialect

true

true

二、 数据库连接池的配置 :

在传统的两层结构中,客户端程序在启动时打开数据库连接,在退出程序时关闭数据库连接。这样,在整个程序运行中,每个客户端始终占用一个数据库连接,即使在大量没有数据库操作的空闲时间,如用户输入数据时,从而造成数据库连接的使用效率低下。

在三层结构模式中,数据库连接通过中间层的连接池管理。只有当用户真正需要进行数据库操作时,中间层才从连接池申请一个连接,数据库操作完毕,连接立即释放到连接池中,以供其他用户使用。这样,不仅大大提高了数据库连接的使用效率,使得大量用户可以共享较少的数据库连接,而且省去了建立连接的时间。

关于数据库连接池几个属性的说明 :

△ 最小连接数是连接池一直保持的数据库连接,所以如果应用程序对数据库连接的使用量不大,将会有大量的数据库连接资源被浪费;

△ 最大连接数是连接池能申请的最大连接数,如果数据库连接请求超过此数,后面的数据库连接请求将被加入到等待队列中,这会影响之后的数据库操作。

△ 如果最小连接数与最大连接数相差太大,那么最先的连接请求将会获利,之后超过最小连接数量的连接请求等价于建立一个新的数据库连接。不过,这些大于最小连接数的数据库连接在使用完不会马上被释放,它将被放到连接池中等待重复使用或是空闲超时后被释放。

Xml 代码

1.

2. com. mysql.jdbc.Driver

3. jdbc:mysql://localhost:3306/struts useUnicode = true & characterEncoding = GBK

1. root

2. 8888

上面的一段配置,在 c3p0 和 dbcp 中,都是必需的,因为 hibernate 会根据上述的配置来生成 connections ,再交给 c3p0 或 dbcp 管理 .

1 C3P0

只需在 hibernate.cfg.xml 中加入

Xml 代码

1. 5

2. 30

3. 1800

4. 50

还有在 classespath 中加入 c3p0-0.8.4.5.jar

2 dbcp

在 hibernate.cfg.xml 中加入

Xml 代码

1. 100

2. 1

3. 60000

4. 10

5.

6. 100

7. 1

8. 60000

9. 10

还有在 classespath 中加入 commons-pool-1.2.jar 和 commons-dbcp-1.2.1.jar.

3) proxool

在 hibernate.cfg.xml 中增加:

Xml 代码

1. dbpool

2. proxool.xml

3. org.hibernate.connection.ProxoolConnectionProvider

3) 、在与 hibernate.cfg.xml 同级目录( src 根目录下)增加 proxool.xml 文件:

Xml 代码

1.

2.

8.

9. jdbc:mysql://127.0.0.1:3306/wlsh characterEncoding = GBK & useUnicode = true & autoReconnect = true

10. com.mysql.jdbc.Driver

11.

12.

13.

14.

15.

16. 90000

17.

18. 5

19.

20. 100

21.

22. 10

23.

24.


于在 hibernate3.0 中,已经不再支持 dbcp 了, hibernate 的作者在 hibernate.org 中,明确指出在实践中发现 dbcp 有 BUG, 在某些种情会产生很多空连接不能释放,所以抛弃了对 dbcp 的支持。至于 c3p0 ,有评论说它的算法不是最优的,因为网上查资料得知:有网友做了一个实验,在同一项目中分别用了几个常用的连接池,然后测试其性能,发现 c3p0 占用资源 比较 大,效率也不高。所以,基于上述原因, proxool 不少行家推荐使用,而且暂时来说,是负面评价是最少的一个。在三星中也有项目是用 proxool 的。 从性能和出错率来说, proxool 稍微比前两种好些。 C3P0 ,稳定性似乎不错,在这方面似乎有很好的口碑。至于 性能 ,应该不是最好的,算是中规中矩的类型。
   Proxool 的口碑似乎很好,不大见到负面的评价,从官方资料上来看,有许多有用的特性和特点,也是许多人推荐的。

4 . JNDI 连接池 ,数据源已经由应用服务配置好 ( 如 Web 服务器 ) , Hibernate 需要做的只是通过 JNDI 名查找到此数据源。应用服务器将连接池对外显示为 JNDI 绑定数据源,它是 javax.jdbc.Datasource 类的一个实例。只要配置一个 Hibernate 文件,如:

hibernate.connection.datasource=java:/comp/env/jdbc/schoolproject //JNDI 名

hibernate.transaction.factory_class = net.sf.hibernate.transaction.JTATransactionFactory

hibernate.transaction.manager_loopup_class =

org.hibernate.transaction.JBossTransactionManagerLookup

hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect

同样我们可以把上面的代码配置到 Spring 中:

1 dbcp

< bean id = dataSource class = org.apache.commons.dbcp.BasicDataSource >

< property name = driverClassName value = com.mysql.jdbc.Driver />

< property name = url value = jdbc:mysql://127.0.0.1/test />

< property name = username value = root />

< property name = password value = root />

< property name = maxActive value = 80 />

< property name = maxIdle value = 20 />

< property name = maxWait value = 3000 />

3. c3p0

< bean id = dataSource class = com.mchange.v2.c3p0.ComboPooledDataSource >

< property name = driverClass value = com.mysql.jdbc.Driver />

< property name = jdbcUrl value = jdbc:mysql://127.0.0.1/test />

< property name = user value = root />

< property name = password value = root />

< property name = acquireIncrement value = 5 />

< property name = acquireRetryAttempts value = 30 />

< property name = acquireRetryDelay value = 1000 />

< property name = idleConnectionTestPeriod value = 3000 />

< property name = checkoutTimeout value = 3000 />

< property name = maxPoolSize value = 80 />

< property name = minPoolSize value = 1 />

< property name = maxStatements value = 6000 />

< property name = initialPoolSize value = 5 />

注:其他参数的意义;

C3P0 拥有比 DBCP 更丰富的配置属性,通过这些属性,可以对数据源进行各种有效的控制:
acquireIncrement :当连接池中的连接用完时, C3P0 一次性创建新连