In looking more at this issue (along with DERBY-2397) I think the dependency manager would be much cleaner if only persistent objects could be Providers.
Persistent objects are much better suited to the DependencyManager because there is a definite termination of the object, the DROP statement.
This would mean GenericPreparedStatement (GPS) would not be a Provider, i.e. no -one could create a dependency on a compiled plan. This causes problems because now some action must be taken when a GPS is no longer in use, to invalidate anything that depends on it. Not performing the invalidation would lead to a memory leak in the dependency manager. This need to know who is using a GPS has lead to the usage count, the partially valid state, generally not a clean way of handling its lifecycle. Not having GPS be a Provider would mean GPS would be like a typical java object, having a reference to the object allows it to be used.
GPS is also (I think) the only non-persistent object that is a Provider.
The only case where one plan depends on another today is when a positioned update/delete plan (GPS) depends on the plan (GPS) for the open cursor.
I don't think this dependency is needed because the positioned update/delete will depend on the table being modified during its compilation.
Thus if the cursor changes due to any change in the base table, then the positioned statement will be invalidated anyway.
The positioned code already has a different mechanism to handle when the cursor changes to a different plan (which isn't triggered by an invalidation on the original cursor, since the original cursor plan may still be valid).
Cursor change/invalidations are tested for in CurrentOfTest. I also added some new test fixtures to cover additional situations where the positioned statement needs to be invalidated or work against a different cursor.
I have changes where this GenericPreparedStatement is no longer implements Provider and thus the positioned update/delete - cursor dependency does not exist. This basically works though various errors change from "cursor not found" to "cursor is closed" and vice-versa. This would be a step to cleanup the life-cycle state of GenericPreparedStatement, thus leading to being able to null out its compile objects once it becomes invalid (ie. current plan is invalid but the object could be reprepared to make it valid again).
The change in errors is interesting, it's basically because the old code always threw 'cursor is closed' at runtime if the positioned update/delete could not find a matching cursor (having successfully compiled against one). I'm not sure this is correct, I tried to mimic the old behaviour by throwing 'cursor not found' if the connection does not have any open activations with that name, and 'cursor is closed' if the connection has an open activation (ie. java.sql.PreparedStatement) with that name, but no open result set. But I'm not sure if that is valid, one viewpoint could be that if there is no open cursor then the cursor doesn't exist and thus there is no such error as 'cursor is closed'. I couldn't see from the SQL spec any specific guidance on this (looking at DECLARE/OPEN and positioned UPDATE & DELETE), if anyone has any thoughts ...