mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-02-07 21:00:33 +03:00
Use set_session instead of set_isolation_level where available
Avoid spurious queries as reported in psycopg2 ticket #125.
This commit is contained in:
parent
f1a8c075ab
commit
57e1631181
|
@ -32,7 +32,7 @@ from psycopg2 import NUMBER, STRING, ROWID, DATETIME
|
||||||
# the DB object, managing all the real query work
|
# the DB object, managing all the real query work
|
||||||
|
|
||||||
class DB(TM, dbi_db.DB):
|
class DB(TM, dbi_db.DB):
|
||||||
|
|
||||||
_p_oid = _p_changed = _registered = None
|
_p_oid = _p_changed = _registered = None
|
||||||
|
|
||||||
def __init__(self, dsn, tilevel, typecasts, enc='utf-8'):
|
def __init__(self, dsn, tilevel, typecasts, enc='utf-8'):
|
||||||
|
@ -46,13 +46,18 @@ class DB(TM, dbi_db.DB):
|
||||||
self.failures = 0
|
self.failures = 0
|
||||||
self.calls = 0
|
self.calls = 0
|
||||||
self.make_mappings()
|
self.make_mappings()
|
||||||
|
|
||||||
def getconn(self, init=True):
|
def getconn(self, init=True):
|
||||||
# if init is False we are trying to get hold on an already existing
|
# if init is False we are trying to get hold on an already existing
|
||||||
# connection, so we avoid to (re)initialize it risking errors.
|
# connection, so we avoid to (re)initialize it risking errors.
|
||||||
conn = pool.getconn(self.dsn)
|
conn = pool.getconn(self.dsn)
|
||||||
if init:
|
if init:
|
||||||
conn.set_isolation_level(int(self.tilevel))
|
# use set_session where available as in these versions
|
||||||
|
# set_isolation_level generates an extra query.
|
||||||
|
if psycopg2.__version__ >= '2.4.2':
|
||||||
|
conn.set_session(isolation_level=int(self.tilevel))
|
||||||
|
else:
|
||||||
|
conn.set_isolation_level(int(self.tilevel))
|
||||||
conn.set_client_encoding(self.encoding)
|
conn.set_client_encoding(self.encoding)
|
||||||
for tc in self.typecasts:
|
for tc in self.typecasts:
|
||||||
register_type(tc, conn)
|
register_type(tc, conn)
|
||||||
|
@ -64,7 +69,7 @@ class DB(TM, dbi_db.DB):
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
pool.putconn(self.dsn, conn, close)
|
pool.putconn(self.dsn, conn, close)
|
||||||
|
|
||||||
def getcursor(self):
|
def getcursor(self):
|
||||||
conn = self.getconn()
|
conn = self.getconn()
|
||||||
return conn.cursor()
|
return conn.cursor()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user