问题
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>
回答1:
You very likely have a Connection leak. A gigantic pool size won't really help with that. Please see here.
Appendix: Robust Resource Cleanup idiom
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(); }
}
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() the
Connection.
You have to be very careful. As Keynes famously put it, there's many a slip 'twixt the cup and the lip.
来源:https://stackoverflow.com/questions/38821464/connections-is-not-available-sometimes-in-c3p0