psycopg2 and Transactions

Read the manual. What happens when a commit fails? Go on. I'll wait. What's that? That link wasn't very helpful? Try this one. So, how about it? How do you know if your commit attempt failed?

The documentation didn't really tell you, did it? Well, psycopg2 is open-source, so read the code, damn it! What's that? You're a Python programmer? So what? Oh, you aren't intimately familiar with C and the Python/C API? Well, I suppose that would be a problem. Since I'm such a nice guy, I'll tell you. If the attempt to commit fails, commit() throws an exception. Not a CommitFailedException or anything like that. Just an exception. You see, if the underlying PostgreSQL C API call indicates that the commit attempt failed, then conn_commit() in the psycopg2 Python/C glue layer will return NULL, which indicates to Python that an error occurred.

Actually, that "just an exception" bit above is pure speculation on my part. You see, the Python/C API documentation doesn't seem to want to explicitly tell you just what happens if you return NULL. I read something on StackOverflow about returning NULL indicating an error, but exactly what that means wasn't spelled out.

What's the moral of the story? Documentation is important. Don't skimp on it.