Mike, et al.,
I've got some questions about closing connections. I suspect my framework may be at fault, but there is potentially a sqlalchemy issue here as well.
See attached script with nested transaction and explicit connection.close().
Things are even more complex because versions have handled this differently in the past:
- on rel_0_9_1 and ealier, the conn.close() always actually emitted a DBAPI ROLLBACK, but on rel_0_9_2+, the previous call to .begin_nested() now prevents the DBAPI ROLLBACK call, even though the close() is on the connection itself. I'm not sure if that was an intended change, but it seems .close() on a connection should always cause ROLLBACK, no?
- rel_1_3_9 and earlier this code raises sqlalchemy.exc.ResourceClosedError on the last DBSession.close() as it invokes the registered 'rollback' event with an already-closed connection, but on current master (1.4.0b1) there is no exception since a rollback isn't attempted, leaving the db connection in idle transaction.
On all versions since rel_0_9_1, even after both of the script's finally clauses (close() statements) but before the program terminates, the transaction is still left in transaction in the database, though the connection's been checked back into the pool.
As far as whether my code here is badly formed, my question is: is it wrong to mix session closing and connection closing or should that be fine?
(My actual application is obviously more complex, with zope.sqlalchemy & transaction and frameworks; I boiled it down to this script for demo purposes and removed those libraries, making this code look weirder.)
Thanks in advance!
Kent