Open GoogleCodeExporter opened 9 years ago
On further investigation, it appears that uncommitted transactions ARE rolled
back if an exception occurs, but if no exception occurs and the context is
exited normally then the transaction IS committed. So what I was saying in my
previous message about the exception scenario wasn't correct.
Nevertheless, my original test is still relevant and this is still not good
behavior. There are times when I want to hand a connection object to another
module to do some updates, but maintain control over commits. If the other
module is unknowingly committing transactions (by using a context manager),
this is not good.
Original comment by toastie...@gmail.com
on 12 Apr 2015 at 7:31
I have created a git branch to give a suggestion for how this could be fixed:
https://github.com/keitherskine/pyodbc/commit/6c0a9d18e496cee00ba8d412db3a1b72e8
ee78de
The changes are as follows:
1) In connection.cpp, Connection_exit() commits only when auto-commit is on and
it's a normal (non-error) exit, otherwise it rolls back the transaction. I
believe this is the correct behavior.
I also added a call to Connection_close() at the end. Without it, I'm not sure
how the connection is being properly closed.
In cursor.cpp, I have removed all attempts to commit/rollback. When a cursor
is closed, my understanding is that there is no requirement or implication that
transactions will be committed or rolled back at the same time. I'm puzzled
why this is happening in this function. Nevertheless, the cursor should still
be closed, hence I've added a call to Cursor_close().
I've also added some tests to cover these changes.
If it helps to turn this into a pull request, let me know.
Original comment by toastie...@gmail.com
on 12 Apr 2015 at 11:06
Original issue reported on code.google.com by
toastie...@gmail.com
on 12 Apr 2015 at 5:34