From 3e1908c55d9c8aa3c92ab65c5754ff8a873187ba Mon Sep 17 00:00:00 2001 From: Christian Zagrodnick Date: Tue, 19 Mar 2013 11:41:23 +0100 Subject: [PATCH 1/2] retab to 8 whitespace characters, remove superflous whitespace --- ZPsycopgDA/db.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/ZPsycopgDA/db.py b/ZPsycopgDA/db.py index b594b3fd..37e6f9d5 100644 --- a/ZPsycopgDA/db.py +++ b/ZPsycopgDA/db.py @@ -26,7 +26,7 @@ import pool import psycopg2 from psycopg2.extensions import INTEGER, LONGINTEGER, FLOAT, BOOLEAN, DATE, TIME from psycopg2.extensions import TransactionRollbackError, register_type -from psycopg2 import NUMBER, STRING, ROWID, DATETIME +from psycopg2 import NUMBER, STRING, ROWID, DATETIME # the DB object, managing all the real query work @@ -107,17 +107,17 @@ class DB(TM, dbi_db.DB): def make_mappings(self): """Generate the mappings used later by self.convert_description().""" self.type_mappings = {} - for t, s in [(INTEGER,'i'), (LONGINTEGER, 'i'), (NUMBER, 'n'), - (BOOLEAN,'n'), (ROWID, 'i'), - (DATETIME, 'd'), (DATE, 'd'), (TIME, 'd')]: + for t, s in [(INTEGER,'i'), (LONGINTEGER, 'i'), (NUMBER, 'n'), + (BOOLEAN,'n'), (ROWID, 'i'), + (DATETIME, 'd'), (DATE, 'd'), (TIME, 'd')]: for v in t.values: - self.type_mappings[v] = (t, s) + self.type_mappings[v] = (t, s) def convert_description(self, desc, use_psycopg_types=False): """Convert DBAPI-2.0 description field to Zope format.""" items = [] for name, typ, width, ds, p, scale, null_ok in desc: - m = self.type_mappings.get(typ, (STRING, 's')) + m = self.type_mappings.get(typ, (STRING, 's')) items.append({ 'name': name, 'type': use_psycopg_types and m[0] or m[1], @@ -158,7 +158,7 @@ class DB(TM, dbi_db.DB): return () self.putconn() return self.convert_description(c.description, True) - + ## query execution ## def query(self, query_string, max_rows=None, query_data=None): @@ -205,5 +205,5 @@ class DB(TM, dbi_db.DB): except StandardError, err: self._abort() raise err - + return self.convert_description(desc), res From 5920d4f25dc5952ef32a4c4ceca93a75dc7f0fe0 Mon Sep 17 00:00:00 2001 From: Christian Zagrodnick Date: Tue, 19 Mar 2013 12:40:49 +0100 Subject: [PATCH 2/2] Fixed multi-thread connection initialization for ZPsycopgDA. The connection initialization (transaction isolation, typecasts) was only done for first connection. When there are multiple threads running in parallel, connections where used which had not been initialized correctly. --- ZPsycopgDA/test_da.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 ZPsycopgDA/test_da.py diff --git a/ZPsycopgDA/test_da.py b/ZPsycopgDA/test_da.py new file mode 100644 index 00000000..83197671 --- /dev/null +++ b/ZPsycopgDA/test_da.py @@ -0,0 +1,42 @@ +# zopectl run script to test the DA/threading behavior +# +# Usage: bin/zopectl run test_da.py "dbname=xxx" +# +from Products.ZPsycopgDA.DA import ZDATETIME +from Products.ZPsycopgDA.db import DB +import sys +import threading + + +dsn = sys.argv[1] + + +typecasts = [ZDATETIME] + + +def DA_connect(): + db = DB(dsn, tilevel=2, typecasts=typecasts) + db.open() + return db + + +def assert_casts(conn, name): + connection = conn.getcursor().connection + if (connection.string_types == + {1114: ZDATETIME, 1184: ZDATETIME}): + print '%s pass\n' % name + else: + print '%s fail (%s)\n' % (name, connection.string_types) + + +def test_connect(name): + assert_casts(conn1, name) + + +conn1 = DA_connect() +t1 = threading.Thread(target=test_connect, args=('t1',)) +t1.start() +t2 = threading.Thread(target=test_connect, args=('t2',)) +t2.start() +t1.join() +t2.join()