Isolation level changes.

This commit is contained in:
Federico Di Gregorio 2005-10-08 03:04:40 +00:00
parent 8920c2662b
commit e1688431e0
7 changed files with 54 additions and 8 deletions

View File

@ -1,3 +1,16 @@
2005-10-08 Federico Di Gregorio <fog@initd.org>
* lib/psycopg1.py: explicitly set isolation level to 2 on .connect()
to mimic psycopg 1 behaviour.
* psycopg/connection_int.c: now set isolation level from
default_transaction_isolation backend environment value.
* psycopg/pqpath.c: removed serialization level 3: now everybody
(except me) has to use the mnemonics defined in psycopg2.extensions.
* lib/extensions.py: Added mnemonics for serialization levels.
2005-10-02 Federico Di Gregorio <fog@debian.org>
* psycopg/cursor_type.c (psyco_curs_callproc): applied callproc

View File

@ -41,6 +41,15 @@ from _psycopg import adapt, adapters, encodings, connection, cursor
from _psycopg import string_types, binary_types, new_type, register_type
from _psycopg import ISQLQuote
"""Isolation level values."""
ISOLATION_LEVEL_AUTOCOMMIT = 0
ISOLATION_LEVEL_READ_COMMITED = 1
ISOLATION_LEVEL_SERIALIZABLE = 2
# Postgresql maps the the other standard values to already defined levels
ISOLATION_LEVEL_REPEATABLE_READ = ISOLATION_LEVEL_SERIALIZABLE
ISOLATION_LEVEL_READ UNCOMMITTED = ISOLATION_LEVEL_READ_COMMITED
def register_adapter(typ, callable):
"""Register 'callable' as an ISQLQuote adapter for type 'typ'."""

View File

@ -31,8 +31,9 @@ del connect
def connect(*args, **kwargs):
"""connect(dsn, ...) -> new psycopg 1.1.x compatible connection object"""
kwargs['connection_factory'] = connection
return _2psycopg.connect(*args, **kwargs)
conn = _2psycopg.connect(*args, **kwargs)
conn.set_isolation_level(2)
return conn
class connection(_2connection):
"""psycopg 1.1.x connection."""

View File

@ -53,11 +53,18 @@ conn_connect(connectionObject *self)
{
PGconn *pgconn;
PGresult *pgres;
char *data;
/* we need the initial date style to be ISO, for typecasters; if the user
later change it, she must know what she's doing... */
const char *datestyle = "SET DATESTYLE TO 'ISO'";
const char *encoding = "SHOW client_encoding";
const char *isolevel = "SHOW default_transaction_isolation";
const char *lvl1a = "read uncommitted";
const char *lvl1b = "read committed";
const char *lvl2a = "repeatable read";
const char *lvl2b = "serializable";
Py_BEGIN_ALLOW_THREADS;
pgconn = PQconnectdb(self->dsn);
@ -86,7 +93,6 @@ conn_connect(connectionObject *self)
Py_END_ALLOW_THREADS;
if (pgres == NULL || PQresultStatus(pgres) != PGRES_COMMAND_OK ) {
Dprintf("conn_connect: setting datestyle to iso FAILED");
PyErr_SetString(OperationalError, "can't set datestyle to ISO");
PQfinish(pgconn);
IFCLEARPGRES(pgres);
@ -99,7 +105,6 @@ conn_connect(connectionObject *self)
Py_END_ALLOW_THREADS;
if (pgres == NULL || PQresultStatus(pgres) != PGRES_TUPLES_OK) {
Dprintf("conn_connect: fetching current client_encoding FAILED");
PyErr_SetString(OperationalError, "can't fetch client_encoding");
PQfinish(pgconn);
IFCLEARPGRES(pgres);
@ -107,6 +112,26 @@ conn_connect(connectionObject *self)
}
self->encoding = strdup(PQgetvalue(pgres, 0, 0));
CLEARPGRES(pgres);
Py_BEGIN_ALLOW_THREADS;
pgres = PQexec(pgconn, isolevel);
Py_END_ALLOW_THREADS;
if (pgres == NULL || PQresultStatus(pgres) != PGRES_TUPLES_OK) {
PyErr_SetString(OperationalError,
"can't fetch default_isolation_level");
PQfinish(pgconn);
IFCLEARPGRES(pgres);
return -1;
}
data = PQgetvalue(pgres, 0, 0);
if ((strncmp(lvl1a, data, strlen(lvl1a)) == 0)
|| (strncmp(lvl1b, data, strlen(lvl1b)) == 0))
self->isolation_level = 1;
else if ((strncmp(lvl2a, data, strlen(lvl2a)) == 0)
|| (strncmp(lvl2b, data, strlen(lvl2b)) == 0))
self->isolation_level = 2;
CLEARPGRES(pgres);
if (PQsetnonblocking(pgconn, 1) != 0) {
Dprintf("conn_connect: PQsetnonblocking() FAILED");

View File

@ -257,7 +257,6 @@ connection_setup(connectionObject *self, char *dsn)
self->dsn = strdup(dsn);
self->notice_list = PyList_New(0);
self->closed = 0;
self->isolation_level = 1;
self->status = CONN_STATUS_READY;
self->critical = NULL;
self->async_cursor = NULL;

View File

@ -186,7 +186,6 @@ pq_begin(connectionObject *conn)
const char *query[] = {
NULL,
"BEGIN; SET TRANSACTION ISOLATION LEVEL READ COMMITTED",
"BEGIN; SET TRANSACTION ISOLATION LEVEL SERIALIZABLE",
"BEGIN; SET TRANSACTION ISOLATION LEVEL SERIALIZABLE"};
int pgstatus, retvalue = -1;

View File

@ -55,7 +55,7 @@ from distutils.command.build_ext import build_ext
from distutils.sysconfig import get_python_inc
from distutils.ccompiler import get_default_compiler
PSYCOPG_VERSION = '2.0b4'
PSYCOPG_VERSION = '2.0rc1'
version_flags = []
# to work around older distutil limitations