问题
From the c3p0 documentation:
For some applications, high performance is more important than the risk of an occasional database exception. In its default configuration, c3p0 does no Connection testing at all. Setting a fairly long
idleConnectionTestPeriod
, and not testing on checkout and check-in at all is an excellent, high-performance approach.
If I understand the meaning of the c3p0 configuration properties correctly, if the database becomes unavailable for a short period of time and then recovers (e.g. it is restarted or a network issue appears), and if there is a fairly high usage of the connections which are pooled in the c3p0 so that no connection is idle longer than idleConnectionTestPeriod
, then none of those connections would ever be tested for validity and all attempts to use them would be unsuccessful. Basically, the connection pool would not automatically recover from the db unavailability.
Is it just bad wording in the documentation stating that this is an excellent, high-performance approach without a warning that the connection pool loses the ability to auto-recover from invalid connections, or I have misunderstood the meaning of the relevant configuration properties?
回答1:
Huh. I wrote that, probably more than a decade ago, and you are right, it isn't good advice. Originally, Connection testing in c3p0 was often very expensive (because for dbms/driver independence and certainty of a full validity test, it used an expensive metadata getTables(...) test), and I strongly advised people to make sure to test asynchronously, in checkins and idle tests. Once preferredTestQuery
and then jdbc4 Connection.isValid() made efficient reliable testing possible, I revised the docs to remove the discouragement of synchronous checks. But apparently this stayed, and it is bad advice even for the old days. In practice, if you are not testing during checkout, I recommend both async tests on checkin and idle tests, and moderately frequent idle tests (30 seconds-ish) usually, to reduce the likelihood an application sees stale Connections and the delay before Connections get refreshed after an outage.
I'll revise this going forward. Thanks for the catch.
来源:https://stackoverflow.com/questions/44807078/c3p0-connection-pool-without-testing-on-either-check-in-or-check-out