From 29d00f56f6e2f703c51471fa00754f530d4c7c47 Mon Sep 17 00:00:00 2001 From: Federico Di Gregorio Date: Sun, 9 Aug 2009 16:22:43 +0200 Subject: [PATCH] Fix for retry on serialization errors for ZPsycopgDA --- ChangeLog | 3 +++ ZPsycopgDA/db.py | 28 ++++++++++------------------ 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/ChangeLog b/ChangeLog index f2be2f2f..736b934e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2009-08-08 Federico Di Gregorio + * ZPsycopgDA/db.py: applied serialization error retry from Brian + Sutherland. + * Implemented connection.reset() method to reset the connection to well-know default parameters. This is much faster than closing and reopening the connection. (Suggested by a bug report by Glenn diff --git a/ZPsycopgDA/db.py b/ZPsycopgDA/db.py index a050ebc9..21711b81 100644 --- a/ZPsycopgDA/db.py +++ b/ZPsycopgDA/db.py @@ -27,7 +27,7 @@ import pool import psycopg2 from psycopg2.extensions import INTEGER, LONGINTEGER, FLOAT, BOOLEAN, DATE, TIME -from psycopg2.extensions import register_type +from psycopg2.extensions import TransactionRollbackError, register_type from psycopg2 import NUMBER, STRING, ROWID, DATETIME @@ -169,26 +169,18 @@ class DB(TM, dbi_db.DB): c.execute(qs, query_data) else: c.execute(qs) - except psycopg2.OperationalError, e: + except TransactionRollbackError: + # Ha, here we have to look like we are the ZODB raising conflict errrors, raising ZPublisher.Publish.Retry just doesn't work + logging.debug("Serialization Error, retrying transaction", exc_info=True) + raise ConflictError("TransactionRollbackError from psycopg2") + except psycopg2.OperationalError: + logging.exception("Operational error on connection, closing it.") try: - self.close() + # Only close our connection + self.putconn(True) except: + logging.debug("Something went wrong when we tried to close the pool", exc_info=True) pass - self.open() - try: - if query_data: - c.execute(qs, query_data) - else: - c.execute(qs) - except (psycopg2.ProgrammingError, - psycopg2.IntegrityError), e: - if e.args[0].find("concurrent update") > -1: - raise ConflictError - raise e - except (psycopg2.ProgrammingError, psycopg2.IntegrityError), e: - if e.args[0].find("concurrent update") > -1: - raise ConflictError - raise e if c.description is not None: nselects += 1 if c.description != desc and nselects > 1: