Clearing up reference counting circular dependencies

o Refactored the circular dependency mess around reference counting and closing statement context. This does
  not address any issues of leaking resources, it only makes the code more readable. This is a step towards
  re-merging the state and the operations, at least from the outside as seen by the API user.

o Fixed test that depended on the order of randomly-ordered list items.
o Removed unused ThreadLocal code from ThreadToStatementContextBridge
Merge pull request #1028 from jakewins/refcount-cleanup

Clearing up reference counting circular dependencies