Class GooGooStatementCache

java.lang.Object
com.mchange.v2.c3p0.stmt.GooGooStatementCache
Direct Known Subclasses:
DoubleMaxStatementCache, GlobalMaxOnlyStatementCache, PerConnectionMaxOnlyStatementCache

public abstract class GooGooStatementCache extends Object
  • Constructor Details

    • GooGooStatementCache

      public GooGooStatementCache(com.mchange.v2.async.AsynchronousRunner blockingTaskAsyncRunner, com.mchange.v2.async.AsynchronousRunner deferredStatementDestroyer, boolean cancelAutomaticallyClosedStatements)
  • Method Details

    • getNumStatements

      public int getNumStatements()
    • getNumStatementsCheckedOut

      public int getNumStatementsCheckedOut()
    • getNumConnectionsWithCachedStatements

      public int getNumConnectionsWithCachedStatements()
    • dumpStatementCacheStatus

      public String dumpStatementCacheStatus()
    • waitMarkConnectionInUse

      public void waitMarkConnectionInUse(Connection physicalConnection) throws InterruptedException
      Throws:
      InterruptedException
    • tryMarkConnectionInUse

      public boolean tryMarkConnectionInUse(Connection physicalConnection)
    • unmarkConnectionInUse

      public void unmarkConnectionInUse(Connection physicalConnection)
    • inUse

      public Boolean inUse(Connection physicalConnection)
    • getStatementDestroyerNumConnectionsInUse

      public int getStatementDestroyerNumConnectionsInUse()
    • getStatementDestroyerNumConnectionsWithDeferredDestroyStatements

      public int getStatementDestroyerNumConnectionsWithDeferredDestroyStatements()
    • getStatementDestroyerNumDeferredDestroyStatements

      public int getStatementDestroyerNumDeferredDestroyStatements()
    • checkoutStatement

      public Object checkoutStatement(Connection physicalConnection, Method stmtProducingMethod, Object[] args) throws SQLException, com.mchange.v2.util.ResourceClosedException
      Throws:
      SQLException
      com.mchange.v2.util.ResourceClosedException
    • checkoutStatement

      public Object checkoutStatement(Connection physicalConnection, Method stmtProducingMethod, Object[] args, boolean[] actuallyCachedHolder) throws SQLException, com.mchange.v2.util.ResourceClosedException
      Parameters:
      actuallyCachedHolder - if non-null, set to whether the returned Statement was taken into the cache. A Statement the cache declines to keep -- because everything it held for this Connection was checked out, so nothing could be culled -- is an "overload Statement", and is returned uncached. The cache destroys it if it is ever checked in, and knows nothing about it otherwise: whoever holds one owns it. But clients that try to ensure that abandoned Statements get close()ed when parent Connections are checked-in need to know that the cache was unable to take control over the "overload Statement", so that they know to close the Statement themselves upon Connection check-in.
      Throws:
      SQLException
      com.mchange.v2.util.ResourceClosedException
    • checkinStatement

      public void checkinStatement(Object pstmt) throws SQLException
      Note that this method currently executes refreshStatement(...) synchronously. It no longer pins mainLock, however, which means it affects only operations having to do with the lifecycle of a single Connection rather than blocking operations on *all* Connections. (It pins one PooledConnection's lifecycle operations because it is typically called via a synchronized method of NewPooledConnection, and so occupies that object's monitor.) refreshStatement(...), depending on how much state a client has mutated, can require multiple JDBC operations in order to restore a Statement to the pristine state that a new client will expect. Since it is called synchronously, it does slow client threads. This is an old issue and has not occasioned complaints. But recent improvements that render refreshStatement(...) more cautious and correct also mean it may do more work than in the past. Prior to c3p0-0.15.0, refresh performed two JDBC operations. As of v0.15.0 it can perform up to nine. In the future, we may consider performing the check-in asynchronously and having this method return very quickly to the client, consistent with c3p0's usual behavior in other contexts. However, when we are handed a deferredStatementDestroyer, we rely upon clients marking when Connections are potentially in use, which includes the entire period that a Connection is checked out, including while Statements are being checked in. (We rely upon Statement-checked-out implies Connection-checked-out implies Connection marked-in-use.) Returning asynchronously while Connection operations remain pending could break that tracking, if refreshing of Statements has not completed prior to Connection check-in. In this scenario, we would become vulnerable to out-of-spec concurrent operations on a single Connection's children that all the careful tracking has been implemented to prevent. A solution would have to ensure all pending asynchronous check-ins of Statements of a given Connection be completed prior to marking the Connection no longer in use. (See C3P0PooledConnectionPool.scacheUnmarkConnectionInUseAndCheckin(...), the client Connection check-in path, which unmarks via scacheUnmarkPhysicalConnectionInUse(...))
      Throws:
      SQLException
    • checkinAll

      public void checkinAll(Connection pcon) throws SQLException
      Throws:
      SQLException
    • closeAll

      public void closeAll(Connection pcon) throws SQLException
      Throws:
      SQLException
    • close

      public void close() throws SQLException
      A known deficiency, deliberately left alone. Recorded here so that whoever next reads this loop does not have to rediscover it, and does not "fix" it without knowing what was traded. This destroys every Statement in stmtToKey, and stmtToKey holds checked-out Statements as well as idle ones. A client using a checked-out Statement holds no lock of ours -- checkoutStatement(...) returns and releases mainLock, and the client then calls JDBC freely -- so we may well close a Statement while somebody is in the middle of executing it. That is concurrent use of a Connection's children, which JDBC forbids, and unlike the asynchronous closes the IncautiousStatementDestructionManager already performs, it can put two threads inside one Statement object rather than merely inside two children of one Connection. Note that we call synchronousDestroyStatement(...) directly, so even the cautious arrangement, which exists precisely to prevent that, does not protect this path. The window is not new and is not narrow. It has always been the whole of a client's use of a checked-out Statement, which may be minutes; a client's brief JDBC work at check-in is a strict subset of it. Where it comes from: PooledDataSource.hardReset() -- and the ComboPooledDataSource property setters, which reset the pool manager -- reach C3P0PooledConnectionPool.close(), which calls this. hardReset() is documented as disruptive, "obviously disruptive, and should be [used] with great care", closing Connections that are currently checked out. It is meant to be the kill -9 of this library, for administrators working around applications that leak Connections. Tearing Statements out from under their users is arguably what makes it hard. There is an irony worth recording. A driver that dislikes a close() racing active use may respond by hanging rather than by failing, in which case this forcefulness could block the very teardown it is trying to accomplish -- and would surface as "hardReset() hangs" rather than as anything about Statements, which may be why we have never had it reported as such. Why we leave it: in more than twenty years, neither freezes nor leaks from hardReset() have been reported. Either they do not happen in practice, or they are rare enough and one-off enough that bounded leaks go unnoticed. Fixing it properly is not a small change -- it means deciding what a forced shutdown ought to do about Statements that clients still hold, which is a design question rather than a bug -- and the risk of disturbing long-stable behavior outweighs a hazard nobody has hit. If someone does report trouble here, look again: the test harness now has instruments that did not exist when this was written. FakeStatement reports two threads inside one Statement directly, and FakeDriverConfig's prepareGate and gateOnStatementMethod can drive such an interleaving deterministically rather than hoping a stress run stumbles onto it.
      Throws:
      SQLException
    • isClosed

      public boolean isClosed()
    • markCursorNameSet

      public void markCursorNameSet(Object pstmt)
    • markCloseOnCompletionSet

      public void markCloseOnCompletionSet(Object pstmt)
    • markQueryTimeoutUpdatedFrom

      public void markQueryTimeoutUpdatedFrom(Object pstmt, int from)
    • markFetchDirectionUpdatedFrom

      public void markFetchDirectionUpdatedFrom(Object pstmt, int from)
    • markFetchSizeUpdatedFrom

      public void markFetchSizeUpdatedFrom(Object pstmt, int from)
    • markMaxFieldSizeUpdatedFrom

      public void markMaxFieldSizeUpdatedFrom(Object pstmt, int from)
    • markMaxRowsUpdatedFrom

      public void markMaxRowsUpdatedFrom(Object pstmt, long from)