I’m wondering how Transactions behave when the various commands (Begin/Start Transaction, Commit etc) are given over various cursors / connections. I.e., which of the following sets of statements actually introduce a single transaction and commit it at the end?
connection = pyodbc.connect(...)
cursor = connection.cursor()
cursor.execute('START TRANSACTION')
cursor.execute('INSERT ....')
cursor.execute('COMMIT')
vs.
connection = pyodbc.connect(...)
connection.cursor().execute('START TRANSACTION')
connection.cursor().execute('INSERT ....')
connection.cursor().execute('COMMIT')
vs.
pyodbc.connect(...).cursor().execute('START TRANSACTION')
pyodbc.connect(...).cursor().execute('INSERT ....')
pyodbc.connect(...).cursor().execute('COMMIT')
(In practice, these commands are dispersed over my code, and I’m trying to figure out on which levels to introduce singletons)
Of course, I can find it out to some degree by “trying”, but I prefer a somewhat more authorative answer so I know stuff won’t break a week from now.
I’m using Python’s Database API, though I suppose this question is not necessarily python specific.
I can imagine (though I do hope for the opposite) that the question is DB-specific. For what it’s worth: we’re using MsSQL Server 2000.
The thing I came up with so far is to dance around my own question by using Python’s Database API’s transaction methods on connection as opposed to creating cursors for them. I haven’t tested it yet in a thorough manner, will posts answers here as soon as I do that.
I.e.