On Sun, 7 Nov 2004 08:39:38 -0800 (PST), Uzo Uzo <[EMAIL PROTECTED]> wrote:

I am using DBPool with pyPgSQL.  I am curious as to how transactions work with 
DBPool.

Suppose I have the following.

conn = dbpool.getConnection()
c = conn.cursor()

try:
 c.execute(foo1...) # inserts and updates
 c.execute(foo2...)
 c.execute(foo3...)
 c.close(); conn.commit(); conn.close()

except:
 #log error here
 c.close()
 conn.commit()  # is this needed here?
 conn.close()

If one of the execute should throw an exception, will the prceeding execute states before it commit or discard? is that conn.commit() in my except clause needed? thanks.



The "conn.commit()" is almost certainly NOT what you want. Indeed, you probably want the exact opposite.


By default, pyPgSQL runs with "autocommit" turned off. That means that no transactions are committed until you specifically commit them. Thus, if one of those executes throws an exception, ALL of the previous queries will be rolled back (cancelled).

This is usually what you want. If the queries within the try/except are not related to each other, so that your database will still be consistent even if some of them fail, you should insert calls to conn.commit() where one atomic unit ends and another begins.

In fact, it leads one to wonder whether the try/except is really providing any service for you here. As sages have said, "never catch an exception you do not know how to handle".

--
- Tim Roberts, [EMAIL PROTECTED]
 Providenza & Boekelheide, Inc.



-------------------------------------------------------
This SF.Net email is sponsored by:
Sybase ASE Linux Express Edition - download now for FREE
LinuxWorld Reader's Choice Award Winner for best database on Linux.
http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click
_______________________________________________
Webware-discuss mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/webware-discuss

Reply via email to