问题描述
我正在一个Web项目中,在Web服务中使用c3p0。我在hibernate.cfg.xml文件中配置了以下参数。但是即使我给出的max_size为10000并且空闲测试时间为30,有时mysql服务器未提供与db的另一个连接。因此,在我重新启动服务器之前,网站正在不断加载和加载。我的日志显示打开了太多连接。在以下配置中缺少的内容。请帮帮我
I am working on a web project in which i have used c3p0 in web services.I have configured the following pararamters in hibernate.cfg.xml file.But even though i have given max_size is 10000 and idle test period is 30 ,Sometimes mysql server is not providing another connection to db.So the website is getting load and load till i restart my server.And my in log shows "too many conection are opened".What i miss in the following configuration.Please help me out
<property name="hibernate.c3p0.min_size">10</property>
<property name="hibernate.c3p0.max_size">10000</property>
<property name="hibernate.c3p0.max_statements">5000</property>
<property name="hibernate.c3p0.maxIdleTime">1000</property>
<property name="hibernate.c3p0.maxIdleTimeExcessConnections">500</property>
<property name="hibernate.c3p0.acquire_increment">100</property>
<property name="hibernate.c3p0.idle_test_period">30</property>
<property name="hibernate.c3p0.validate">true</property>
<property name="hibernate.c3p0.preferredTestQuery">SELECT 1</property>
<property name="hibernate.c3p0.testConnectionOnCheckin">true</property>
<property name="hibernate.c3p0.testConnectionOnCheckout">false</property>
推荐答案
您很可能发生了连接泄漏。巨大的游泳池规模并不能真正解决这个问题。请在。
You very likely have a Connection leak. A gigantic pool size won't really help with that. Please see here.
最好使用。但是,如果您使用的是Java的较早版本(Java 7之前的版本),或者使用的是未实现 AutoCloseable
的资源,则可能仍必须恢复到这种状态
It's best when you can to use try with resources. But if you are working with an older version of Java (pre Java 7), or with resources that don't implement AutoCloseable
you may still have to revert to this kind of thing.
Connection c = null;
OtherResource or = null;
try
{
c = cpds.getConnection();
or = getOtherResource()
// do stuff
// ...
}
finally
{
try { if (or != null) or.close(); }
catch (Exception e) { e.printStackTrace(); }
try { if (c != null) c.close(); }
catch (Exception e) { e.printStackTrace(); }
}
请注意,如果获得了Connection,则finally子句肯定会执行,并且每个资源都有一个最佳尝试的close():如果或
无法close(),则该异常不会阻止尝试进行close()。 )
连接。
Note that the finally clause will definitely be executed if the Connection is acquired, and there is a best-attempt close() of each resource: If or
fails to close(), that Exception won't prevent the attempt to close() theConnection.
您必须非常小心。正如凯恩斯(Keynes)所说的那样,杯子和唇部有很多滑动。
You have to be very careful. As Keynes famously put it, there's many a slip 'twixt the cup and the lip.
这篇关于有时在c3p0中不可用连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!