From 4af32a2817f4a71ee960dc4000299de80bb1efb9 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Wed, 15 Apr 2015 14:51:21 +0100 Subject: [PATCH] Postgres does not allow you to continue using a cursor after a DB exception has been raised, so move _simple_insert or_ignore flag out of transaction --- synapse/storage/_base.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/synapse/storage/_base.py b/synapse/storage/_base.py index fa5199104..c9677710b 100644 --- a/synapse/storage/_base.py +++ b/synapse/storage/_base.py @@ -388,6 +388,7 @@ class SQLBaseStore(object): # "Simple" SQL API methods that operate on a single table with no JOINs, # no complex WHERE clauses, just a dict of values for columns. + @defer.inlineCallbacks def _simple_insert(self, table, values, or_ignore=False, desc="_simple_insert"): """Executes an INSERT query on the named table. @@ -396,14 +397,20 @@ class SQLBaseStore(object): table : string giving the table name values : dict of new column names and values for them """ - return self.runInteraction( - desc, - self._simple_insert_txn, table, values, - or_ignore=or_ignore - ) + try: + yield self.runInteraction( + desc, + self._simple_insert_txn, table, values, + or_ignore=or_ignore + ) + except self.database_engine.module.IntegrityError: + # We have to do or_ignore flag at this layer, since we can't reuse + # a cursor after we receive an error from the db. + if not or_ignore: + raise @log_function - def _simple_insert_txn(self, txn, table, values, or_ignore=False): + def _simple_insert_txn(self, txn, table, values): sql = "INSERT INTO %s (%s) VALUES(%s)" % ( table, ", ".join(k for k in values), @@ -415,11 +422,7 @@ class SQLBaseStore(object): sql, values.values(), ) - try: - txn.execute(sql, values.values()) - except self.database_engine.module.IntegrityError: - if not or_ignore: - raise + txn.execute(sql, values.values()) def _simple_upsert(self, table, keyvalues, values, desc="_simple_upsert"): """