hibernate和c3p0的配置,必须注意的问题

                 这几天,配置hibernate和c3p0的问题,发生了莫名其妙的问题。
每一次查询都会创建初始化的连接数。具体描述如下:
hibernate.cfg.xml 里面如下配置。
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>perty name="hibernate.c3p0.acquireRetryAttempts">30</property>
<property name="hibernate.c3p0.acquireIncrement">2</property>
<property name="hibernate.c3p0.checkoutTimeout">30000</property> 
<property name="hibernate.c3p0.idleConnectionTestPeriod">120</property>
<property name="hibernate.c3p0.maxIdleTime">180</property>
<property name="hibernate.c3p0.initialPoolSize">3</property>
<property name="hibernate.c3p0.maxPoolSize">50</property>
<property name="hibernate.c3p0.minPoolSize">1</property>
<property name="hibernate.c3p0.maxStatements">0</property>

然后,在使用的时候每一次都会创建 initialPoolSize = 3的连接数目。多刷新几次,一下子就报 too many connects错误。上网查了好久,都不知到为什么。只在javaeye上找到一条类似问题描述。
后来还是通过读 c3p0的官方文档,找到原因。
解决办法,把这条配置删除了!因为,不能在这里配置,如果需要可以通过c3p0自己的配置文件进行配置。
官方文档:
c3p0-native property name hibernate configuration key
c3p0.acquireIncrement hibernate.c3p0.acquire_increment
c3p0.idleConnectionTestPeriod hibernate.c3p0.idle_test_period
c3p0.initialPoolSize not available -- uses minimum size
c3p0.maxIdleTime hibernate.c3p0.timeout
c3p0.maxPoolSize hibernate.c3p0.max_size
c3p0.maxStatements hibernate.c3p0.max_statements
c3p0.minPoolSize hibernate.c3p0.min_size
c3p0.testConnectionsOnCheckout  hibernate.c3p0.validate hibernate 2.x only!
Remember -- these, and only these, properties must be defined in your hibernate configuration,or else they will be set to hibernate-specified defaults. All other configuration properties that you wish to set should be defined in a c3p0.properties file. (See "Overriding c3p0 defaults via c3p0.properties".)

注意看2处红色的地方:
1。c3p0.initialPoolSize not available -- uses minimum size
说明,这个配置不能在hibernate里面配置,它会使用 minPoolSize。
2。these, and only these, properties must be defined in your hibernate configuration,
  强调,在hibernate里面 只能且必须配置这些属性。

很多网上给出的配置文档,都是不正确的,或者说不规范!不知为什么就没有人提出这问题来。估计是抄来抄去的。
另外,配置这些属性在hibernate里,下面的写法是不正确的!或者说不规范。
<property name="hibernate.c3p0.acquireRetryAttempts">30</property>
<property name="hibernate.c3p0.acquireIncrement">2</property>
<property name="hibernate.c3p0.idleConnectionTestPeriod">120</property>
<property name="hibernate.c3p0.maxIdleTime">180</property>
<property name="hibernate.c3p0.maxPoolSize">50</property>
<property name="hibernate.c3p0.minPoolSize">1</property>
<property name="hibernate.c3p0.maxStatements">0</property>
经测试,每次查询会创建3个连接。为什么呢?并没有在这里配置初始化 initialPoolSize值阿?
initialPoolSize 默认为3,所以每次会创建3个连接?估计是这问题了。

所以,还是规规矩矩的用官方写的方式。至于,为什么会每次创建初始化的个数(或者说,每查一次就初始化一次,目前不清楚,是我的代码问题,还是cp30有问题。)有时间,有能力调查一下。

注:本文,只是解决了问题,没有深入探讨问题,既为什么会每次连接,每次初始化的问题。如果,有朋友看到此文,知道为什么,请告知,谢谢。

           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

猜你喜欢

转载自blog.csdn.net/qq_43679720/article/details/87199549