六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 133|回复: 0

Hibernate配置c3p0连接池

[复制链接]

升级  21.33%

20

主题

20

主题

20

主题

秀才

Rank: 2

积分
82
 楼主| 发表于 2013-1-30 01:31:22 | 显示全部楼层 |阅读模式
网上有很多Hibernate+c3p0配置方面的文章,不过大部分都是从Hibernate的文档里抄的,大概都是这样   

hibernate.connection.driver_class = org.postgresql.Driver
hibernate.connection.url = jdbc:postgresql://localhost/mydatabase
hibernate.connection.username = myuser
hibernate.connection.password = secret
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

    但是我照样配置之后,日志显示,使用的还是hibernate的内置的连接池,然后就上网查资料,jboss网站有一篇 HowTo configure the C3P0 connection pool,里面讲的也还是配置这几项,下面有人回帖说如果想让hibernate3能正常使用c3p0,还需要一个hibernate-c3p0-3.x.x.-Final.jar,于是上网下载了这个文件,还是不行,而且仔细一看,这个jar里面就一个org.hibernate.connection.C3p0ConnectionProvider, hibernate3.jar里面已经包含有这个类了,根本就不需要这个额外的jar包。
    后来又看了几个贴,还是不能解决问题,干脆直接去代码里找答案,在hibernate源代码里搜索c3p0,终于在org.hibernate.connection.ConnectionProviderFactory这个类的注释里看明白了,这个类的javadoc说“Instantiates a connection provider given either System properties or a java.util.Properties instance. The ConnectionProviderFactory first attempts to find a name of a ConnectionProvider subclass in the property hibernate.connection.provider_class. If missing, heuristics are used to choose either DriverManagerConnectionProvider, DatasourceConnectionProvider, C3P0ConnectionProvider or DBCPConnectionProvider.” 看来是要设置hibernate.connection.provider_class这个属性才行,在配置文件里加上
hibernate.connection.provider_class=org.hibernate.connection.C3P0ConnectionProvider
再次运行,终于可以使用c3p0的连接池了。
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表