I'm doing some work with PostgreSQL 9.3 using the psycopg2
database API.
I have the DB API set in the minimum isolation level ("autocommit" mode), and am managing my own transactions directly via SQL. Example:
cur = self.conn.cursor()
cur.execute("BEGIN;")
cur.execute("SELECT dbId, downloadPath, fileName, tags FROM {tableName} WHERE dlState=%s".format(tableName=self.tableName), (2, ))
ret = cur.fetchall()
cur.execute("COMMIT;")
Basically, Is the transaction that is started by the cur.execute("BEGIN;")
limited to just that cursor, or is it for the whole connection (self.conn.cursor()
)?
Some of the more complex things I am doing involve multiple separate database operations, that I logically break down into functions. Since this is all in a class which has the connection as a member, it's a lot more convenient to create cursors within each function. However, I'm not sure how creating cursors within a transaction works.
Basically, if transactions are per-connection, I can just create lots of cursors on-the-fly within the transaction. If they're per-cursor, that means I have to pass the cursor around everywhere. Which is it?
The documentation does not touch on this, though the fact that you can call connection.commit()
makes me fairly confident the transaction-control is per-connection.